Lean  $LEAN_TAG$
ConsoleLogHandler.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.Globalization;
18 using System.IO;
19 
20 namespace QuantConnect.Logging
21 {
22  /// <summary>
23  /// ILogHandler implementation that writes log output to console.
24  /// </summary>
26  {
27  private const string DefaultDateFormat = "yyyyMMdd HH:mm:ss.fff";
28  private readonly TextWriter _trace;
29  private readonly TextWriter _error;
30  private readonly string _dateFormat;
31 
32  /// <summary>
33  /// Initializes a new instance of the <see cref="QuantConnect.Logging.ConsoleLogHandler"/> class.
34  /// </summary>
36  : this(DefaultDateFormat)
37  {
38  }
39 
40  /// <summary>
41  /// Initializes a new instance of the <see cref="QuantConnect.Logging.ConsoleLogHandler"/> class.
42  /// </summary>
43  /// <param name="dateFormat">Specifies the date format to use when writing log messages to the console window</param>
44  public ConsoleLogHandler(string dateFormat = DefaultDateFormat)
45  {
46  // saves references to the real console text writer since in a deployed state we may overwrite this in order
47  // to redirect messages from algorithm to result handler
48  _trace = Console.Out;
49  _error = Console.Error;
50  _dateFormat = dateFormat;
51  }
52 
53  /// <summary>
54  /// Write error message to log
55  /// </summary>
56  /// <param name="text">The error text to log</param>
57  public virtual void Error(string text)
58  {
59 #if DEBUG
60  Console.ForegroundColor = ConsoleColor.Red;
61 #endif
62  _error.WriteLine($"{DateTime.UtcNow.ToString(_dateFormat, CultureInfo.InvariantCulture)} ERROR:: {text}");
63 #if DEBUG
64  Console.ResetColor();
65 #endif
66  }
67 
68  /// <summary>
69  /// Write debug message to log
70  /// </summary>
71  /// <param name="text">The debug text to log</param>
72  public virtual void Debug(string text)
73  {
74  _trace.WriteLine($"{DateTime.UtcNow.ToString(_dateFormat, CultureInfo.InvariantCulture)} DEBUG:: {text}");
75  }
76 
77  /// <summary>
78  /// Write debug message to log
79  /// </summary>
80  /// <param name="text">The trace text to log</param>
81  public virtual void Trace(string text)
82  {
83  _trace.WriteLine($"{DateTime.UtcNow.ToString(_dateFormat, CultureInfo.InvariantCulture)} TRACE:: {text}");
84  }
85 
86  /// <summary>
87  /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
88  /// </summary>
89  /// <filterpriority>2</filterpriority>
90  public void Dispose()
91  {
92  }
93  }
94 }