فهرست منبع

Merge remote-tracking branch 'origin/feature/bepin_logger' into release/v2.12.0

Scrublord1336 6 سال پیش
والد
کامیت
3f505ca9e4

+ 1 - 0
src/XUnity.AutoTranslator.Plugin.BepIn/AutoTranslatorPlugin.cs

@@ -22,6 +22,7 @@ namespace XUnity.AutoTranslator.Plugin.BepIn
       {
          _dataFolder = "BepInEx";
          _configPath = Path.Combine( _dataFolder, "AutoTranslatorConfig.ini" );
+         Logger.Current = new BepInLogger();
       }
 
       public IniFile Preferences

+ 16 - 0
src/XUnity.AutoTranslator.Plugin.BepIn/BepInLogger.cs

@@ -0,0 +1,16 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using XUnity.AutoTranslator.Plugin.Core;
+
+namespace XUnity.AutoTranslator.Plugin.BepIn
+{
+   public class BepInLogger : Logger
+   {
+      protected override void Log( LogLevel level, string message )
+      {
+         BepInEx.BepInLogger.Log( "[XUnity.AutoTranslator] " + message );
+      }
+   }
+}

+ 4 - 1
src/XUnity.AutoTranslator.Plugin.Core/AutoTranslationPlugin.cs

@@ -110,7 +110,10 @@ namespace XUnity.AutoTranslator.Plugin.Core
       public void Initialize()
       {
          Current = this;
-         Logger.Current = new ConsoleLogger();
+         if( Logger.Current == null )
+         {
+            Logger.Current = new ConsoleLogger();
+         }
 
          Settings.Configure();
 

+ 2 - 2
src/XUnity.AutoTranslator.Plugin.Core/ConsoleLogger.cs

@@ -4,9 +4,9 @@ namespace XUnity.AutoTranslator.Plugin.Core
 {
    public class ConsoleLogger : Logger
    {
-      protected override void Write( string formattedMessage )
+      protected override void Log( LogLevel level, string message )
       {
-         Console.WriteLine( formattedMessage );
+         Console.WriteLine( $"{GetPrefix( level )} {message}" );
       }
    }
 }

+ 10 - 0
src/XUnity.AutoTranslator.Plugin.Core/LogLevel.cs

@@ -0,0 +1,10 @@
+namespace XUnity.AutoTranslator.Plugin.Core
+{
+   public enum LogLevel
+   {
+      Debug,
+      Info,
+      Warn,
+      Error
+   }
+}

+ 26 - 9
src/XUnity.AutoTranslator.Plugin.Core/Logger.cs

@@ -12,39 +12,39 @@ namespace XUnity.AutoTranslator.Plugin.Core
 
       public void Error( Exception e, string message )
       {
-         Write( $"[XUnity.AutoTranslator][ERROR]: {message}" + Environment.NewLine + e );
+         Log( LogLevel.Error, message + Environment.NewLine + e );
       }
 
       public void Error( string message )
       {
-         Write( $"[XUnity.AutoTranslator][ERROR]: {message}" );
+         Log( LogLevel.Error, message );
       }
 
       public void Warn( Exception e, string message )
       {
-         Write( $"[XUnity.AutoTranslator][WARN]: {message}" + Environment.NewLine + e );
+         Log( LogLevel.Warn, message + Environment.NewLine + e );
       }
 
       public void Warn( string message )
       {
-         Write( $"[XUnity.AutoTranslator][WARN]: {message}" );
+         Log( LogLevel.Warn, message );
       }
 
       public void Info( Exception e, string message )
       {
-         Write( $"[XUnity.AutoTranslator][INFO]: {message}" + Environment.NewLine + e );
+         Log( LogLevel.Info, message + Environment.NewLine + e );
       }
 
       public void Info( string message )
       {
-         Write( $"[XUnity.AutoTranslator][INFO]: {message}" );
+         Log( LogLevel.Info, message );
       }
 
       public void Debug( Exception e, string message )
       {
          if( Settings.EnableDebugLogs )
          {
-            Write( $"[XUnity.AutoTranslator][DEBUG]: {message}" + Environment.NewLine + e );
+            Log( LogLevel.Debug, message + Environment.NewLine + e );
          }
       }
 
@@ -52,10 +52,27 @@ namespace XUnity.AutoTranslator.Plugin.Core
       {
          if( Settings.EnableDebugLogs )
          {
-            Write( $"[XUnity.AutoTranslator][DEBUG]: {message}" );
+            Log( LogLevel.Debug, message );
          }
       }
 
-      protected abstract void Write( string formattedMessage );
+      protected abstract void Log( LogLevel level, string message );
+
+      protected string GetPrefix( LogLevel level )
+      {
+         switch( level )
+         {
+            case LogLevel.Debug:
+               return "[XUnity.AutoTranslator][DEBUG]: ";
+            case LogLevel.Info:
+               return "[XUnity.AutoTranslator][INFO]: ";
+            case LogLevel.Warn:
+               return "[XUnity.AutoTranslator][WARN]: ";
+            case LogLevel.Error:
+               return "[XUnity.AutoTranslator][ERROR]: ";
+            default:
+               return "[XUnity.AutoTranslator][UNKNOW]: ";
+         }
+      }
    }
 }