Skip to content

Commit

Permalink
Revamp logging handling - moving a lot to a new LoggingController
Browse files Browse the repository at this point in the history
  • Loading branch information
Banane9 committed Mar 24, 2024
1 parent c27245f commit 7e9c321
Show file tree
Hide file tree
Showing 18 changed files with 327 additions and 222 deletions.
4 changes: 2 additions & 2 deletions MonkeyLoader.ConsoleTest/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ private static void Main(string[] args)
//}

var loader = new MonkeyLoader();
loader.LoggingLevel = LoggingLevel.Trace;
loader.LoggingHandler = ConsoleLoggingHandler.Instance;
loader.LoggingController.Level = LoggingLevel.Trace;
loader.LoggingController.Handler = ConsoleLoggingHandler.Instance;

loader.FullLoad();

Expand Down
8 changes: 4 additions & 4 deletions MonkeyLoader/AssemblyPool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public sealed class AssemblyPool : IAssemblyResolver
private readonly HashSet<string> _directories = new(StringComparer.OrdinalIgnoreCase);
private readonly HashSet<AssemblyPool> _fallbackPools = new();
private readonly Func<string?>? _getPatchedAssemblyPath;
private readonly MonkeyLogger _logger;
private readonly Logger _logger;

public MonkeyLoader Loader { get; }

