Просмотр исходного кода

Move disk logging configuration options to chainloader, and add config option to enable and disable disk logging

Bepis 5 лет назад
Родитель
Сommit
5fadac5b43
2 измененных файлов с 47 добавлено и 33 удалено
  1. 35 5
      BepInEx/Bootstrap/Chainloader.cs
  2. 12 28
      BepInEx/Logging/DiskLogListener.cs

+ 35 - 5
BepInEx/Bootstrap/Chainloader.cs

@@ -71,7 +71,12 @@ namespace BepInEx.Bootstrap
 			}
 
 			Logger.Listeners.Add(new UnityLogListener());
-			Logger.Listeners.Add(new DiskLogListener());
+
+			if (ConfigDiskLogging.Value)
+			{
+				var logLevel = (LogLevel)Enum.Parse(typeof(LogLevel), ConfigDiskConsoleDisplayedLevel.Value, true);
+				Logger.Listeners.Add(new DiskLogListener("LogOutput.log", logLevel, ConfigDiskAppend.Value, ConfigDiskWriteUnityLog.Value));
+			}
 
 			if (!TraceLogSource.IsListening)
 				Logger.Sources.Add(TraceLogSource.CreateSource());
@@ -304,10 +309,35 @@ namespace BepInEx.Bootstrap
 
 		#region Config
 
-		private static readonly ConfigWrapper<string> ConfigPluginsDirectory = ConfigFile.CoreConfig.Wrap("Paths", "PluginsDirectory", "The relative directory to the BepInEx folder where plugins are loaded.", "plugins");
-
-		private static readonly ConfigWrapper<bool> ConfigUnityLogging = ConfigFile.CoreConfig.Wrap("Logging", "UnityLogListening", "Enables showing unity log messages in the BepInEx logging system.", true);
-
+		private static readonly ConfigWrapper<bool> ConfigUnityLogging = ConfigFile.CoreConfig.Wrap(
+			"Logging", 
+			"UnityLogListening", 
+			"Enables showing unity log messages in the BepInEx logging system.", 
+			true);
+
+		private static readonly ConfigWrapper<bool> ConfigDiskLogging = ConfigFile.CoreConfig.Wrap(
+			"Logging.Disk", 
+			"Enabled", 
+			"Enables writing log messages to disk.", 
+			true);
+
+		private static readonly ConfigWrapper<string> ConfigDiskConsoleDisplayedLevel = ConfigFile.CoreConfig.Wrap(
+			"Logging.Disk",
+			"DisplayedLogLevel",
+			"Only displays the specified log level and above in the console output.",
+			"Info");
+
+		private static readonly ConfigWrapper<bool> ConfigDiskWriteUnityLog = ConfigFile.CoreConfig.Wrap(
+			"Logging.Disk",
+			"WriteUnityLog",
+			"Include unity log messages in log file output.",
+			false);
+
+		private static readonly ConfigWrapper<bool> ConfigDiskAppend = ConfigFile.CoreConfig.Wrap(
+			"Logging.Disk",
+			"AppendLog",
+			"Appends to the log file instead of overwriting, on game startup.",
+			false);
 		#endregion
 	}
 }

+ 12 - 28
BepInEx/Logging/DiskLogListener.cs

@@ -1,8 +1,6 @@
-using System;
-using System.IO;
+using System.IO;
 using System.Text;
 using System.Threading;
-using BepInEx.Configuration;
 
 namespace BepInEx.Logging
 {
@@ -11,20 +9,24 @@ namespace BepInEx.Logging
 	/// </summary>
 	public class DiskLogListener : ILogListener
 	{
-		protected LogLevel DisplayedLogLevel = (LogLevel)Enum.Parse(typeof(LogLevel), ConfigConsoleDisplayedLevel.Value, true);
+		public LogLevel DisplayedLogLevel { get; set; }
 
-		protected TextWriter LogWriter { get; set; }
+		public TextWriter LogWriter { get; protected set; }
 
-		protected Timer FlushTimer { get; set; }
+		public Timer FlushTimer { get; protected set; }
 
-		public DiskLogListener()
+		public bool WriteFromUnityLog { get; set; }
+
+		public DiskLogListener(string localPath, LogLevel displayedLogLevel = LogLevel.Info, bool appendLog = false, bool includeUnityLog = false)
 		{
+			WriteFromUnityLog = includeUnityLog;
+			DisplayedLogLevel = displayedLogLevel;
+
 			int counter = 1;
-			string localPath = "LogOutput.log";
 
 			FileStream fileStream;
 
-			while (!Utility.TryOpenFileStream(Path.Combine(Paths.BepInExRootPath, localPath), ConfigAppendLog.Value ? FileMode.Append : FileMode.Create, out fileStream, share: FileShare.Read))
+			while (!Utility.TryOpenFileStream(Path.Combine(Paths.BepInExRootPath, localPath), appendLog ? FileMode.Append : FileMode.Create, out fileStream, share: FileShare.Read))
 			{
 				if (counter == 5)
 				{
@@ -46,7 +48,7 @@ namespace BepInEx.Logging
 
 		public void LogEvent(object sender, LogEventArgs eventArgs)
 		{
-			if (!ConfigWriteUnityLog.Value && eventArgs.Source is UnityLogSource)
+			if (!WriteFromUnityLog && eventArgs.Source is UnityLogSource)
 				return;
 
 			if (eventArgs.Level.GetHighestLevel() > DisplayedLogLevel)
@@ -66,23 +68,5 @@ namespace BepInEx.Logging
 		{
 			Dispose();
 		}
-
-		private static readonly ConfigWrapper<string> ConfigConsoleDisplayedLevel = ConfigFile.CoreConfig.Wrap(
-			"Logging.Disk",
-			"DisplayedLogLevel",
-			"Only displays the specified log level and above in the console output.",
-			"Info");
-
-		private static readonly ConfigWrapper<bool> ConfigWriteUnityLog = ConfigFile.CoreConfig.Wrap(
-			"Logging.Disk",
-			"WriteUnityLog",
-			"Include unity log messages in log file output.",
-			false);
-
-		private static readonly ConfigWrapper<bool> ConfigAppendLog = ConfigFile.CoreConfig.Wrap(
-			"Logging.Disk",
-			"AppendLog",
-			"Appends to the log file instead of overwriting, on game startup.",
-			false);
 	}
 }