From 619fa2db743e09459ce62d54dd6dbc5b40bf288b Mon Sep 17 00:00:00 2001 From: js6pak Date: Mon, 16 Nov 2020 08:29:40 +0100 Subject: [PATCH] Add log file limit to config --- BepInEx.Core/Bootstrap/BaseChainloader.cs | 7 ++++++- BepInEx.Core/Logging/DiskLogListener.cs | 10 +++++++--- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/BepInEx.Core/Bootstrap/BaseChainloader.cs b/BepInEx.Core/Bootstrap/BaseChainloader.cs index 382772e..fd3c2a6 100644 --- a/BepInEx.Core/Bootstrap/BaseChainloader.cs +++ b/BepInEx.Core/Bootstrap/BaseChainloader.cs @@ -70,7 +70,7 @@ namespace BepInEx.Bootstrap } if (ConfigDiskLogging.Value) - Logger.Listeners.Add(new DiskLogListener("LogOutput.log", ConfigDiskLoggingDisplayedLevel.Value, ConfigDiskAppend.Value, ConfigDiskLoggingInstantFlushing.Value)); + Logger.Listeners.Add(new DiskLogListener("LogOutput.log", ConfigDiskLoggingDisplayedLevel.Value, ConfigDiskAppend.Value, ConfigDiskLoggingInstantFlushing.Value, ConfigDiskLoggingFileLimit.Value)); if (!TraceLogSource.IsListening) Logger.Sources.Add(TraceLogSource.CreateSource()); @@ -374,6 +374,11 @@ namespace BepInEx.Bootstrap .AppendLine("This incurs a major performance hit if a lot of log messages are being written, however it is really useful for debugging crashes.") .ToString()); + private static readonly ConfigEntry ConfigDiskLoggingFileLimit = ConfigFile.CoreConfig.Bind( + "Logging.Disk", "FileLimit", + 5, + "Maximum amount of concurrently opened log files. Can help with infinite game boot loops."); + #endregion } } diff --git a/BepInEx.Core/Logging/DiskLogListener.cs b/BepInEx.Core/Logging/DiskLogListener.cs index acdf17c..2e33ae4 100644 --- a/BepInEx.Core/Logging/DiskLogListener.cs +++ b/BepInEx.Core/Logging/DiskLogListener.cs @@ -34,7 +34,8 @@ namespace BepInEx.Logging /// Log levels to display. /// Whether to append logs to an already existing log file. /// Whether to delay flushing to disk to improve performance. Useful to set this to false when debugging crashes. - public DiskLogListener(string localPath, LogLevel displayedLogLevel = LogLevel.Info, bool appendLog = false, bool delayedFlushing = true) + /// Maximum amount of concurrently opened log files. Can help with infinite game boot loops. + public DiskLogListener(string localPath, LogLevel displayedLogLevel = LogLevel.Info, bool appendLog = false, bool delayedFlushing = true, int fileLimit = 5) { DisplayedLogLevel = displayedLogLevel; @@ -44,7 +45,7 @@ namespace BepInEx.Logging while (!Utility.TryOpenFileStream(Path.Combine(Paths.BepInExRootPath, localPath), appendLog ? FileMode.Append : FileMode.Create, out fileStream, share: FileShare.Read, access: FileAccess.Write)) { - if (counter == 5) + if (counter == fileLimit) { Logger.LogError("Couldn't open a log file for writing. Skipping log file creation"); @@ -53,7 +54,7 @@ namespace BepInEx.Logging Logger.LogWarning($"Couldn't open log file '{localPath}' for writing, trying another..."); - localPath = $"LogOutput.log.{counter++}"; + localPath = $"LogOutput.{counter++}.log"; } LogWriter = TextWriter.Synchronized(new StreamWriter(fileStream, Encoding.UTF8)); @@ -71,6 +72,9 @@ namespace BepInEx.Logging /// public void LogEvent(object sender, LogEventArgs eventArgs) { + if (LogWriter == null) + return; + if (BlacklistedSources.Contains(eventArgs.Source.SourceName)) return;