Skip to content

Commit

Permalink
Minor logging improvements (QuantConnect#5277)
Browse files Browse the repository at this point in the history
* Minor logging improvements

- Remove Thread.Sleep() call when logging debug
- FileLogHandler will capture lock after generating message to log
- ConsoleLogHandler will log time as UTC, cheaper than converting time
  zones

* Fix typo

Fix typo
  • Loading branch information
Martin-Molinero authored Feb 9, 2021
1 parent 17dbade commit a1cbe13
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 10 deletions.
6 changes: 3 additions & 3 deletions Logging/ConsoleLogHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public virtual void Error(string text)
#if DEBUG
Console.ForegroundColor = ConsoleColor.Red;
#endif
_error.WriteLine(DateTime.Now.ToString(_dateFormat, CultureInfo.InvariantCulture) + " ERROR:: " + text);
_error.WriteLine($"{DateTime.UtcNow.ToString(_dateFormat, CultureInfo.InvariantCulture)} ERROR:: {text}");
#if DEBUG
Console.ResetColor();
#endif
Expand All @@ -71,7 +71,7 @@ public virtual void Error(string text)
/// <param name="text">The debug text to log</param>
public virtual void Debug(string text)
{
_trace.WriteLine(DateTime.Now.ToString(_dateFormat, CultureInfo.InvariantCulture) + " DEBUG:: " + text);
_trace.WriteLine($"{DateTime.UtcNow.ToString(_dateFormat, CultureInfo.InvariantCulture)} DEBUG:: {text}");
}

/// <summary>
Expand All @@ -80,7 +80,7 @@ public virtual void Debug(string text)
/// <param name="text">The trace text to log</param>
public virtual void Trace(string text)
{
_trace.WriteLine(DateTime.Now.ToString(_dateFormat, CultureInfo.InvariantCulture) + " Trace:: " + text);
_trace.WriteLine($"{DateTime.UtcNow.ToString(_dateFormat, CultureInfo.InvariantCulture)} TRACE:: {text}");
}

/// <summary>
Expand Down
3 changes: 2 additions & 1 deletion Logging/FileLogHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,11 @@ protected virtual string CreateMessage(string text, string level)
/// </summary>
private void WriteMessage(string text, string level)
{
var message = CreateMessage(text, level);
lock (_lock)
{
if (_disposed) return;
_writer.Value.WriteLine(CreateMessage(text, level));
_writer.Value.WriteLine(message);
_writer.Value.Flush();
}
}
Expand Down
9 changes: 3 additions & 6 deletions Logging/Log.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
using System.Globalization;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading;

namespace QuantConnect.Logging
{
Expand Down Expand Up @@ -144,18 +143,16 @@ public static void Error(string format, params object[] args)
}

/// <summary>
/// Output to the console, and sleep the thread for a little period to monitor the results.
/// Output to the console
/// </summary>
/// <param name="text"></param>
/// <param name="text">The message to show</param>
/// <param name="level">debug level</param>
/// <param name="delay"></param>
public static void Debug(string text, int level = 1, int delay = 0)
public static void Debug(string text, int level = 1)
{
try
{
if (!_debuggingEnabled || level < _level) return;
_logHandler.Debug(text);
Thread.Sleep(delay);
}
catch (Exception err)
{
Expand Down

0 comments on commit a1cbe13

Please sign in to comment.