Browse Source

Implement a config option for instantly flushing logs to disk

Bepis 3 years ago
parent
commit
0d8da1b30f
2 changed files with 24 additions and 6 deletions
  1. 11 3
      BepInEx.Core/Bootstrap/BaseChainloader.cs
  2. 13 3
      BepInEx.Core/Logging/DiskLogListener.cs

+ 11 - 3
BepInEx.Core/Bootstrap/BaseChainloader.cs

@@ -1,9 +1,9 @@
 using System;
 using System.Collections.Generic;
-using System.Diagnostics;
 using System.IO;
 using System.Linq;
 using System.Reflection;
+using System.Text;
 using System.Text.RegularExpressions;
 using BepInEx.Configuration;
 using BepInEx.Logging;
@@ -70,7 +70,7 @@ namespace BepInEx.Bootstrap
 			}
 
 			if (ConfigDiskLogging.Value)
-				Logger.Listeners.Add(new DiskLogListener("LogOutput.log", ConfigDiskConsoleDisplayedLevel.Value, ConfigDiskAppend.Value));
+				Logger.Listeners.Add(new DiskLogListener("LogOutput.log", ConfigDiskLoggingDisplayedLevel.Value, ConfigDiskAppend.Value, ConfigDiskLoggingInstantFlushing.Value));
 
 			if (!TraceLogSource.IsListening)
 				Logger.Sources.Add(TraceLogSource.CreateSource());
@@ -361,11 +361,19 @@ namespace BepInEx.Bootstrap
 			true,
 			"Enables writing log messages to disk.");
 
-		private static readonly ConfigEntry<LogLevel> ConfigDiskConsoleDisplayedLevel = ConfigFile.CoreConfig.Bind(
+		private static readonly ConfigEntry<LogLevel> ConfigDiskLoggingDisplayedLevel = ConfigFile.CoreConfig.Bind(
 			"Logging.Disk", "LogLevels",
 			LogLevel.Fatal | LogLevel.Error | LogLevel.Warning | LogLevel.Message | LogLevel.Info,
 			"Only displays the specified log levels in the disk log output.");
 
+		private static readonly ConfigEntry<bool> ConfigDiskLoggingInstantFlushing = ConfigFile.CoreConfig.Bind(
+			"Logging.Disk", "InstantFlushing",
+			false,
+			new StringBuilder()
+				.AppendLine("If true, instantly writes any received log entries to disk.")
+				.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());
+
 		#endregion
 	}
 }

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

@@ -23,7 +23,9 @@ namespace BepInEx.Logging
 		/// <summary>
 		/// Timer for flushing the logs to a file.
 		/// </summary>
-		public Timer FlushTimer { get; protected set; }
+		private Timer FlushTimer { get; set; }
+
+		private bool InstantFlushing { get; set; }
 
 		/// <summary>
 		/// Creates a new disk log listener.
@@ -31,7 +33,8 @@ namespace BepInEx.Logging
 		/// <param name="localPath">Path to the log.</param>
 		/// <param name="displayedLogLevel">Log levels to display.</param>
 		/// <param name="appendLog">Whether to append logs to an already existing log file.</param>
-		public DiskLogListener(string localPath, LogLevel displayedLogLevel = LogLevel.Info, bool appendLog = false)
+		/// <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)
 		{
 			DisplayedLogLevel = displayedLogLevel;
 
@@ -55,8 +58,12 @@ namespace BepInEx.Logging
 
 			LogWriter = TextWriter.Synchronized(new StreamWriter(fileStream, Encoding.UTF8));
 
+			if (delayedFlushing)
+			{
+				FlushTimer = new Timer(o => { LogWriter?.Flush(); }, null, 2000, 2000);
+			}
 
-			FlushTimer = new Timer(o => { LogWriter?.Flush(); }, null, 2000, 2000);
+			InstantFlushing = !delayedFlushing;
 		}
 
 		public static HashSet<string> BlacklistedSources = new HashSet<string>();
@@ -71,6 +78,9 @@ namespace BepInEx.Logging
 				return;
 
 			LogWriter.WriteLine(eventArgs.ToString());
+
+			if (InstantFlushing)
+				LogWriter.Flush();
 		}
 
 		/// <inheritdoc />