Skip to content

Commit

Permalink
Fixed Logger.Write creating new lines in LogFile (#223)
Browse files Browse the repository at this point in the history
* Fixed Logger.Write creating new lines in LogFile. Fixed DirectoryNotFound in Setup

* Removed changes from Logger. Moved to LogWriter

* Removed unused reference at top

* Added space after if statement
  • Loading branch information
gurrenm3 committed Apr 23, 2023
1 parent d4b3aa1 commit e1d1a1d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
1 change: 1 addition & 0 deletions source/Reloaded.Mod.Launcher.Lib/Setup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ private static void HandleLogsAndCrashdumps()
logCompressor.DeleteOldFiles(TimeSpan.FromHours(loaderConfig.LogFileDeleteHours));

// Crashdumps (delete)
Directory.CreateDirectory(Paths.CrashDumpPath);
var dumpFolders = Directory.GetDirectories(Paths.CrashDumpPath);
var now = DateTime.UtcNow;
foreach (var folder in dumpFolders)
Expand Down
27 changes: 23 additions & 4 deletions source/Reloaded.Mod.Loader/Logging/LogWriter.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Text;
using Environment = System.Environment;

namespace Reloaded.Mod.Loader.Logging;
Expand All @@ -18,6 +19,7 @@ public class LogWriter : IDisposable
private StreamWriter _textStream;
private Logger _logger;
private List<string> _logItems;
private StringBuilder _writeCache;

/// <summary>
/// Intercepts logs from the console and provides the ability to flush them to a file in the event of a crash.
Expand All @@ -32,7 +34,8 @@ public LogWriter(Logger logger, string outputDir)

_logItems = new List<string>(MaxBufferLength + 1);
_logger = logger;
_logger.OnPrintMessage += OnPrintMessage;
_logger.OnWrite += OnWrite;
_logger.OnWriteLine += OnWriteLine;

// Add integer to file path in case there is another instance of same process launched at the same time.
string GetLogFilePath()
Expand All @@ -57,6 +60,7 @@ string GetLogFilePath()
_textStream = File.CreateText(FlushPath);
_textStream.AutoFlush = false;
_autoFlushThread = new Timer(AutoFlush, null, TimeSpan.FromMilliseconds(0), TimeSpan.FromMilliseconds(250));
_writeCache = new();
}

/// <summary>
Expand All @@ -82,18 +86,33 @@ public void Flush()
}

[MethodImpl(MethodImplOptions.Synchronized)]
private void OnPrintMessage(object sender, string message)
private void OnWrite(object sender, (string text, Color color) e)
{
_writeCache.Append(e.text);
}

[MethodImpl(MethodImplOptions.Synchronized)]
private void OnWriteLine(object sender, (string text, Color color) e)
{
if (_logItems.Count == MaxBufferLength)
Flush();

_logItems.Add($"[{DateTime.Now:HH:mm:ss}] {message}");
if (_writeCache.Length == 0)
{
_logItems.Add($"[{DateTime.Now:HH:mm:ss}] {e.text}");
}
else
{
_logItems.Add($"[{DateTime.Now:HH:mm:ss}] {$"{_writeCache}{e.text}"}");
_writeCache.Clear();
}
}

/// <inheritdoc />
public void Dispose()
{
_logger.OnPrintMessage -= OnPrintMessage;
_logger.OnWrite -= OnWrite;
_logger.OnWriteLine -= OnWriteLine;
_autoFlushThread.Dispose();
_textStream.Dispose();
}
Expand Down

0 comments on commit e1d1a1d

Please sign in to comment.