Expand All @@ -44,7 +44,7 @@ public sealed class AssemblyPool : IAssemblyResolver
public AssemblyPool(MonkeyLoader loader, string poolName = "AssemblyPool", Func<string?>? getPatchedAssemblyPath = null, bool loadForResolve = true)
{
Loader = loader;
_logger = new MonkeyLogger(loader.Logger, poolName);
_logger = new Logger(loader.Logger, poolName);
_getPatchedAssemblyPath = getPatchedAssemblyPath;
LoadForResolve = loadForResolve;

Expand Down Expand Up @@ -351,7 +351,7 @@ public IEnumerable<AssemblyName> GetDependencies(HashSet<AssemblyName> alreadyLo

public AssemblyDefinition GetResolveDefinition() => _definition;

public Assembly LoadAssembly(MonkeyLogger logger, string? patchedAssemblyPath)
public Assembly LoadAssembly(Logger logger, string? patchedAssemblyPath)
{
var saveAssemblies = true;
if (string.IsNullOrWhiteSpace(patchedAssemblyPath) || !Directory.Exists(patchedAssemblyPath))
Expand Down Expand Up @@ -432,7 +432,7 @@ public AssemblyDefinition WaitForDefinition()
return _definition;
}

internal string? SaveAssembly(string path, MonkeyLogger logger)
internal string? SaveAssembly(string path, Logger logger)
{
var targetPath = Path.Combine(path, $"{Name}.dll");

Expand Down
4 changes: 2 additions & 2 deletions MonkeyLoader/Configuration/Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public ISet<IDefiningConfigKey> ConfigurationItemDefinitions
/// <summary>
/// Gets the logger used by this config.
/// </summary>
public MonkeyLogger Logger { get; }
public Logger Logger { get; }

/// <summary>
/// Gets the mod that owns this config.
Expand Down Expand Up @@ -70,7 +70,7 @@ public object? this[IConfigKey key]
internal Config(IConfigOwner owner)
{
Owner = owner;
Logger = new MonkeyLogger(owner.Logger, "Config");
Logger = new Logger(owner.Logger, "Config");

_loadedConfig = LoadConfig();
if (_loadedConfig[OwnerKey]?.ToObject<string>() != Owner.Id)
Expand Down
2 changes: 1 addition & 1 deletion MonkeyLoader/Configuration/DefiningConfigKey.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ ConfigSection IDefiningConfigKeyInternal.Section
/// <summary>
/// Gets the logger of the config this item belongs to if it's a <see cref="IsDefiningKey">defining key</see>.
/// </summary>
protected MonkeyLogger Logger => Section.Config.Logger;
protected Logger Logger => Section.Config.Logger;

/// <summary>
/// Gets the config section this item belongs to.
Expand Down
5 changes: 2 additions & 3 deletions MonkeyLoader/Configuration/IConfigOwner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,8 @@ public interface IConfigOwner
/// Gets the logger to be used by this owner.
/// </summary>
/// <remarks>
/// Every owner instance has its own logger and can thus have a different <see cref="LoggingLevel"/>.<br/>
/// They do all share the <see cref="Loader">Loader's</see> <see cref="MonkeyLoader.LoggingHandler">LoggingHandler</see> though.
/// All loggers share the <see cref="Loader">Loader</see>'s <see cref="MonkeyLoader.LoggingController">LoggingController</see>.
/// </remarks>
public MonkeyLogger Logger { get; }
public Logger Logger { get; }
}
}
7 changes: 5 additions & 2 deletions MonkeyLoader/Entrypoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,11 @@ public static void Start()
{
AppDomain.CurrentDomain.UnhandledException += (sender, e) => log.Fatal(() => (e.ExceptionObject as Exception)?.Format("Unhandled Exception!") ?? "Unhandled Exception!");

var loader = new MonkeyLoader.MonkeyLoader(loggingLevel: LoggingLevel.Trace);
loader.LoggingHandler += log;
var loader = new MonkeyLoader.MonkeyLoader();
loader.LoggingController.Level = LoggingLevel.Trace;
loader.LoggingController.Handler += log;

AppDomain.CurrentDomain.ProcessExit += (_, _) => loader.Shutdown();

var type = Type.GetType("Mono.Runtime");
if (type != null)
Expand Down
21 changes: 3 additions & 18 deletions MonkeyLoader/Logging/FileLoggingHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ namespace MonkeyLoader.Logging
/// </summary>
public sealed class FileLoggingHandler : LoggingHandler, IDisposable
{
private readonly int _flushTimeout;
private readonly Timer _flushTimer;
private readonly StreamWriter _streamWriter;

/// <inheritdoc/>
Expand All @@ -24,21 +22,18 @@ public sealed class FileLoggingHandler : LoggingHandler, IDisposable
/// Creates a new file logging handler with the file at the given path as the target.
/// </summary>
/// <param name="path">The file to write to.</param>
public FileLoggingHandler(string path) : this(new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Read), 10000)
public FileLoggingHandler(string path)
: this(new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Read))
{ }

/// <summary>
/// Creates a new file logging handler with the given <see cref="FileStream"/> as the target.
/// </summary>
/// <param name="fileStream">The file to write to.</param>
/// <param name="flushTimeout">The time in ms to wait for more logging before flushing after non-critical messages.</param>
public FileLoggingHandler(FileStream fileStream, int flushTimeout)
public FileLoggingHandler(FileStream fileStream)
{
fileStream.SetLength(0);
_streamWriter = new StreamWriter(fileStream);

_flushTimeout = flushTimeout;
_flushTimer = new Timer(Flush, null, Timeout.Infinite, flushTimeout);
}

/// <inheritdoc/>
Expand All @@ -49,8 +44,6 @@ public void Dispose()
{
_streamWriter.Flush();
_streamWriter.Dispose();

_flushTimer.Dispose();
}

/// <inheritdoc/>
Expand All @@ -63,10 +56,7 @@ public void Dispose()
public override void Flush()
{
lock (_streamWriter)
{
_streamWriter.Flush();
_flushTimer.Change(Timeout.Infinite, _flushTimeout);
}
}

/// <inheritdoc/>
Expand All @@ -79,18 +69,13 @@ public override void Flush()
public void Log(string message)
{
lock (_streamWriter)
{
_streamWriter.WriteLine($"[{DateTime.Now:HH:mm:ss.ffff}] {message}");
_flushTimer.Change(0, _flushTimeout);
}
}

/// <inheritdoc/>
public override void Trace(Func<object> messageProducer) => Log(messageProducer().ToString());

/// <inheritdoc/>
public override void Warn(Func<object> messageProducer) => Log(messageProducer().ToString());

private void Flush(object state) => Flush();
}
}
Loading

0 comments on commit 7e9c321

Please sign in to comment.