-
Notifications
You must be signed in to change notification settings - Fork 12
/
Logger.cs
387 lines (351 loc) · 10.5 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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
#region License
/*
* Logger.cs
*
* The MIT License
*
* Copyright (c) 2013-2015 sta.blockhead
* Copyright © 2016 Nivloc Enterprises Ltd
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#endregion
using System;
#if SSHARP
using Crestron.SimplSharp;
using Crestron.SimplSharp.CrestronIO;
using TextWriter = SSMono.IO.TextWriter;
using StreamWriter = SSMono.IO.StreamWriter;
using Crestron.SimplSharp.Reflection;
#else
using System.IO;
#endif
namespace WebSocketSharp
{
/// <summary>
/// Provides a set of methods and properties for logging.
/// </summary>
/// <remarks>
/// <para>
/// If you output a log with lower than the value of the <see cref="Logger.Level"/> property,
/// it cannot be outputted.
/// </para>
/// <para>
/// The default output action writes a log to the standard output stream and the log file
/// if the <see cref="Logger.File"/> property has a valid path to it.
/// </para>
/// <para>
/// If you would like to use the custom output action, you should set
/// the <see cref="Logger.Output"/> property to any <c>Action<LogData, string></c>
/// delegate.
/// </para>
/// </remarks>
public class Logger
{
#region Private Fields
private volatile string _file;
private volatile LogLevel _level;
private Action<LogData, string> _output;
private object _sync;
#endregion
#region Public Constructors
/// <summary>
/// Initializes a new instance of the <see cref="Logger"/> class.
/// </summary>
/// <remarks>
/// This constructor initializes the current logging level with <see cref="LogLevel.Error"/>.
/// </remarks>
public Logger ()
: this (LogLevel.Error, null, null)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="Logger"/> class with
/// the specified logging <paramref name="level"/>.
/// </summary>
/// <param name="level">
/// One of the <see cref="LogLevel"/> enum values.
/// </param>
public Logger (LogLevel level)
: this (level, null, null)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="Logger"/> class with
/// the specified logging <paramref name="level"/>, path to the log <paramref name="file"/>,
/// and <paramref name="output"/> action.
/// </summary>
/// <param name="level">
/// One of the <see cref="LogLevel"/> enum values.
/// </param>
/// <param name="file">
/// A <see cref="string"/> that represents the path to the log file.
/// </param>
/// <param name="output">
/// An <c>Action<LogData, string></c> delegate that references the method(s) used to
/// output a log. A <see cref="string"/> parameter passed to this delegate is
/// <paramref name="file"/>.
/// </param>
public Logger (LogLevel level, string file, Action<LogData, string> output)
{
_level = level;
_file = file;
_output = output ?? defaultOutput;
_sync = new object ();
}
#endregion
#region Public Properties
/// <summary>
/// Gets or sets the current path to the log file.
/// </summary>
/// <value>
/// A <see cref="string"/> that represents the current path to the log file if any.
/// </value>
public string File
{
get
{
return _file;
}
set
{
lock (_sync)
{
_file = value;
Warn (
String.Format ("The current path to the log file has been changed to {0}.", _file));
}
}
}
/// <summary>
/// Gets or sets the current logging level.
/// </summary>
/// <remarks>
/// A log with lower than the value of this property cannot be outputted.
/// </remarks>
/// <value>
/// One of the <see cref="LogLevel"/> enum values, specifies the current logging level.
/// </value>
public LogLevel Level
{
get
{
return _level;
}
set
{
lock (_sync)
{
_level = value;
Warn (String.Format ("The current logging level has been changed to {0}.", _level));
}
}
}
/// <summary>
/// Gets or sets the current output action used to output a log.
/// </summary>
/// <value>
/// <para>
/// An <c>Action<LogData, string></c> delegate that references the method(s) used to
/// output a log. A <see cref="string"/> parameter passed to this delegate is the value of
/// the <see cref="Logger.File"/> property.
/// </para>
/// <para>
/// If the value to set is <see langword="null"/>, the current output action is changed to
/// the default output action.
/// </para>
/// </value>
public Action<LogData, string> Output
{
get
{
return _output;
}
set
{
lock (_sync)
{
_output = value ?? defaultOutput;
Warn ("The current output action has been changed.");
}
}
}
#endregion
#region Private Methods
private static void defaultOutput (LogData data, string path)
{
var log = data.ToString ();
#if SSHARP
var lines = log.Split (new[] { "\r\n" }, StringSplitOptions.None);
foreach (var line in lines)
{
if (Debugger.IsAttached)
Debugger.WriteLine (line);
else
CrestronConsole.PrintLine (line);
}
#else
Console.WriteLine (log);
#endif
if (path != null && path.Length > 0)
writeToFile (log, path);
}
private void output (string message, LogLevel level)
{
lock (_sync)
{
if (_level > level)
return;
LogData data = null;
try
{
data = new LogData (level,
#if NETCF
MethodBaseEx.GetStackMethodName (2),
#else
new StackFrame (2, true),
#endif
message);
_output (data, _file);
}
catch (Exception ex)
{
data = new LogData (LogLevel.Fatal,
#if NETCF
MethodBaseEx.GetStackMethodName (0),
#else
new StackFrame (0, true),
#endif
ex.Message);
#if SSHARP
var lines = data.ToString ().Split (new[] { "\r\n" }, StringSplitOptions.None);
foreach (var line in lines)
{
if (Debugger.IsAttached)
Debugger.WriteLine (line);
else
CrestronConsole.PrintLine (line);
}
#else
Console.WriteLine (data.ToString ());
#endif
}
}
}
private static void writeToFile (string value, string path)
{
using (var writer = new StreamWriter (path, true))
using (var syncWriter = TextWriter.Synchronized (writer))
syncWriter.WriteLine (value);
}
#endregion
#region Public Methods
/// <summary>
/// Outputs <paramref name="message"/> as a log with <see cref="LogLevel.Debug"/>.
/// </summary>
/// <remarks>
/// If the current logging level is higher than <see cref="LogLevel.Debug"/>,
/// this method doesn't output <paramref name="message"/> as a log.
/// </remarks>
/// <param name="message">
/// A <see cref="string"/> that represents the message to output as a log.
/// </param>
public void Debug (string message, params object[] args)
{
if (_level > LogLevel.Debug)
return;
output (String.Format (message, args), LogLevel.Debug);
}
/// <summary>
/// Outputs <paramref name="message"/> as a log with <see cref="LogLevel.Error"/>.
/// </summary>
/// <remarks>
/// If the current logging level is higher than <see cref="LogLevel.Error"/>,
/// this method doesn't output <paramref name="message"/> as a log.
/// </remarks>
/// <param name="message">
/// A <see cref="string"/> that represents the message to output as a log.
/// </param>
public void Error (string message, params object[] args)
{
if (_level > LogLevel.Error)
return;
output (String.Format (message, args), LogLevel.Error);
}
/// <summary>
/// Outputs <paramref name="message"/> as a log with <see cref="LogLevel.Fatal"/>.
/// </summary>
/// <param name="message">
/// A <see cref="string"/> that represents the message to output as a log.
/// </param>
public void Fatal (string message, params object[] args)
{
output (String.Format (message, args), LogLevel.Fatal);
}
/// <summary>
/// Outputs <paramref name="message"/> as a log with <see cref="LogLevel.Info"/>.
/// </summary>
/// <remarks>
/// If the current logging level is higher than <see cref="LogLevel.Info"/>,
/// this method doesn't output <paramref name="message"/> as a log.
/// </remarks>
/// <param name="message">
/// A <see cref="string"/> that represents the message to output as a log.
/// </param>
public void Info (string message, params object[] args)
{
if (_level > LogLevel.Info)
return;
output (String.Format (message, args), LogLevel.Info);
}
/// <summary>
/// Outputs <paramref name="message"/> as a log with <see cref="LogLevel.Trace"/>.
/// </summary>
/// <remarks>
/// If the current logging level is higher than <see cref="LogLevel.Trace"/>,
/// this method doesn't output <paramref name="message"/> as a log.
/// </remarks>
/// <param name="message">
/// A <see cref="string"/> that represents the message to output as a log.
/// </param>
public void Trace (string message, params object[] args)
{
if (_level > LogLevel.Trace)
return;
output (String.Format (message, args), LogLevel.Trace);
}
/// <summary>
/// Outputs <paramref name="message"/> as a log with <see cref="LogLevel.Warn"/>.
/// </summary>
/// <remarks>
/// If the current logging level is higher than <see cref="LogLevel.Warn"/>,
/// this method doesn't output <paramref name="message"/> as a log.
/// </remarks>
/// <param name="message">
/// A <see cref="string"/> that represents the message to output as a log.
/// </param>
public void Warn (string message, params object[] args)
{
if (_level > LogLevel.Warn)
return;
output (String.Format (message, args), LogLevel.Warn);
}
#endregion
}
}