|
@ -28,7 +28,6 @@ namespace IPA.Logging.Printers |
|
|
/// </summary>
|
|
|
/// </summary>
|
|
|
protected StreamWriter FileWriter; |
|
|
protected StreamWriter FileWriter; |
|
|
|
|
|
|
|
|
private GZipStream zstream; |
|
|
|
|
|
private FileStream fstream; |
|
|
private FileStream fstream; |
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
/// <summary>
|
|
@ -37,6 +36,8 @@ namespace IPA.Logging.Printers |
|
|
/// <returns></returns>
|
|
|
/// <returns></returns>
|
|
|
protected abstract FileInfo GetFileInfo(); |
|
|
protected abstract FileInfo GetFileInfo(); |
|
|
|
|
|
|
|
|
|
|
|
private const string latestFormat = "_latest{0}"; |
|
|
|
|
|
|
|
|
private void InitLog() |
|
|
private void InitLog() |
|
|
{ |
|
|
{ |
|
|
try |
|
|
try |
|
@ -45,12 +46,19 @@ namespace IPA.Logging.Printers |
|
|
{ // first init
|
|
|
{ // first init
|
|
|
fileInfo = GetFileInfo(); |
|
|
fileInfo = GetFileInfo(); |
|
|
var ext = fileInfo.Extension; |
|
|
var ext = fileInfo.Extension; |
|
|
fileInfo = new FileInfo(fileInfo.FullName + ".gz"); |
|
|
|
|
|
fileInfo.Create().Close(); |
|
|
|
|
|
|
|
|
|
|
|
var symlink = new FileInfo(Path.Combine(fileInfo.DirectoryName ?? throw new InvalidOperationException(), $"_latest{ext}.gz")); |
|
|
|
|
|
|
|
|
var symlink = new FileInfo(Path.Combine(fileInfo.DirectoryName ?? throw new InvalidOperationException(), string.Format(latestFormat, ext))); |
|
|
if (symlink.Exists) symlink.Delete(); |
|
|
if (symlink.Exists) symlink.Delete(); |
|
|
|
|
|
|
|
|
|
|
|
foreach (var file in fileInfo.Directory.EnumerateFiles("*.log", SearchOption.TopDirectoryOnly)) |
|
|
|
|
|
{ |
|
|
|
|
|
if (file.Extension == ".gz") continue; |
|
|
|
|
|
|
|
|
|
|
|
CompressOldLog(file); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
fileInfo.Create().Close(); |
|
|
|
|
|
|
|
|
try |
|
|
try |
|
|
{ |
|
|
{ |
|
|
if (!CreateHardLink(symlink.FullName, fileInfo.FullName, IntPtr.Zero)) |
|
|
if (!CreateHardLink(symlink.FullName, fileInfo.FullName, IntPtr.Zero)) |
|
@ -73,6 +81,20 @@ namespace IPA.Logging.Printers |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private static async void CompressOldLog(FileInfo file) |
|
|
|
|
|
{ |
|
|
|
|
|
Logger.log.Debug($"Compressing log file {file}"); |
|
|
|
|
|
|
|
|
|
|
|
var newFile = new FileInfo(file.FullName + ".gz"); |
|
|
|
|
|
|
|
|
|
|
|
using (var istream = file.OpenRead()) |
|
|
|
|
|
using (var ostream = newFile.Create()) |
|
|
|
|
|
using (var gz = new GZipStream(ostream, CompressionMode.Compress, CompressionLevel.BestCompression, false)) |
|
|
|
|
|
await istream.CopyToAsync(gz); |
|
|
|
|
|
|
|
|
|
|
|
file.Delete(); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
/// <summary>
|
|
|
/// Called at the start of any print session.
|
|
|
/// Called at the start of any print session.
|
|
|
/// </summary>
|
|
|
/// </summary>
|
|
@ -81,11 +103,7 @@ namespace IPA.Logging.Printers |
|
|
InitLog(); |
|
|
InitLog(); |
|
|
|
|
|
|
|
|
fstream = fileInfo.Open(FileMode.Append, FileAccess.Write); |
|
|
fstream = fileInfo.Open(FileMode.Append, FileAccess.Write); |
|
|
zstream = new GZipStream(fstream, CompressionMode.Compress) |
|
|
|
|
|
{ |
|
|
|
|
|
FlushMode = FlushType.Full |
|
|
|
|
|
}; |
|
|
|
|
|
FileWriter = new StreamWriter(zstream, new UTF8Encoding(false)); |
|
|
|
|
|
|
|
|
FileWriter = new StreamWriter(fstream, new UTF8Encoding(false)); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
/// <summary>
|
|
@ -94,13 +112,10 @@ namespace IPA.Logging.Printers |
|
|
public sealed override void EndPrint() |
|
|
public sealed override void EndPrint() |
|
|
{ |
|
|
{ |
|
|
FileWriter.Flush(); |
|
|
FileWriter.Flush(); |
|
|
zstream.Flush(); |
|
|
|
|
|
fstream.Flush(); |
|
|
fstream.Flush(); |
|
|
FileWriter.Dispose(); |
|
|
FileWriter.Dispose(); |
|
|
zstream.Dispose(); |
|
|
|
|
|
fstream.Dispose(); |
|
|
fstream.Dispose(); |
|
|
FileWriter = null; |
|
|
FileWriter = null; |
|
|
zstream = null; |
|
|
|
|
|
fstream = null; |
|
|
fstream = null; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
@ -120,13 +135,10 @@ namespace IPA.Logging.Printers |
|
|
if (disposing) |
|
|
if (disposing) |
|
|
{ |
|
|
{ |
|
|
FileWriter.Flush(); |
|
|
FileWriter.Flush(); |
|
|
zstream.Flush(); |
|
|
|
|
|
fstream.Flush(); |
|
|
fstream.Flush(); |
|
|
FileWriter.Close(); |
|
|
FileWriter.Close(); |
|
|
zstream.Close(); |
|
|
|
|
|
fstream.Close(); |
|
|
fstream.Close(); |
|
|
FileWriter.Dispose(); |
|
|
FileWriter.Dispose(); |
|
|
zstream.Dispose(); |
|
|
|
|
|
fstream.Dispose(); |
|
|
fstream.Dispose(); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|