Lean  $LEAN_TAG$
QueueLogHandler.cs
1 /*
2  * QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
3  * Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14 */
15 
16 using System;
17 using System.Collections.Concurrent;
18 using System.Globalization;
19 using System.IO;
20 
21 namespace QuantConnect.Logging
22 {
23  /// <summary>
24  /// ILogHandler implementation that queues all logs and writes them when instructed.
25  /// </summary>
27  {
28  private readonly ConcurrentQueue<LogEntry> _logs;
29  private const string DateFormat = "yyyyMMdd HH:mm:ss";
30  private readonly TextWriter _trace;
31  private readonly TextWriter _error;
32 
33  /// <summary>
34  /// Public access to the queue for log processing.
35  /// </summary>
36  public ConcurrentQueue<LogEntry> Logs
37  {
38  get { return _logs; }
39  }
40 
41  /// <summary>
42  /// LOgging event delegate
43  /// </summary>
44  public delegate void LogEventRaised(LogEntry log);
45 
46  /// <summary>
47  /// Logging Event Handler
48  /// </summary>
49  public event LogEventRaised LogEvent;
50 
51  /// <summary>
52  /// Initializes a new instance of the <see cref="QueueLogHandler"/> class.
53  /// </summary>
54  public QueueLogHandler()
55  {
56  _logs = new ConcurrentQueue<LogEntry>();
57  _trace = Console.Out;
58  _error = Console.Error;
59  }
60 
61  /// <summary>
62  /// Write error message to log
63  /// </summary>
64  /// <param name="text">The error text to log</param>
65  public void Error(string text)
66  {
67  var log = new LogEntry(text, DateTime.UtcNow, LogType.Error);
68  _logs.Enqueue(log);
69  OnLogEvent(log);
70 
71  Console.ForegroundColor = ConsoleColor.Red;
72  _error.WriteLine(DateTime.Now.ToString(DateFormat, CultureInfo.InvariantCulture) + " Error:: " + text);
73  Console.ResetColor();
74  }
75 
76  /// <summary>
77  /// Write debug message to log
78  /// </summary>
79  /// <param name="text">The debug text to log</param>
80  public void Debug(string text)
81  {
82  var log = new LogEntry(text, DateTime.UtcNow, LogType.Debug);
83  _logs.Enqueue(log);
84  OnLogEvent(log);
85 
86  _trace.WriteLine(DateTime.Now.ToString(DateFormat, CultureInfo.InvariantCulture) + " Debug:: " + text);
87  }
88 
89  /// <summary>
90  /// Write debug message to log
91  /// </summary>
92  /// <param name="text">The trace text to log</param>
93  public void Trace(string text)
94  {
95  var log = new LogEntry(text, DateTime.UtcNow, LogType.Trace);
96  _logs.Enqueue(log);
97  OnLogEvent(log);
98 
99  _trace.WriteLine(DateTime.Now.ToString(DateFormat, CultureInfo.InvariantCulture) + " Trace:: " + text);
100  }
101 
102  /// <summary>
103  /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
104  /// </summary>
105  /// <filterpriority>2</filterpriority>
106  public void Dispose()
107  {
108  }
109 
110  /// <summary>
111  /// Raise a log event safely
112  /// </summary>
113  protected virtual void OnLogEvent(LogEntry log)
114  {
115  var handler = LogEvent;
116 
117  if (handler != null)
118  {
119  handler(log);
120  }
121  }
122  }
123 }