Lean  $LEAN_TAG$
FuncTextWriter.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 
17 using System;
18 using System.IO;
19 using System.Text;
20 
21 namespace QuantConnect.Util
22 {
23  /// <summary>
24  /// Provides an implementation of <see cref="TextWriter"/> that redirects Write(string) and WriteLine(string)
25  /// </summary>
26  public class FuncTextWriter : TextWriter
27  {
28  private readonly Action<string> _writer;
29 
30  /// <inheritdoc />
31  public override Encoding Encoding
32  {
33  get { return Encoding.Default; }
34  }
35 
36  /// <summary>
37  /// Initializes a new instance of the <see cref="FuncTextWriter"/> that will direct
38  /// messages to the algorithm's Debug function.
39  /// </summary>
40  /// <param name="writer">The algorithm hosting the Debug function where messages will be directed</param>
41  public FuncTextWriter(Action<string> writer)
42  {
43  _writer = writer;
44  }
45 
46  /// <summary>
47  /// Writes the string value using the delegate provided at construction
48  /// </summary>
49  /// <param name="value">The string value to be written</param>
50  public override void Write(string value)
51  {
52  _writer(value);
53  }
54 
55  /// <summary>
56  /// Writes the string value using the delegate provided at construction
57  /// </summary>
58  /// <param name="value"></param>
59  public override void WriteLine(string value)
60  {
61  // these are grouped in a list so we don't need to add new line characters here
62  _writer(value);
63  }
64  }
65 }