-
Notifications
You must be signed in to change notification settings - Fork 1
/
Logger.cs
213 lines (177 loc) · 6.06 KB
/
Logger.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Diagnostics;
using System.IO;
namespace TAInstruments.CommonInstrumentInterface
{
internal class Logger
{
#region Private members
private readonly string touchLogFileName;
private readonly string logPath;
private Thread loggerThread;
private object logLock;
private Queue<LogMessage> messageQueue;
private volatile bool loggingEnabled;
private AutoResetEvent messageAvailable;
#endregion
private class LogMessage
{
private DateTime timestamp;
private byte[] data;
private string message;
public override string ToString()
{
//
// TODO - is it better to alloc a new one all the time?
// or reuse one and clear it out?
//
StringBuilder s = new StringBuilder(timestamp.ToString() + " ", 5000);
if (message != null)
{
s.Append(message);
s.Append(" ");
}
if (data != null)
{
s.Append(BitConverter.ToString(data));
s.Replace("-", "");
}
return s.ToString();
}
public LogMessage(string message, byte [] buffer, int dataLength)
{
timestamp = DateTime.Now;
if (message != null)
{
this.message = message;
}
if (buffer != null)
{
data = new byte[dataLength];
Array.Copy(buffer, data, dataLength);
}
}
}
private void LoggerThread()
{
bool wasEnabled = false;
StreamWriter writer = null;
Random random = new Random();
while (true)
{
//
// We poll every second to support turning logging off or on.
//
bool isMessageAvailable = messageAvailable.WaitOne(1000, false);
//
// Check if we need to start logging here.
//
loggingEnabled = File.Exists(touchLogFileName);
//
// Turning it on, create a file.
//
if (loggingEnabled && !wasEnabled)
{
DateTime now = DateTime.Now;
int r = random.Next();
string filename = logPath + "\\" +
now.Year + "_" +
now.Month + "_" +
now.Day + "_" +
now.Hour + "__" +
now.Minute + "_" +
now.Second + "_" +
r + ".log";
writer = File.CreateText(filename);
}
//
// Turning it off, close the file.
//
else if (!loggingEnabled && wasEnabled)
{
writer.Flush();
writer.Close();
}
wasEnabled = loggingEnabled;
if (!isMessageAvailable || !loggingEnabled)
{
continue;
}
//
// LOCK --------------------------------------------
//
Monitor.Enter(logLock);
while (messageQueue.Count != 0)
{
LogMessage msg = messageQueue.Dequeue();
//
// UNLOCK --------------------------------------
//
Monitor.Exit(logLock);
//Debug.WriteLine(msg.ToString());
writer.WriteLine(msg.ToString());
//
// LOCK ----------------------------------------
//
Monitor.Enter(logLock);
}
//
// UNLOCK ------------------------------------------
//
Monitor.Exit(logLock);
}
}
public void Log(string message, byte [] buffer, int dataLength)
{
if (loggingEnabled)
{
LogMessage msg = new LogMessage(message, buffer, dataLength);
lock (logLock)
{
messageQueue.Enqueue(msg);
messageAvailable.Set();
}
}
}
private Logger()
{
if (Environment.OSVersion.Platform == PlatformID.WinCE)
{
logPath = @"\Temp";
touchLogFileName = @"\Temp\CIILOG";
}
else
{
logPath = @"C:\Temp";
touchLogFileName = @"C:\Temp\CIILOG";
}
logLock = new object();
loggingEnabled = false;
messageAvailable = new AutoResetEvent(false);
messageQueue = new Queue<LogMessage>(500);
loggingEnabled = File.Exists(touchLogFileName);
loggerThread = new Thread(LoggerThread);
loggerThread.IsBackground = true;
loggerThread.Name = "CII Logger";
loggerThread.Priority = ThreadPriority.AboveNormal;
loggerThread.Start();
}
#region Singleton Code
private static readonly Logger instance = new Logger();
/// <summary>
/// This is a singleton and is created in a static constructor.
/// </summary>
public static Logger Instance
{
get
{
return instance;
}
}
#endregion
}
}