Skip to content

Commit

Permalink
Fix old log clearing
Browse files Browse the repository at this point in the history
  • Loading branch information
Banane9 committed Aug 26, 2024
1 parent edc64f5 commit 7cdb58d
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 27 deletions.
36 changes: 14 additions & 22 deletions MonkeyLoader/Entrypoint.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using MonkeyLoader;
using MonkeyLoader.Logging;
using System;
using System.Diagnostics;
using System.IO;
Expand All @@ -13,34 +12,27 @@ internal static class Entrypoint
{
public static void Start()
{
var log = new FileLoggingHandler("MonkeyLoader/MonkeyLog.log");
var loader = new MonkeyLoader.MonkeyLoader();
var log = loader.Logger;

foreach (var file in Directory.EnumerateFiles("./"))
try
{
try
{
if (Path.GetFileName(file).StartsWith("doorstop", StringComparison.OrdinalIgnoreCase)
&& Path.GetExtension(file).Equals(".log", StringComparison.OrdinalIgnoreCase))
File.Delete(file);
}
catch
foreach (var file in Directory.EnumerateFiles("./"))
{
log.Warn(() => $"Failed to delete doorstop logfile - probably the active one: {file}");
try
{
if (Path.GetFileName(file).StartsWith("doorstop", StringComparison.OrdinalIgnoreCase)
&& Path.GetExtension(file).Equals(".log", StringComparison.OrdinalIgnoreCase))
File.Delete(file);
}
catch
{
log.Warn(() => $"Failed to delete doorstop logfile - probably the active one: {file}");
}
}
}

try
{
AppDomain.CurrentDomain.UnhandledException += (sender, e) => log.Fatal(() => (e.ExceptionObject as Exception)?.Format("Unhandled Exception!") ?? "Unhandled Exception!");

var loggingController = new LoggingController("MonkeyLoader")
{
Level = LoggingLevel.Trace,
Handler = log
};

var loader = new MonkeyLoader.MonkeyLoader(loggingController: loggingController);

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

var type = Type.GetType("Mono.Runtime");
Expand Down
17 changes: 12 additions & 5 deletions MonkeyLoader/Logging/LoggingConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ public sealed class LoggingConfig : ConfigSection
public const string FileExtension = ".log";
public const string FileNamePrefix = $"MonkeyLog_";
public const string FileSearchPattern = "*" + FileExtension;
public const string TimestampFormat = "yyyy-MM-ddTHH\\:mm\\:ss";
public const string TimestampFormat = "yyyy-MM-ddTHH-mm-ss";

public readonly DefiningConfigKey<string?> DirectoryPathKey = new("DirectoryPath", "The directory to write log files to. Changes will only take effect on restart.", () => "./MonkeyLoader/Logs");
public readonly DefiningConfigKey<int> FilesToPreserveKey = new("FilesToPreserve", "The number of recent log files to keep around. Set <1 to disable. Changes take effect on restart.", () => 16);
public readonly DefiningConfigKey<LoggingLevel> LevelKey = new("Level", "The logging level used to filter logging requests. Changes take effect immediately.", () => LoggingLevel.Info);
public readonly DefiningConfigKey<LoggingLevel> LevelKey = new("Level", "The logging level used to filter logging requests. May be ignored in the initial startup phase. Changes take effect immediately.", () => LoggingLevel.Info);
private readonly Lazy<string?> _currentLogFilePath;
private LoggingController _loggingController;

Expand Down Expand Up @@ -71,13 +71,20 @@ public LoggingConfig()

public static bool TryGetTimestamp(string logFile, [NotNullWhen(true)] out DateTime? timestamp)
{
if (DateTime.TryParseExact(Path.GetFileNameWithoutExtension(logFile), $"{FileNamePrefix}{TimestampFormat}", CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal, out var result))
timestamp = default;
var fileName = Path.GetFileNameWithoutExtension(logFile);

if (!fileName.StartsWith(FileNamePrefix))
return false;

var timestampStr = fileName[FileNamePrefix.Length..];

if (DateTime.TryParseExact(timestampStr, TimestampFormat, CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal, out var result))
{
timestamp = result;
return true;
}

timestamp = default;
return false;
}

Expand All @@ -90,7 +97,7 @@ private void CleanLogDirectory()

try
{
var logFilesToDelete = Directory.EnumerateFiles(CurrentLogFilePath, FileSearchPattern)
var logFilesToDelete = Directory.EnumerateFiles(DirectoryPath, FileSearchPattern)
.Select(logFile => (File: logFile, Success: TryGetTimestamp(logFile, out var timestamp), Created: timestamp))
.Where(logFile => logFile.Success)
.OrderByDescending(logFile => logFile.Created)
Expand Down

0 comments on commit 7cdb58d

Please sign in to comment.