From 428496bdad403ae79caee29f54f2969ff7825456 Mon Sep 17 00:00:00 2001 From: Somebody Date: Sat, 17 May 2025 15:44:45 +0800 Subject: [PATCH] feat: pack the old log files when launching --- Common/Util/Logger.cs | 48 +++++++++++++++++++++++++---------- Program/Program/EntryPoint.cs | 43 +++++++++++++++++++++++++++++++ 2 files changed, 78 insertions(+), 13 deletions(-) diff --git a/Common/Util/Logger.cs b/Common/Util/Logger.cs index 18a1fd9c..72406132 100644 --- a/Common/Util/Logger.cs +++ b/Common/Util/Logger.cs @@ -5,19 +5,27 @@ namespace EggLink.DanhengServer.Util; public class Logger(string moduleName) { - private static FileInfo? LogFile; - private static readonly object _lock = new(); - private readonly string ModuleName = moduleName; + private static FileInfo? _logFile; + private static FileInfo? _debugLogFile; + private static readonly object Lock = new(); public void Log(string message, LoggerLevel level) { - lock (_lock) + lock (Lock) { AnsiConsole.Write(new Markup($"[[[bold deepskyblue3_1]{DateTime.Now:HH:mm:ss}[/]]] " + - $"[[[gray]{ModuleName}[/]]] [[[{(ConsoleColor)level}]{level}[/]]] {message.Replace("[", "[[").Replace("]", "]]")}\n")); + $"[[[gray]{moduleName}[/]]] [[[{(ConsoleColor)level}]{level}[/]]] {message.Replace("[", "[[").Replace("]", "]]")}\n")); - var logMessage = $"[{DateTime.Now:HH:mm:ss}] [{ModuleName}] [{level}] {message}"; + var logMessage = $"[{DateTime.Now:HH:mm:ss}] [{moduleName}] [{level}] {message}"; PluginEventCommon.InvokeOnConsoleLog(logMessage); + + if (level == LoggerLevel.DEBUG) + { + WriteToDebugFile(logMessage); + return; + } + + WriteToDebugFile(logMessage); WriteToFile(logMessage); } } @@ -74,15 +82,33 @@ public class Logger(string moduleName) public static void SetLogFile(FileInfo file) { - LogFile = file; + _logFile = file; + } + + public static void SetDebugLogFile(FileInfo file) + { + _debugLogFile = file; } public static void WriteToFile(string message) { try { - if (LogFile == null) throw new Exception("LogFile is not set"); - using var sw = LogFile.AppendText(); + if (_logFile == null) throw new Exception("LogFile is not set"); + using var sw = _logFile.AppendText(); + sw.WriteLine(message); + } + catch + { + } + } + + public static void WriteToDebugFile(string message) + { + try + { + if (_debugLogFile == null) throw new Exception("DebugLogFile is not set"); + using var sw = _debugLogFile.AppendText(); sw.WriteLine(message); } catch @@ -103,8 +129,4 @@ public enum LoggerLevel ERROR = ConsoleColor.Red, FATAL = ConsoleColor.DarkRed, DEBUG = ConsoleColor.Blue -} - -public class LoggerLevelHelper -{ } \ No newline at end of file diff --git a/Program/Program/EntryPoint.cs b/Program/Program/EntryPoint.cs index 0779fb82..a3631097 100644 --- a/Program/Program/EntryPoint.cs +++ b/Program/Program/EntryPoint.cs @@ -1,4 +1,5 @@ using System.Globalization; +using System.IO.Compression; using EggLink.DanhengServer.Command.Command; using EggLink.DanhengServer.Configuration; using EggLink.DanhengServer.Data; @@ -50,18 +51,60 @@ public class EntryPoint Environment.Exit(0); }; var time = DateTime.Now; + + // pack the old log + var logDirectory = new DirectoryInfo(GetConfig().Path.LogPath); + if (logDirectory.Exists) + { + List packed = []; + foreach (var oldFile in logDirectory.GetFiles().ToArray()) + { + if (!oldFile.Name.EndsWith(".log")) continue; + if (oldFile.Name.EndsWith("-debug.log")) continue; + if (packed.Contains(oldFile.Name)) continue; + + var fileName = oldFile.Name.Replace(".log", ""); + var debugFileName = fileName + "-debug"; + var oldDebugFile = logDirectory.GetFiles(debugFileName + ".log").FirstOrDefault(); + + if (oldFile.Exists) + { + var zipFileName = fileName + ".zip"; + var zipFile = new FileInfo(GetConfig().Path.LogPath + $"/{zipFileName}"); + if (zipFile.Exists) zipFile.Delete(); + using (var zip = ZipFile.Open(zipFile.FullName, ZipArchiveMode.Create)) + { + zip.CreateEntryFromFile(oldFile.FullName, oldFile.Name); + if (oldDebugFile is { Exists: true }) + zip.CreateEntryFromFile(oldDebugFile.FullName, oldDebugFile.Name); + } + + oldFile.Delete(); + oldDebugFile?.Delete(); + packed.Add(oldFile.Name); + packed.Add(oldDebugFile?.Name ?? ""); + } + } + } + // Initialize the logfile var counter = 0; FileInfo file; + FileInfo zi; while (true) { file = new FileInfo(GetConfig().Path.LogPath + $"/{DateTime.Now:yyyy-MM-dd}-{++counter}.log"); + zi = new FileInfo(GetConfig().Path.LogPath + $"/{DateTime.Now:yyyy-MM-dd}-{counter}.zip"); if (file is not { Exists: false, Directory: not null }) continue; + if (zi is not { Exists: false, Directory: not null }) continue; file.Directory.Create(); break; } + var debugFile = new FileInfo(GetConfig().Path.LogPath + $"/{DateTime.Now:yyyy-MM-dd}-{counter}-debug.log"); + Logger.SetLogFile(file); + Logger.SetDebugLogFile(debugFile); // Starting the server Logger.Info(I18NManager.Translate("Server.ServerInfo.StartingServer"));