From 4f66631d4e9d5eae284dc11c65911326f043d0cf Mon Sep 17 00:00:00 2001 From: Anairkoen Schno Date: Tue, 31 Jul 2018 20:37:07 -0500 Subject: [PATCH] Updated logger to write to GZ files in all cases and create a latest.gz --- IllusionInjector/IllusionInjector.csproj | 3 +- .../Logging/Printers/GZFilePrinter.cs | 92 +++++++++++++++++++ .../Logging/Printers/GlobalLogFilePrinter.cs | 30 ++++++ .../Logging/Printers/GlobalZFIlePrinter.cs | 64 ------------- .../Logging/Printers/PluginLogFilePrinter.cs | 29 ++---- IllusionInjector/Logging/StandardLogger.cs | 2 +- ...ionInjector.csproj.CoreCompileInputs.cache | 2 +- 7 files changed, 134 insertions(+), 88 deletions(-) create mode 100644 IllusionInjector/Logging/Printers/GZFilePrinter.cs create mode 100644 IllusionInjector/Logging/Printers/GlobalLogFilePrinter.cs delete mode 100644 IllusionInjector/Logging/Printers/GlobalZFIlePrinter.cs diff --git a/IllusionInjector/IllusionInjector.csproj b/IllusionInjector/IllusionInjector.csproj index dae9245d..9fae63a3 100644 --- a/IllusionInjector/IllusionInjector.csproj +++ b/IllusionInjector/IllusionInjector.csproj @@ -56,7 +56,8 @@ - + + diff --git a/IllusionInjector/Logging/Printers/GZFilePrinter.cs b/IllusionInjector/Logging/Printers/GZFilePrinter.cs new file mode 100644 index 00000000..8156ff92 --- /dev/null +++ b/IllusionInjector/Logging/Printers/GZFilePrinter.cs @@ -0,0 +1,92 @@ +using IllusionPlugin.Logging; +using Ionic.Zlib; +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Runtime.InteropServices; +using System.Text; +using System.Threading.Tasks; + +namespace IllusionInjector.Logging.Printers +{ + public abstract class GZFilePrinter : LogPrinter + { + [DllImport("Kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)] + static extern bool CreateHardLink( + string lpFileName, + string lpExistingFileName, + IntPtr lpSecurityAttributes + ); + + [DllImport("Kernel32.dll")] + static extern Int32 GetLastError(); + + private FileInfo fileInfo; + protected StreamWriter fileWriter; + private GZipStream zstream; + private FileStream fstream; + + protected abstract FileInfo GetFileInfo(); + + private void InitLog() + { + try + { + if (fileInfo == null) + { // first init + fileInfo = GetFileInfo(); + var ext = fileInfo.Extension; + fileInfo = new FileInfo(fileInfo.FullName + ".gz"); + fileInfo.Create().Close(); + + var symlink = new FileInfo(Path.Combine(fileInfo.DirectoryName, $"latest{ext}.gz")); + if (symlink.Exists) symlink.Delete(); + + try + { + if (!CreateHardLink(symlink.FullName, fileInfo.FullName, IntPtr.Zero)) + { + Logger.log.Error($"Hardlink creation failed {GetLastError()}"); + } + } + catch (Exception e) + { + Logger.log.Error("Error creating latest hardlink!"); + Logger.log.Error(e); + } + } + } + catch (Exception e) + { + Logger.log.Error("Error initializing log!"); + Logger.log.Error(e); + } + } + + public override sealed void StartPrint() + { + InitLog(); + + fstream = fileInfo.Open(FileMode.Append, FileAccess.Write); + zstream = new GZipStream(fstream, CompressionMode.Compress) + { + FlushMode = FlushType.Full + }; + fileWriter = new StreamWriter(zstream, Encoding.UTF8); + } + + public override sealed void EndPrint() + { + fileWriter.Flush(); + zstream.Flush(); + fstream.Flush(); + fileWriter.Close(); + zstream.Close(); + fstream.Close(); + fileWriter.Dispose(); + zstream.Dispose(); + fstream.Dispose(); + } + } +} diff --git a/IllusionInjector/Logging/Printers/GlobalLogFilePrinter.cs b/IllusionInjector/Logging/Printers/GlobalLogFilePrinter.cs new file mode 100644 index 00000000..9f617b72 --- /dev/null +++ b/IllusionInjector/Logging/Printers/GlobalLogFilePrinter.cs @@ -0,0 +1,30 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using IllusionPlugin.Logging; +using LoggerBase = IllusionPlugin.Logging.Logger; + +namespace IllusionInjector.Logging.Printers +{ + class GlobalLogFilePrinter : GZFilePrinter + { + public override LoggerBase.LogLevel Filter { get; set; } = LoggerBase.LogLevel.All; + + public override void Print(IllusionPlugin.Logging.Logger.Level level, DateTime time, string logName, string message) + { + foreach (var line in message.Split(new string[] { "\n", Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries)) + fileWriter.WriteLine(string.Format(LoggerBase.LogFormat, line, logName, time, level.ToString().ToUpper())); + } + + protected override FileInfo GetFileInfo() + { + var logsDir = new DirectoryInfo("Logs"); + logsDir.Create(); + var finfo = new FileInfo(Path.Combine(logsDir.FullName, $"{DateTime.Now:yyyy.MM.dd.HH.mm}.log")); + return finfo; + } + } +} diff --git a/IllusionInjector/Logging/Printers/GlobalZFIlePrinter.cs b/IllusionInjector/Logging/Printers/GlobalZFIlePrinter.cs deleted file mode 100644 index 5d8f8b7d..00000000 --- a/IllusionInjector/Logging/Printers/GlobalZFIlePrinter.cs +++ /dev/null @@ -1,64 +0,0 @@ -using IllusionPlugin.Logging; -using Ionic.Zlib; -using System; -using System.Collections.Generic; -using System.IO; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace IllusionInjector.Logging.Printers -{ - public class GlobalZFilePrinter : LogPrinter - { - public override IllusionPlugin.Logging.Logger.LogLevel Filter { get; set; } = IllusionPlugin.Logging.Logger.LogLevel.All; - - private FileInfo fileInfo; - private StreamWriter fileWriter; - private GZipStream zstream; - private FileStream fstream; - - private static FileInfo GetFileInfo() - { - var logsDir = new DirectoryInfo("Logs"); - logsDir.Create(); - var finfo = new FileInfo(Path.Combine(logsDir.FullName, $"{DateTime.Now:yyyy.MM.dd.HH.MM}.log.z")); - finfo.Create().Close(); - return finfo; - } - - public GlobalZFilePrinter() - { - fileInfo = GetFileInfo(); - } - - public override void StartPrint() - { - fstream = fileInfo.Open(FileMode.Append, FileAccess.Write); - zstream = new GZipStream(fstream, CompressionMode.Compress) - { - FlushMode = FlushType.Full - }; - fileWriter = new StreamWriter(zstream, Encoding.UTF8); - } - - public override void Print(IllusionPlugin.Logging.Logger.Level level, DateTime time, string logName, string message) - { - foreach (var line in message.Split(new string[] { "\n", Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries)) - fileWriter.WriteLine(string.Format("[{3} @ {2:HH:mm:ss} | {1}] {0}", line, logName, time, level.ToString().ToUpper())); - } - - public override void EndPrint() - { - fileWriter.Flush(); - zstream.Flush(); - fstream.Flush(); - fileWriter.Close(); - zstream.Close(); - fstream.Close(); - fileWriter.Dispose(); - zstream.Dispose(); - fstream.Dispose(); - } - } -} diff --git a/IllusionInjector/Logging/Printers/PluginLogFilePrinter.cs b/IllusionInjector/Logging/Printers/PluginLogFilePrinter.cs index de573470..e311a718 100644 --- a/IllusionInjector/Logging/Printers/PluginLogFilePrinter.cs +++ b/IllusionInjector/Logging/Printers/PluginLogFilePrinter.cs @@ -5,33 +5,27 @@ using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; +using LoggerBase = IllusionPlugin.Logging.Logger; namespace IllusionInjector.Logging.Printers { - public class PluginLogFilePrinter : LogPrinter + public class PluginLogFilePrinter : GZFilePrinter { - public override IllusionPlugin.Logging.Logger.LogLevel Filter { get; set; } = IllusionPlugin.Logging.Logger.LogLevel.All; + public override LoggerBase.LogLevel Filter { get; set; } = LoggerBase.LogLevel.All; - private FileInfo fileInfo; - private StreamWriter fileWriter; + private string name; - private static FileInfo GetFileInfo(string modName) + protected override FileInfo GetFileInfo() { - var logsDir = new DirectoryInfo(Path.Combine("Logs",modName)); + var logsDir = new DirectoryInfo(Path.Combine("Logs",name)); logsDir.Create(); - var finfo = new FileInfo(Path.Combine(logsDir.FullName, $"{DateTime.Now:yyyy.MM.dd.HH.MM}.log")); - finfo.CreateText().Close(); + var finfo = new FileInfo(Path.Combine(logsDir.FullName, $"{DateTime.Now:yyyy.MM.dd.HH.mm}.log")); return finfo; } public PluginLogFilePrinter(string name) { - fileInfo = GetFileInfo(name); - } - - public override void StartPrint() - { - fileWriter = fileInfo.AppendText(); + this.name = name; } public override void Print(IllusionPlugin.Logging.Logger.Level level, DateTime time, string logName, string message) @@ -39,12 +33,5 @@ namespace IllusionInjector.Logging.Printers foreach (var line in message.Split(new string[] { "\n", Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries)) fileWriter.WriteLine(string.Format("[{3} @ {2:HH:mm:ss}] {0}", line, logName, time, level.ToString().ToUpper())); } - - public override void EndPrint() - { - fileWriter.Flush(); - fileWriter.Close(); - fileWriter.Dispose(); - } } } diff --git a/IllusionInjector/Logging/StandardLogger.cs b/IllusionInjector/Logging/StandardLogger.cs index c506c325..8cbb4ad2 100644 --- a/IllusionInjector/Logging/StandardLogger.cs +++ b/IllusionInjector/Logging/StandardLogger.cs @@ -55,7 +55,7 @@ namespace IllusionInjector.Logging Filter = LogLevel.CriticalOnly, Color = ConsoleColor.Magenta, }, - new GlobalZFilePrinter() + new GlobalLogFilePrinter() }; private string logName; diff --git a/IllusionInjector/obj/Debug/IllusionInjector.csproj.CoreCompileInputs.cache b/IllusionInjector/obj/Debug/IllusionInjector.csproj.CoreCompileInputs.cache index 6c5e77d0..c7289e41 100644 --- a/IllusionInjector/obj/Debug/IllusionInjector.csproj.CoreCompileInputs.cache +++ b/IllusionInjector/obj/Debug/IllusionInjector.csproj.CoreCompileInputs.cache @@ -1 +1 @@ -479f561c6be8784a5830081167b62f103b95e138 +2fe547896965157e6254a6138df5fa8e17aceac2