Selaa lähdekoodia

Fix logging filter flags; clean up logging API

ghorsington 4 vuotta sitten
vanhempi
commit
1a574b3e58

+ 83 - 90
BepInEx.Preloader/Logger/PreloaderLogWriter.cs

@@ -1,91 +1,84 @@
-using System;
-using System.Collections.Generic;
-using System.IO;
-using System.Text;
-using BepInEx.ConsoleUtil;
-using BepInEx.Logging;
-
-namespace BepInEx.Preloader
-{
-	public class PreloaderConsoleListener : ILogListener
-	{
-		public static List<LogEventArgs> LogEvents { get; } = new List<LogEventArgs>();
-		protected StringBuilder LogBuilder = new StringBuilder();
-
-		public static TextWriter StandardOut { get; set; }
-		protected PreloaderConsoleSource LoggerSource { get; set; }
-
-
-		public PreloaderConsoleListener(bool redirectConsole)
-		{
-			StandardOut = Console.Out;
-
-			if (redirectConsole)
-			{
-				LoggerSource = new PreloaderConsoleSource();
-
-				Logger.Sources.Add(LoggerSource);
-				Console.SetOut(LoggerSource);
-			}
-		}
-
-		public void LogEvent(object sender, LogEventArgs eventArgs)
-		{
-			LogEvents.Add(eventArgs);
-
-			string log = $"[{eventArgs.Level,-7}:{((ILogSource)sender).SourceName,10}] {eventArgs.Data}\r\n";
-
-			LogBuilder.Append(log);
-
-			Kon.ForegroundColor = eventArgs.Level.GetConsoleColor();
-			ConsoleDirectWrite(log);
-			Kon.ForegroundColor = ConsoleColor.Gray;
-		}
-
-		public void ConsoleDirectWrite(string value)
-		{
-			StandardOut.Write(value);
-		}
-
-		public void ConsoleDirectWriteLine(string value)
-		{
-			StandardOut.WriteLine(value);
-		}
-
-		public override string ToString() => LogBuilder.ToString();
-
-		public void Dispose()
-		{
-			if (LoggerSource != null)
-			{
-				Console.SetOut(StandardOut);
-				Logger.Sources.Remove(LoggerSource);
-				LoggerSource.Dispose();
-				LoggerSource = null;
-			}
-		}
-	}
-
-	public class PreloaderConsoleSource : TextWriter, ILogSource
-	{
-		public override Encoding Encoding { get; } = Console.OutputEncoding;
-
-		public string SourceName { get; } = "BepInEx Preloader";
-
-		public event EventHandler<LogEventArgs> LogEvent;
-
-		public override void Write(object value)
-			=> LogEvent?.Invoke(this, new LogEventArgs(value, LogLevel.Info, this));
-
-		public override void Write(string value)
-			=> Write((object)value);
-
-		public override void WriteLine() { }
-
-		public override void WriteLine(object value)
-			=> Write(value);
-
-		public override void WriteLine(string value)
-			=> Write(value);
-	}
+using System;
+using System.Collections.Generic;
+using System.IO;
+using System.Text;
+using BepInEx.ConsoleUtil;
+using BepInEx.Logging;
+
+namespace BepInEx.Preloader
+{
+	public class PreloaderConsoleListener : ILogListener
+	{
+		public static List<LogEventArgs> LogEvents { get; } = new List<LogEventArgs>();
+
+		public static TextWriter StandardOut { get; set; }
+		protected PreloaderConsoleSource LoggerSource { get; set; }
+
+
+		public PreloaderConsoleListener(bool redirectConsole)
+		{
+			StandardOut = Console.Out;
+
+			if (redirectConsole)
+			{
+				LoggerSource = new PreloaderConsoleSource();
+
+				Logger.Sources.Add(LoggerSource);
+				Console.SetOut(LoggerSource);
+			}
+		}
+
+		public void LogEvent(object sender, LogEventArgs eventArgs)
+		{
+			LogEvents.Add(eventArgs);
+
+			Kon.ForegroundColor = eventArgs.Level.GetConsoleColor();
+			ConsoleDirectWrite(eventArgs.ToStringLine());
+			Kon.ForegroundColor = ConsoleColor.Gray;
+		}
+
+		public void ConsoleDirectWrite(string value)
+		{
+			StandardOut.Write(value);
+		}
+
+		public void ConsoleDirectWriteLine(string value)
+		{
+			StandardOut.WriteLine(value);
+		}
+
+		public void Dispose()
+		{
+			if (LoggerSource != null)
+			{
+				Console.SetOut(StandardOut);
+				Logger.Sources.Remove(LoggerSource);
+				LoggerSource.Dispose();
+				LoggerSource = null;
+			}
+		}
+	}
+
+	public class PreloaderConsoleSource : TextWriter, ILogSource
+	{
+		public override Encoding Encoding { get; } = Console.OutputEncoding;
+
+		public string SourceName { get; } = "BepInEx Preloader";
+
+		public event EventHandler<LogEventArgs> LogEvent;
+
+		public override void Write(object value)
+			=> LogEvent?.Invoke(this, new LogEventArgs(value, LogLevel.Info, this));
+
+		public override void Write(string value)
+			=> Write((object)value);
+
+		public override void WriteLine() { }
+
+		public override void WriteLine(object value)
+			=> Write(value);
+
+		public override void WriteLine(string value)
+			=> Write(value);
+	}
 }

+ 1 - 1
BepInEx/Bootstrap/Chainloader.cs

@@ -109,7 +109,7 @@ namespace BepInEx.Bootstrap
 				var preloaderLogSource = Logger.CreateLogSource("Preloader");
 
 				foreach (var preloaderLogEvent in preloaderLogEvents)
-					preloaderLogSource.Log(preloaderLogEvent.Level, $"[{preloaderLogEvent.Source.SourceName,10}] {preloaderLogEvent.Data}");
+					Logger.InternalLogEvent(preloaderLogSource, preloaderLogEvent);
 
 				Logger.Sources.Remove(preloaderLogSource);	
 			}

