@ -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(); | |||||
} | |||||
} | |||||
} |
@ -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; | |||||
} | |||||
} | |||||
} |
@ -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(); | |||||
} | |||||
} | |||||
} |
@ -1 +1 @@ | |||||
479f561c6be8784a5830081167b62f103b95e138 | |||||
2fe547896965157e6254a6138df5fa8e17aceac2 |