-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTSLogProvider.cs
197 lines (147 loc) · 4.88 KB
/
TSLogProvider.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
using System;
using System.IO;
using System.Text;
using System.Threading;
namespace uWAVE_VLBL
{
/// <summary>
/// Thread-safe logger
/// </summary>
public enum LogLineType
{
INFO,
ERROR,
CRITICAL
}
public class LogEventArgs : EventArgs
{
#region Properties
public LogLineType EventType { get; private set; }
public string LogString { get; private set; }
#endregion
#region Constructor
public LogEventArgs(LogLineType eventType, Exception ex)
: this(eventType, ex.StackTrace)
{
}
public LogEventArgs(LogLineType eventType, string logString)
{
EventType = eventType;
LogString = logString;
}
#endregion
}
public class TextAddedEventArgs : EventArgs
{
#region Properties
public string Text { get; private set; }
#endregion
#region Constructor
public TextAddedEventArgs(string text)
{
Text = text;
}
#endregion
}
public class TSLogProvider
{
#region Properties
public string FileName { get; private set; }
TSQueue<string> queue;
int synLock;
DateTime prevLogLineTimeStamp;
#endregion
#region Constructor
public TSLogProvider(string fileName)
{
FileName = fileName;
queue = new TSQueue<string>(32);
queue.ItemEnqueued += new EventHandler(queue_ItemEnquequed);
prevLogLineTimeStamp = DateTime.Now;
}
#endregion
#region Methods
public void WriteStart()
{
DateTime now = DateTime.Now;
Write(string.Format("\r\n<Log started at {0}, {1}>", now.ToShortDateString(), now.ToLongTimeString()), false);
}
public string Write(Exception ex)
{
return Write(ex, true);
}
public string Write(Exception ex, bool isTimeStamp)
{
if (ex != null)
return Write(string.Format("{0} {1}", ex.Message, ex.StackTrace), isTimeStamp);
else
return string.Empty;
}
public string Write(string logString)
{
return Write(logString, true);
}
public string Write(string logString, bool isTimeStamp)
{
DateTime now = DateTime.Now;
StringBuilder sb = new StringBuilder();
if (isTimeStamp)
{
if (now.Subtract(prevLogLineTimeStamp).Days > 1)
sb.AppendFormat("Log continues at {0} ", now.ToShortDateString());
sb.AppendFormat("{0}.{1}: ", now.ToLongTimeString(), now.Millisecond.ToString("000"));
}
sb.Append(logString);
prevLogLineTimeStamp = now;
if (!logString.EndsWith("\r\n"))
sb.Append("\r\n");
var result = sb.ToString();
queue.Enqueue(result);
if (TextAddedEvent != null) TextAddedEvent(this, new TextAddedEventArgs(result));
return result;
}
public string FinishLog()
{
DateTime now = DateTime.Now;
var result = Write(string.Format("<Log finished at {0}, {1}>", now.ToShortDateString(), now.ToLongTimeString()), false);
Flush();
return result;
}
public void Flush()
{
while (queue.Count > 0)
queue_ItemEnquequed(null, null);
}
public void Restart()
{
while (Interlocked.CompareExchange(ref synLock, 1, 0) != 0) { Thread.SpinWait(1); }
WriteStart();
Interlocked.Decrement(ref synLock);
}
#endregion
#region Handlers
private void queue_ItemEnquequed(object sender, EventArgs e)
{
if (Interlocked.CompareExchange(ref synLock, 1, 0) == 0)
{
if (queue.Count > 0)
{
var lines = queue.Dump();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < lines.Length; i++)
sb.Append(lines[i]);
try
{
File.AppendAllText(FileName, sb.ToString());
}
catch { }
}
Interlocked.Decrement(ref synLock);
}
}
#endregion
#region Events
public EventHandler<TextAddedEventArgs> TextAddedEvent;
#endregion
}
}