+ 2 - 4
BepInEx/Logging/ConsoleLogListener.cs

@@ -11,13 +11,11 @@ namespace BepInEx.Logging
 	{
 		public void LogEvent(object sender, LogEventArgs eventArgs)
 		{
-			if ((eventArgs.Level & ConfigConsoleDisplayedLevel.Value) > 0)
+			if ((eventArgs.Level & ConfigConsoleDisplayedLevel.Value) == 0)
 				return;
 
-			string log = $"[{eventArgs.Level,-7}:{((ILogSource)sender).SourceName,10}] {eventArgs.Data}\r\n";
-
 			Kon.ForegroundColor = eventArgs.Level.GetConsoleColor();
-			Console.Write(log);
+			Console.Write(eventArgs.ToStringLine());
 			Kon.ForegroundColor = ConsoleColor.Gray;
 		}
 

+ 2 - 2
BepInEx/Logging/DiskLogListener.cs

@@ -51,10 +51,10 @@ namespace BepInEx.Logging
 			if (!WriteFromUnityLog && eventArgs.Source is UnityLogSource)
 				return;
 
-			if ((eventArgs.Level & DisplayedLogLevel) > 0)
+			if ((eventArgs.Level & DisplayedLogLevel) == 0)
 				return;
 
-			LogWriter.WriteLine($"[{eventArgs.Level,-7}:{((ILogSource)sender).SourceName,10}] {eventArgs.Data}");
+			LogWriter.WriteLine(eventArgs.ToString());
 		}
 
 		public void Dispose()

+ 29 - 19
BepInEx/Logging/LogEventArgs.cs

