Browse Source

Add log file limit to config

js6pak 3 years ago
parent
commit
619fa2db74

+ 6 - 1
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<int> ConfigDiskLoggingFileLimit = ConfigFile.CoreConfig.Bind(
+			"Logging.Disk", "FileLimit",
+			5,
+			"Maximum amount of concurrently opened log files. Can help with infinite game boot loops.");
+
 		#endregion
 	}
 }

+ 7 - 3
BepInEx.Core/Logging/DiskLogListener.cs

@@ -34,7 +34,8 @@ namespace BepInEx.Logging
 		/// <param name="displayedLogLevel">Log levels to display.</param>
 		/// <param name="appendLog">Whether to append logs to an already existing log file.</param>
 		/// <param name="delayedFlushing">Whether to delay flushing to disk to improve performance. Useful to set this to false when debugging crashes.</param>
-		public DiskLogListener(string localPath, LogLevel displayedLogLevel = LogLevel.Info, bool appendLog = false, bool delayedFlushing = true)
+		/// <param name="fileLimit">Maximum amount of concurrently opened log files. Can help with infinite game boot loops.</param>
+		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
 		/// <inheritdoc />
 		public void LogEvent(object sender, LogEventArgs eventArgs)
 		{
+			if (LogWriter == null)
+				return;
+			
 			if (BlacklistedSources.Contains(eventArgs.Source.SourceName))
 				return;