Ver código fonte

Basic port of old unity logging system

Bepis 6 anos atrás
pai
commit
08446dca33

+ 4 - 2
BepInEx/BepInEx.csproj

@@ -66,13 +66,15 @@
     <Compile Include="ConsoleUtil\SafeConsole.cs" />
     <Compile Include="Bootstrap\Chainloader.cs" />
     <Compile Include="Contract\BaseUnityPlugin.cs" />
-    <Compile Include="Logger.cs" />
+    <Compile Include="Deprecated\BepInLogger.cs" />
     <Compile Include="Logger\BaseLogger.cs" />
     <Compile Include="Logger\LogLevel.cs" />
-    <Compile Include="Logger\PreloaderTextWriter.cs" />
+    <Compile Include="Logger\PreloaderLogWriter.cs" />
+    <Compile Include="Logger\UnityLogWriter.cs" />
     <Compile Include="Properties\AssemblyInfo.cs" />
     <Compile Include="Bootstrap\TypeLoader.cs" />
   </ItemGroup>
+  <ItemGroup />
   <Import Project="..\BepInEx.Common\BepInEx.Common.projitems" Label="Shared" />
   <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
 </Project>

+ 3 - 2
BepInEx/Bootstrap/Preloader.cs

@@ -34,7 +34,7 @@ namespace BepInEx.Bootstrap
 
         #endregion
 
-        public static PreloaderTextWriter PreloaderLog { get; private set; }
+        public static PreloaderLogWriter PreloaderLog { get; private set; }
 
         public static Dictionary<string, IList<AssemblyPatcherDelegate>> PatcherDictionary = new Dictionary<string, IList<AssemblyPatcherDelegate>>(StringComparer.OrdinalIgnoreCase);
 
@@ -57,7 +57,7 @@ namespace BepInEx.Bootstrap
         {
             try
             {
-                PreloaderLog = new PreloaderTextWriter();
+                PreloaderLog = new PreloaderLogWriter();
 
                 PreloaderLog.WriteLine($"BepInEx {Assembly.GetExecutingAssembly().GetName().Version}");
                 PreloaderLog.Log(LogLevel.Message, "Preloader started");
@@ -161,6 +161,7 @@ namespace BepInEx.Bootstrap
                 catch (Exception ex)
                 {
                     PreloaderLog.Log(LogLevel.Warning, $"Could not load patcher methods from {assembly.GetName().Name}");
+                    PreloaderLog.Log(LogLevel.Warning, $"{ex}");
                 }
             }
 

+ 1 - 9
BepInEx/Logger.cs

@@ -7,6 +7,7 @@ namespace BepInEx
     /// <summary>
     /// A helper class to use for logging.
     /// </summary>
+    [Obsolete("This class has been deprecated; please use the Logger static class and BaseLogger implementations", true)]
     public static class BepInLogger
     {
         /// <summary>
@@ -58,13 +59,4 @@ namespace BepInEx
             EntryLogged?.Invoke(entry, show);
         }
     }
-}
-
-namespace UnityEngine
-{
-    internal sealed class UnityLogWriter
-    {
-        [MethodImpl(MethodImplOptions.InternalCall)]
-        public static extern void WriteStringToUnityLog(string s);
-    }
 }

+ 23 - 1
BepInEx/Logger/BaseLogger.cs

@@ -10,9 +10,31 @@ namespace BepInEx.Logger
     {
         public override Encoding Encoding { get; } = new UTF8Encoding(true);
 
+
+        /// <summary>
+        /// The handler for a entry logged event.
+        /// </summary>
+        /// <param name="entry">The text element of the log itself.</param>
+        /// <param name="show">Whether or not it should be dislpayed to the user.</param>
+        public delegate void EntryLoggedEventHandler(LogLevel level, object entry);
+
+        /// <summary>
+        /// The listener event for an entry being logged.
+        /// </summary>
+        public static event EntryLoggedEventHandler EntryLogged;
+
+        
+        public LogLevel DisplayedLevels = LogLevel.All;
+
+
+
         public virtual void Log(LogLevel level, object entry)
         {
-            WriteLine($"[{level}] {entry}");
+            if ((DisplayedLevels & level) != LogLevel.None)
+            {
+                EntryLogged?.Invoke(level, entry);
+                WriteLine($"[{level}] {entry}");
+            }
         }
 
         public virtual void Log(object entry)

+ 2 - 4
BepInEx/Logger/PreloaderTextWriter.cs

@@ -5,10 +5,8 @@ using System.Text;
 
 namespace BepInEx.Logger
 {
-    public class PreloaderTextWriter : BaseLogger
+    public class PreloaderLogWriter : BaseLogger
     {
-        public override Encoding Encoding { get; } = new UTF8Encoding(true);
-
         public StringBuilder StringBuilder = new StringBuilder();
 
         protected TextWriter stdout;
@@ -26,7 +24,7 @@ namespace BepInEx.Logger
             }
         }
 
-        public PreloaderTextWriter()
+        public PreloaderLogWriter()
         {
             stdout = Console.Out;
             traceListener = new TextWriterTraceListener(this, "Preloader");

+ 30 - 0
BepInEx/Logger/UnityLogWriter.cs

@@ -0,0 +1,30 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Runtime.CompilerServices;
+using System.Text;
+
+namespace BepInEx.Logger
+{
+    public class UnityLogWriter : BaseLogger
+    {
+        public void WriteToUnity(string value)
+        {
+            Console.Write(value);
+            UnityEngine.UnityLogWriter.WriteStringToUnityLog(value);
+        }
+
+        public override void WriteLine(string value) => WriteToUnity($"{value}\r\n");
+        public override void Write(char value) => WriteToUnity(value.ToString());
+        public override void Write(string value) => WriteToUnity(value);
+    }
+}
+
+namespace UnityEngine
+{
+    internal sealed class UnityLogWriter
+    {
+        [MethodImpl(MethodImplOptions.InternalCall)]
+        public static extern void WriteStringToUnityLog(string s);
+    }
+}