Lean  $LEAN_TAG$
FunctionalLogHandler.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.ComponentModel.Composition;
18 using System.Globalization;
19 
20 namespace QuantConnect.Logging
21 {
22  /// <summary>
23  /// ILogHandler implementation that writes log output to result handler
24  /// </summary>
25  [PartNotDiscoverable]
27  {
28  private const string DateFormat = "yyyyMMdd HH:mm:ss";
29  private readonly Action<string> _debug;
30  private readonly Action<string> _trace;
31  private readonly Action<string> _error;
32 
33  /// <summary>
34  /// Default constructor to handle MEF.
35  /// </summary>
37  {
38 
39  }
40 
41  /// <summary>
42  /// Initializes a new instance of the <see cref="QuantConnect.Logging.FunctionalLogHandler"/> class.
43  /// </summary>
44  public FunctionalLogHandler(Action<string> debug, Action<string> trace, Action<string> error)
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  _debug = debug;
49  _trace = trace;
50  _error = error;
51  }
52 
53  /// <summary>
54  /// Write error message to log
55  /// </summary>
56  /// <param name="text">The error text to log</param>
57  public void Error(string text)
58  {
59  if (_error != null)
60  {
61  _error(DateTime.Now.ToString(DateFormat, CultureInfo.InvariantCulture) + " ERROR " + text);
62  }
63  }
64 
65  /// <summary>
66  /// Write debug message to log
67  /// </summary>
68  /// <param name="text">The debug text to log</param>
69  public void Debug(string text)
70  {
71  if (_debug != null)
72  {
73  _debug(DateTime.Now.ToString(DateFormat, CultureInfo.InvariantCulture) + " DEBUG " + text);
74  }
75  }
76 
77  /// <summary>
78  /// Write debug message to log
79  /// </summary>
80  /// <param name="text">The trace text to log</param>
81  public void Trace(string text)
82  {
83  if (_trace != null)
84  {
85  _trace(DateTime.Now.ToString(DateFormat, CultureInfo.InvariantCulture) + " TRACE " + text);
86  }
87  }
88 
89  /// <summary>
90  /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
91  /// </summary>
92  /// <filterpriority>2</filterpriority>
93  public void Dispose()
94  {
95  }
96  }
97 }