소스 검색

Add config entry for logging patcher console output

Bepis 6 년 전
부모
커밋
c08aa97f30
5개의 변경된 파일32개의 추가작업 그리고 16개의 파일을 삭제
  1. 15 1
      BepInEx/Bootstrap/Preloader.cs
  2. 1 4
      BepInEx/Logger/BaseLogger.cs
  3. 0 4
      BepInEx/Logger/LogLevel.cs
  4. 1 1
      BepInEx/Logger/LoggerTraceListener.cs
  5. 15 6
      BepInEx/Logger/PreloaderLogWriter.cs

+ 15 - 1
BepInEx/Bootstrap/Preloader.cs

@@ -53,6 +53,20 @@ namespace BepInEx.Bootstrap
             }
         }
 
+        private static bool TryGetConfigBool(string key, string defaultValue)
+        {
+            try
+            {
+                string result = Config.GetEntry(key, defaultValue);
+
+                return bool.Parse(result);
+            }
+            catch
+            {
+                return false;
+            }
+        }
+
         public static void Main(string[] args)
         {
             try
@@ -61,7 +75,7 @@ namespace BepInEx.Bootstrap
                 ExecutablePath = args[0];
 
 
-                PreloaderLog = new PreloaderLogWriter();
+                PreloaderLog = new PreloaderLogWriter(TryGetConfigBool("preloader-logconsole", "false"));
                 PreloaderLog.Enabled = true;
 
                 PreloaderLog.WriteLine($"BepInEx {Assembly.GetExecutingAssembly().GetName().Version}");

+ 1 - 4
BepInEx/Logger/BaseLogger.cs

@@ -1,7 +1,4 @@
-using System;
-using System.Collections.Generic;
-using System.IO;
-using System.Linq;
+using System.IO;
 using System.Text;
 
 namespace BepInEx.Logger

+ 0 - 4
BepInEx/Logger/LogLevel.cs

@@ -1,7 +1,4 @@
 using System;
-using System.Collections.Generic;
-using System.Linq;
-using System.Text;
 
 namespace BepInEx.Logger
 {
@@ -15,7 +12,6 @@ namespace BepInEx.Logger
         Message = 8,
         Info = 16,
         Debug = 32,
-
         All = Fatal | Error | Warning | Message | Info | Debug
     }
 }

+ 1 - 1
BepInEx/Logger/LoggerTraceListener.cs

@@ -95,7 +95,7 @@ namespace BepInEx.Logger
 
                 instance.Patch(
                     typeof(Trace).GetMethod("DoTrace", BindingFlags.Static | BindingFlags.NonPublic),
-                    new HarmonyMethod(typeof(TraceFixer).GetMethod("DoTraceReplacement", BindingFlags.Static | BindingFlags.Public)),
+                    new HarmonyMethod(typeof(TraceFixer).GetMethod(nameof(DoTraceReplacement), BindingFlags.Static | BindingFlags.Public)),
                     null);
             }
 

+ 15 - 6
BepInEx/Logger/PreloaderLogWriter.cs

@@ -7,7 +7,9 @@ namespace BepInEx.Logger
 {
     public class PreloaderLogWriter : BaseLogger
     {
-        public StringBuilder StringBuilder = new StringBuilder();
+        public StringBuilder StringBuilder { get; protected set; } = new StringBuilder();
+
+        public bool IsRedirectingConsole { get; protected set; }
 
         protected TextWriter stdout;
         protected LoggerTraceListener traceListener;
@@ -24,9 +26,13 @@ namespace BepInEx.Logger
             }
         }
 
-        public PreloaderLogWriter()
+        public PreloaderLogWriter(bool redirectConsole)
         {
-            stdout = Console.Out;
+            IsRedirectingConsole = redirectConsole;
+
+            if (IsRedirectingConsole)
+                stdout = Console.Out;
+
             traceListener = new LoggerTraceListener(this);
         }
 
@@ -35,7 +41,9 @@ namespace BepInEx.Logger
             if (Enabled)
                 return;
 
-            Console.SetOut(this);
+            if (IsRedirectingConsole)
+                Console.SetOut(this);
+
             Trace.Listeners.Add(traceListener);
 
             _enabled = true;
@@ -46,7 +54,9 @@ namespace BepInEx.Logger
             if (!Enabled)
                 return;
 
-            Console.SetOut(stdout);
+            if (IsRedirectingConsole)
+                Console.SetOut(stdout);
+
             Trace.Listeners.Remove(traceListener);
 
             _enabled = false;
@@ -73,7 +83,6 @@ namespace BepInEx.Logger
         public override string ToString()
         {
             return StringBuilder.ToString().Trim();
-
         }
     }
 }