Browse Source

Provide all BaseUnityPlugin classes with their own Logger instance

Bepis 6 years ago
parent
commit
a1664ad223
2 changed files with 26 additions and 9 deletions
  1. 13 2
      BepInEx/Contract/BaseUnityPlugin.cs
  2. 13 7
      BepInEx/Logging/Logger.cs

+ 13 - 2
BepInEx/Contract/BaseUnityPlugin.cs

@@ -1,9 +1,20 @@
-using UnityEngine;
+using BepInEx.Logging;
+using UnityEngine;
 
 namespace BepInEx
 {
 	/// <summary>
 	/// The base plugin type that is used by the BepInEx plugin loader.
 	/// </summary>
-	public abstract class BaseUnityPlugin : MonoBehaviour { }
+	public abstract class BaseUnityPlugin : MonoBehaviour
+	{
+		protected ManualLogSource Logger { get; }
+
+		protected BaseUnityPlugin()
+		{
+			var metadata = MetadataHelper.GetMetadata(this);
+
+			Logger = BepInEx.Logger.CreateLogSource(metadata.Name);
+		}
+	}
 }

+ 13 - 7
BepInEx/Logging/Logger.cs

@@ -9,14 +9,11 @@ namespace BepInEx
 	/// </summary>
 	public static class Logger
 	{
-		private static readonly ManualLogSource InternalLogSource = new ManualLogSource("BepInEx");
-
 		public static ICollection<ILogListener> Listeners { get; } = new List<ILogListener>();
 
-		public static ICollection<ILogSource> Sources { get; } = new LogSourceCollection()
-		{
-			InternalLogSource
-		};
+		public static ICollection<ILogSource> Sources { get; } = new LogSourceCollection();
+
+		private static readonly ManualLogSource InternalLogSource = CreateLogSource("BepInEx");
 
 		private static void InternalLogEvent(object sender, LogEventArgs eventArgs)
 		{
@@ -33,7 +30,7 @@ namespace BepInEx
 		/// <param name="entry">The textual value of the entry.</param>
 		public static void Log(LogLevel level, object data)
 		{
-			InternalLogEvent(InternalLogSource, new LogEventArgs(data, level, InternalLogSource));
+			InternalLogSource.Log(level, data);
 		}
 
 		public static void LogFatal(object data) => Log(LogLevel.Fatal, data);
@@ -43,6 +40,15 @@ namespace BepInEx
 		public static void LogInfo(object data) => Log(LogLevel.Info, data);
 		public 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>
 		{