@@ -1,20 +1,30 @@
-using System;
-
-namespace BepInEx.Logging
-{
-	public class LogEventArgs : EventArgs
-	{
-		public object Data { get; protected set; }
-
-		public LogLevel Level { get; protected set; }
-
-		public ILogSource Source { get; protected set; }
-
-		public LogEventArgs(object data, LogLevel level, ILogSource source)
-		{
-			Data = data;
-			Level = level;
-			Source = source;
-		}
-	}
+using System;
+
+namespace BepInEx.Logging
+{
+	public class LogEventArgs : EventArgs
+	{
+		public object Data { get; protected set; }
+
+		public LogLevel Level { get; protected set; }
+
+		public ILogSource Source { get; protected set; }
+
+		public LogEventArgs(object data, LogLevel level, ILogSource source)
+		{
+			Data = data;
+			Level = level;
+			Source = source;
+		}
+
+		public override string ToString()
+		{
+			return $"[{Level,-7}:{Source.SourceName,10}] {Data}";
+		}
+
+		public string ToStringLine()
+		{
+			return $"[{Level,-7}:{Source.SourceName,10}] {Data}{Environment.NewLine}";
+		}
+	}
 }

+ 87 - 87
BepInEx/Logging/Logger.cs

@@ -1,88 +1,88 @@
-using System;
-using System.Collections.Generic;
-
-namespace BepInEx.Logging
-{
-	/// <summary>
-	/// A static <see cref="BaseLogger"/> instance.
-	/// </summary>
-	public static class Logger
-	{
-		public static ICollection<ILogListener> Listeners { get; } = new List<ILogListener>();
-
-		public static ICollection<ILogSource> Sources { get; } = new LogSourceCollection();
-
-		private static readonly ManualLogSource InternalLogSource = CreateLogSource("BepInEx");
-
-		private static void InternalLogEvent(object sender, LogEventArgs eventArgs)
-		{
-			foreach (var listener in Listeners)
-			{
-				listener?.LogEvent(sender, eventArgs);
-			}
-		}
-
-		/// <summary>
-		/// Logs an entry to the current logger instance.
-		/// </summary>
-		/// <param name="level">The level of the entry.</param>
-		/// <param name="entry">The textual value of the entry.</param>
-		internal static void Log(LogLevel level, object data)
-		{
-			InternalLogSource.Log(level, data);
-		}
-
-		internal static void LogFatal(object data) => Log(LogLevel.Fatal, data);
-		internal static void LogError(object data) => Log(LogLevel.Error, data);
-		internal static void LogWarning(object data) => Log(LogLevel.Warning, data);
-		internal static void LogMessage(object data) => Log(LogLevel.Message, data);
-		internal static void LogInfo(object data) => Log(LogLevel.Info, data);
-		internal static void LogDebug(object data) => Log(LogLevel.Debug, data);
-
-		public static ManualLogSource CreateLogSource(string sourceName)
-		{
-			var source = new ManualLogSource(sourceName);
-
-			Sources.Add(source);
-
-			return source;
-		}
-
-
-		private class LogSourceCollection : List<ILogSource>, ICollection<ILogSource>
-		{
-			void ICollection<ILogSource>.Add(ILogSource item)
-			{
-				if (item == null)
-					throw new ArgumentNullException(nameof(item), "Log sources cannot be null when added to the source list.");
-
-				item.LogEvent += InternalLogEvent;
-
-				base.Add(item);
-			}
-
-			void ICollection<ILogSource>.Clear()
-			{
-				foreach (var item in base.ToArray())
-				{
-					((ICollection<ILogSource>)this).Remove(item);
-				}
-			}
-
-			bool ICollection<ILogSource>.Remove(ILogSource item)
-			{
-				if (item == null)
-					return false;
-
-				if (!base.Contains(item))
-					return false;
-
-				item.LogEvent -= InternalLogEvent;
-
-				base.Remove(item);
-
-				return true;
-			}
-		}
-	}
+using System;
+using System.Collections.Generic;
+
+namespace BepInEx.Logging
+{
+	/// <summary>
+	/// A static <see cref="BaseLogger"/> instance.
+	/// </summary>
+	public static class Logger
+	{
+		public static ICollection<ILogListener> Listeners { get; } = new List<ILogListener>();
+
+		public static ICollection<ILogSource> Sources { get; } = new LogSourceCollection();
+
+		private static readonly ManualLogSource InternalLogSource = CreateLogSource("BepInEx");
+
+		internal static void InternalLogEvent(object sender, LogEventArgs eventArgs)
+		{
+			foreach (var listener in Listeners)
+			{
+				listener?.LogEvent(sender, eventArgs);
+			}
+		}
+
+		/// <summary>
+		/// Logs an entry to the current logger instance.
+		/// </summary>
+		/// <param name="level">The level of the entry.</param>
+		/// <param name="entry">The textual value of the entry.</param>
+		internal static void Log(LogLevel level, object data)
+		{
+			InternalLogSource.Log(level, data);
+		}
+
+		internal static void LogFatal(object data) => Log(LogLevel.Fatal, data);
+		internal static void LogError(object data) => Log(LogLevel.Error, data);
+		internal static void LogWarning(object data) => Log(LogLevel.Warning, data);
+		internal static void LogMessage(object data) => Log(LogLevel.Message, data);
+		internal static void LogInfo(object data) => Log(LogLevel.Info, data);
+		internal static void LogDebug(object data) => Log(LogLevel.Debug, data);
+
+		public static ManualLogSource CreateLogSource(string sourceName)
+		{
+			var source = new ManualLogSource(sourceName);
+
+			Sources.Add(source);
+
+			return source;
+		}
+
+
+		private class LogSourceCollection : List<ILogSource>, ICollection<ILogSource>
+		{
+			void ICollection<ILogSource>.Add(ILogSource item)
+			{
+				if (item == null)
+					throw new ArgumentNullException(nameof(item), "Log sources cannot be null when added to the source list.");
+
+				item.LogEvent += InternalLogEvent;
+
+				base.Add(item);
+			}
+
+			void ICollection<ILogSource>.Clear()
+			{
+				foreach (var item in base.ToArray())
+				{
+					((ICollection<ILogSource>)this).Remove(item);
+				}
+			}
+
+			bool ICollection<ILogSource>.Remove(ILogSource item)
+			{
+				if (item == null)
+					return false;
+
+				if (!base.Contains(item))
+					return false;
+
+				item.LogEvent -= InternalLogEvent;
+
+				base.Remove(item);
+
+				return true;
+			}
+		}
+	}
 }

+ 56 - 58
BepInEx/Logging/UnityLogListener.cs

@@ -1,59 +1,57 @@
-using System;
-using System.Reflection;
-using System.Runtime.CompilerServices;
-
-namespace BepInEx.Logging
-{
-	/// <summary>
-	/// Logs entries using Unity specific outputs.
-	/// </summary>
-	public class UnityLogListener : ILogListener
-	{
-		internal static readonly Action<string> WriteStringToUnityLog;
-
-		static UnityLogListener()
-		{
-			foreach (MethodInfo methodInfo in typeof(UnityEngine.UnityLogWriter).GetMethods(BindingFlags.Static | BindingFlags.Public))
-			{
-				try
-				{
-					methodInfo.Invoke(null, new object[] { "" });
-				}
-				catch
-				{
-					continue;
-				}
-
-				WriteStringToUnityLog = (Action<string>)Delegate.CreateDelegate(typeof(Action<string>), methodInfo);
-				break;
-			}
-
-			if (WriteStringToUnityLog == null)
-				Logger.LogError("Unable to start Unity log writer");
-		}
-
-		public void LogEvent(object sender, LogEventArgs eventArgs)
-		{
-			if (sender is UnityLogSource)
-				return;
-
-			string log = $"[{eventArgs.Level}:{((ILogSource)sender).SourceName}] {eventArgs.Data}\r\n";
-
-			WriteStringToUnityLog?.Invoke(log);
-		}
-
-		public void Dispose() { }
-	}
-}
-
-namespace UnityEngine
-{
-	internal sealed class UnityLogWriter
-	{
-		[MethodImpl(MethodImplOptions.InternalCall)]
-		public static extern void WriteStringToUnityLogImpl(string s);
-
-		[MethodImpl(MethodImplOptions.InternalCall)]
-		public static extern void WriteStringToUnityLog(string s);
-	}
+using System;
+using System.Reflection;
+using System.Runtime.CompilerServices;
+
+namespace BepInEx.Logging
+{
+	/// <summary>
+	/// Logs entries using Unity specific outputs.
+	/// </summary>
+	public class UnityLogListener : ILogListener
+	{
+		internal static readonly Action<string> WriteStringToUnityLog;
+
+		static UnityLogListener()
+		{
+			foreach (MethodInfo methodInfo in typeof(UnityEngine.UnityLogWriter).GetMethods(BindingFlags.Static | BindingFlags.Public))
+			{
+				try
+				{
+					methodInfo.Invoke(null, new object[] { "" });
+				}
+				catch
+				{
+					continue;
+				}
+
+				WriteStringToUnityLog = (Action<string>)Delegate.CreateDelegate(typeof(Action<string>), methodInfo);
+				break;
+			}
+
+			if (WriteStringToUnityLog == null)
+				Logger.LogError("Unable to start Unity log writer");
+		}
+
+		public void LogEvent(object sender, LogEventArgs eventArgs)
+		{
+			if (eventArgs.Source is UnityLogSource)
+				return;
+
+			WriteStringToUnityLog?.Invoke(eventArgs.ToStringLine());
+		}
+
+		public void Dispose() { }
+	}
+}
+
+namespace UnityEngine
+{
+	internal sealed class UnityLogWriter
+	{
+		[MethodImpl(MethodImplOptions.InternalCall)]
+		public static extern void WriteStringToUnityLogImpl(string s);
+
+		[MethodImpl(MethodImplOptions.InternalCall)]
+		public static extern void WriteStringToUnityLog(string s);
+	}
 }