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