Browse Source

Updated logger to write to GZ files in all cases and create a latest.gz

pull/46/head
Anairkoen Schno 6 years ago
parent
commit
4f66631d4e
7 changed files with 134 additions and 88 deletions
  1. +2
    -1
      IllusionInjector/IllusionInjector.csproj
  2. +92
    -0
      IllusionInjector/Logging/Printers/GZFilePrinter.cs
  3. +30
    -0
      IllusionInjector/Logging/Printers/GlobalLogFilePrinter.cs
  4. +0
    -64
      IllusionInjector/Logging/Printers/GlobalZFIlePrinter.cs
  5. +8
    -21
      IllusionInjector/Logging/Printers/PluginLogFilePrinter.cs
  6. +1
    -1
      IllusionInjector/Logging/StandardLogger.cs
  7. +1
    -1
      IllusionInjector/obj/Debug/IllusionInjector.csproj.CoreCompileInputs.cache

+ 2
- 1
IllusionInjector/IllusionInjector.csproj View File

@ -56,7 +56,8 @@
<Compile Include="Injector.cs" /> <Compile Include="Injector.cs" />
<Compile Include="IPA\CompositeIPAPlugin.cs" /> <Compile Include="IPA\CompositeIPAPlugin.cs" />
<Compile Include="Logging\Printers\ColoredConsolePrinter.cs" /> <Compile Include="Logging\Printers\ColoredConsolePrinter.cs" />
<Compile Include="Logging\Printers\GlobalZFilePrinter.cs" />
<Compile Include="Logging\Printers\GlobalLogFilePrinter.cs" />
<Compile Include="Logging\Printers\GZFilePrinter.cs" />
<Compile Include="Logging\Printers\PluginLogFilePrinter.cs" /> <Compile Include="Logging\Printers\PluginLogFilePrinter.cs" />
<Compile Include="Logging\StandardLogger.cs" /> <Compile Include="Logging\StandardLogger.cs" />
<Compile Include="Logging\UnityLogInterceptor.cs" /> <Compile Include="Logging\UnityLogInterceptor.cs" />


+ 92
- 0
IllusionInjector/Logging/Printers/GZFilePrinter.cs View File

@ -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();
}
}
}

+ 30
- 0
IllusionInjector/Logging/Printers/GlobalLogFilePrinter.cs View File

@ -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;
}
}
}

+ 0
- 64
IllusionInjector/Logging/Printers/GlobalZFIlePrinter.cs View File

@ -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();
}
}
}

+ 8
- 21
IllusionInjector/Logging/Printers/PluginLogFilePrinter.cs View File

@ -5,33 +5,27 @@ using System.IO;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using LoggerBase = IllusionPlugin.Logging.Logger;
namespace IllusionInjector.Logging.Printers 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(); 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; return finfo;
} }
public PluginLogFilePrinter(string name) 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) 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)) 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())); 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();
}
} }
} }

+ 1
- 1
IllusionInjector/Logging/StandardLogger.cs View File

@ -55,7 +55,7 @@ namespace IllusionInjector.Logging
Filter = LogLevel.CriticalOnly, Filter = LogLevel.CriticalOnly,
Color = ConsoleColor.Magenta, Color = ConsoleColor.Magenta,
}, },
new GlobalZFilePrinter()
new GlobalLogFilePrinter()
}; };
private string logName; private string logName;


+ 1
- 1
IllusionInjector/obj/Debug/IllusionInjector.csproj.CoreCompileInputs.cache View File

@ -1 +1 @@
479f561c6be8784a5830081167b62f103b95e138
2fe547896965157e6254a6138df5fa8e17aceac2

Loading…
Cancel
Save