forked from connamara/quickfixn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFileLog.cs
executable file
·137 lines (106 loc) · 4.03 KB
/
FileLog.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
namespace QuickFix
{
/// <summary>
/// File log implementation
/// </summary>
public class FileLog : ILog, System.IDisposable
{
private object sync_ = new object();
private System.IO.StreamWriter messageLog_;
private System.IO.StreamWriter eventLog_;
private string messageLogFileName_;
private string eventLogFileName_;
private bool _disposed = false;
public FileLog(string fileLogPath)
{
Init(fileLogPath, "GLOBAL");
}
public FileLog(string fileLogPath, SessionID sessionID)
{
Init(fileLogPath, Prefix(sessionID));
}
private void Init(string fileLogPath, string prefix)
{
if (!System.IO.Directory.Exists(fileLogPath))
System.IO.Directory.CreateDirectory(fileLogPath);
messageLogFileName_ = System.IO.Path.Combine(fileLogPath, prefix + ".messages.current.log");
eventLogFileName_ = System.IO.Path.Combine(fileLogPath, prefix + ".event.current.log");
messageLog_ = new System.IO.StreamWriter(messageLogFileName_,true);
eventLog_ = new System.IO.StreamWriter(eventLogFileName_,true);
messageLog_.AutoFlush = true;
eventLog_.AutoFlush = true;
}
public static string Prefix(SessionID sessionID)
{
System.Text.StringBuilder prefix = new System.Text.StringBuilder(sessionID.BeginString)
.Append('-').Append(sessionID.SenderCompID);
if (SessionID.IsSet(sessionID.SenderSubID))
prefix.Append('_').Append(sessionID.SenderSubID);
if (SessionID.IsSet(sessionID.SenderLocationID))
prefix.Append('_').Append(sessionID.SenderLocationID);
prefix.Append('-').Append(sessionID.TargetCompID);
if (SessionID.IsSet(sessionID.TargetSubID))
prefix.Append('_').Append(sessionID.TargetSubID);
if (SessionID.IsSet(sessionID.TargetLocationID))
prefix.Append('_').Append(sessionID.TargetLocationID);
if (SessionID.IsSet(sessionID.SessionQualifier))
prefix.Append('-').Append(sessionID.SessionQualifier);
return prefix.ToString();
}
private void DisposedCheck()
{
if (_disposed)
throw new System.ObjectDisposedException(this.GetType().Name);
}
#region Log Members
public void Clear()
{
DisposedCheck();
lock (sync_)
{
messageLog_.Close();
eventLog_.Close();
messageLog_ = new System.IO.StreamWriter(messageLogFileName_, false);
eventLog_ = new System.IO.StreamWriter(eventLogFileName_, false);
messageLog_.AutoFlush = true;
eventLog_.AutoFlush = true;
}
}
public void OnIncoming(string msg)
{
DisposedCheck();
lock (sync_)
{
messageLog_.WriteLine(Fields.Converters.DateTimeConverter.Convert(System.DateTime.UtcNow) + " : " + msg);
}
}
public void OnOutgoing(string msg)
{
DisposedCheck();
lock (sync_)
{
messageLog_.WriteLine(Fields.Converters.DateTimeConverter.Convert(System.DateTime.UtcNow) + " : " + msg);
}
}
public void OnEvent(string s)
{
DisposedCheck();
lock (sync_)
{
eventLog_.WriteLine(Fields.Converters.DateTimeConverter.Convert(System.DateTime.UtcNow) + " : "+ s);
}
}
#endregion
#region IDisposable Members
public void Dispose()
{
if (messageLog_ != null) { messageLog_.Dispose(); }
if (eventLog_ != null) { eventLog_.Dispose(); }
messageLog_ = null;
eventLog_ = null;
_disposed = true;
}
#endregion
}
}