From c0ba05474f5ad3516146952f8f7b28ce5d55cbc3 Mon Sep 17 00:00:00 2001 From: Anairkoen Schno Date: Thu, 21 Jul 2022 02:01:58 -0500 Subject: [PATCH] Fix #70 #71 #78 and related issues --- IPA.Loader/Logging/Printers/GZFilePrinter.cs | 56 ++++++++++++-------- 1 file changed, 34 insertions(+), 22 deletions(-) diff --git a/IPA.Loader/Logging/Printers/GZFilePrinter.cs b/IPA.Loader/Logging/Printers/GZFilePrinter.cs index ebd9b5a2..93fe65e5 100644 --- a/IPA.Loader/Logging/Printers/GZFilePrinter.cs +++ b/IPA.Loader/Logging/Printers/GZFilePrinter.cs @@ -1,5 +1,7 @@ -using Ionic.Zlib; +#nullable enable +using Ionic.Zlib; using System; +using System.Diagnostics.CodeAnalysis; using System.IO; using System.Runtime.InteropServices; using System.Text; @@ -29,17 +31,17 @@ namespace IPA.Logging.Printers private const RegexOptions reOptions = RegexOptions.None; #endif - internal static Regex removeControlCodes = new Regex("\x1b\\[\\d+m", reOptions); + internal static Regex removeControlCodes = new("\x1b\\[\\d+m", reOptions); - private FileInfo fileInfo; + private FileInfo? fileInfo; /// /// The that writes to the GZip file. /// /// the writer to the underlying filestream - protected StreamWriter FileWriter; + protected StreamWriter? FileWriter; - private FileStream fstream; + private FileStream? fstream; /// /// Gets the for the file to write to. @@ -49,6 +51,7 @@ namespace IPA.Logging.Printers private const string latestFormat = "_latest{0}"; + [MemberNotNull(nameof(fileInfo))] private void InitLog() { try @@ -90,21 +93,30 @@ namespace IPA.Logging.Printers { Logger.Default.Error("Error initializing log!"); Logger.Default.Error(e); + throw; } } private static async void CompressOldLog(FileInfo file) { - Logger.Default.Debug($"Compressing log file {file}"); + try + { + Logger.Default.Debug($"Compressing log file {file}"); - var newFile = new FileInfo(file.FullName + ".gz"); + 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); + 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).ConfigureAwait(false); - file.Delete(); + file.Delete(); + } + catch (Exception e) + { + Logger.Default.Error("Error compressing old log file:"); + Logger.Default.Error(e); + } } /// @@ -123,10 +135,10 @@ namespace IPA.Logging.Printers /// public sealed override void EndPrint() { - FileWriter.Flush(); - fstream.Flush(); - FileWriter.Dispose(); - fstream.Dispose(); + FileWriter?.Flush(); + fstream?.Flush(); + FileWriter?.Dispose(); + fstream?.Dispose(); FileWriter = null; fstream = null; } @@ -146,12 +158,12 @@ namespace IPA.Logging.Printers { if (disposing) { - FileWriter.Flush(); - fstream.Flush(); - FileWriter.Close(); - fstream.Close(); - FileWriter.Dispose(); - fstream.Dispose(); + FileWriter?.Flush(); + fstream?.Flush(); + FileWriter?.Close(); + fstream?.Close(); + FileWriter?.Dispose(); + fstream?.Dispose(); } } }