Skip to content

Commit

Permalink
Use a logging optimized StreamWriter
Browse files Browse the repository at this point in the history
  • Loading branch information
CptMoore committed May 18, 2024
1 parent c8f78e9 commit c64b839
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 20 deletions.
12 changes: 12 additions & 0 deletions ModTek.Common/Utils/FileUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using ModTek.Common.Globals;

namespace ModTek.Common.Utils;
Expand Down Expand Up @@ -190,4 +191,15 @@ internal static void SetupCleanDirectory(string path, bool recursive = false)
di.Create();
}
}

public static StreamWriter LogStream(string path)
{
return new StreamWriter(
File.Open(path, FileMode.Append, FileAccess.Write, FileShare.ReadWrite|FileShare.Delete),
Encoding.UTF8,
32 * 1024
) {
AutoFlush = true
};
}
}
2 changes: 1 addition & 1 deletion ModTek/Features/Logging/AppenderFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ internal AppenderFile(string path, AppenderSettings settings)

FileUtils.CreateParentOfPath(path);
FileUtils.RotatePath(path, settings.LogRotationCount);
_writer = new StreamWriter(path) { AutoFlush = true };
_writer = FileUtils.LogStream(path);
_writer.WriteLine($"ModTek v{GitVersionInformation.InformationalVersion} ({GitVersionInformation.CommitDate})");
_writer.WriteLine(DateTimeOffset.Now.ToString("o", CultureInfo.InvariantCulture));
_writer.WriteLine(new string('-', 80));
Expand Down
3 changes: 2 additions & 1 deletion ModTek/Features/Logging/HarmonyXLoggerAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ namespace ModTek.Features.Logging;

internal static class HarmonyXLoggerAdapter
{
private static readonly HBS.Logging.Logger.LogImpl s_logImpl = Log.HarmonyX.Log;
internal static void Setup()
{
Logger.MessageReceived += (_, args) =>
{
var level = MapHarmonyLogChannelToHbsLogLevel(args.LogChannel);
Log.HarmonyX.Log.LogAtLevel(level, args.Message);
s_logImpl.LogAtLevel(level, args.Message);
};
}

Expand Down
1 change: 0 additions & 1 deletion ModTekPreloader/Harmony12X/ShimInjectorPatches.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ internal static void Register(DynamicShimInjector shimInjector)
{
HarmonyLib.Tools.Logger.ChannelFilter = (HarmonyLib.Tools.Logger.LogChannel)filter;
var logger = new Logger(Paths.HarmonyLogFile);
logger.Rotate();
HarmonyLib.Tools.Logger.MessageReceived += (_, args) =>
{
logger.Log($"[{args.LogChannel}] {args.Message}");
Expand Down
2 changes: 0 additions & 2 deletions ModTekPreloader/Loader/Preloader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ internal static class Preloader
{
internal static void Run()
{
Logger.Main.Rotate();

Logger.Main.Log($"Preloader v{GitVersionInformation.InformationalVersion} ({GitVersionInformation.CommitDate})");
PrintPaths();
SingleInstanceEnforcer.Enforce();
Expand Down
36 changes: 21 additions & 15 deletions ModTekPreloader/Logging/Logger.cs
Original file line number Diff line number Diff line change
@@ -1,35 +1,41 @@
using System;
using System.Globalization;
using System.IO;
using System.Runtime.CompilerServices;
using ModTek.Common.Utils;
using ModTekPreloader.Injector;

namespace ModTekPreloader.Logging;

// thread safe but not async
internal class Logger
{
internal static readonly Logger Main = new(Paths.LogFile)
{
Prefix = AppDomain.CurrentDomain.FriendlyName == InjectorsAppDomain.ModTekInjectorsDomainName ? " [Injectors]" : ""
};
internal static readonly Logger Main = new(Paths.LogFile);

internal string Prefix { get; set; }
private readonly string _path;
private readonly string _prefix;
private readonly StreamWriter _writer;
internal Logger(string path)
{
_path = path;
}

internal void Rotate()
{
FileUtils.CreateDirectoryForFile(_path);
FileUtils.RotatePath(_path, 1);
File.WriteAllText(_path, "");
FileUtils.CreateDirectoryForFile(path);
if (AppDomain.CurrentDomain.FriendlyName == InjectorsAppDomain.ModTekInjectorsDomainName)
{
// TODO move injector code in own project/library and use own log
_prefix = " [Injectors]";
}
else
{
FileUtils.RotatePath(path, 1);
}
_writer = FileUtils.LogStream(path);
}

[MethodImpl(MethodImplOptions.Synchronized)]
internal void Log(object obj)
{
File.AppendAllText(_path, $"{GetTime()}{Prefix} {obj}{Environment.NewLine}");
lock (this)
{
_writer.WriteLine($"{GetTime()}{_prefix} {obj}");
}
}

private static string GetTime()
Expand Down

0 comments on commit c64b839

Please sign in to comment.