Forráskód Böngészése

better debuggin capabilities

Scrublord1336 6 éve
szülő
commit
8d0dd65ac5

+ 15 - 14
src/XUnity.AutoTranslator.Plugin.Core/AutoTranslationPlugin.cs

@@ -89,13 +89,12 @@ namespace XUnity.AutoTranslator.Plugin.Core
       public void Initialize()
       {
          Current = this;
+         Logger.Current = new ConsoleLogger();
 
          Settings.Configure();
 
          if( Settings.EnableConsole ) DebugConsole.Enable();
 
-         Console.WriteLine( "[XUnity.AutoTranslator][INFO]: Initializing XUnity.AutoTranslator." );
-
          HooksSetup.InstallHooks( Override_TextChanged );
 
          try
@@ -104,7 +103,7 @@ namespace XUnity.AutoTranslator.Plugin.Core
          }
          catch( Exception e )
          {
-            Console.WriteLine( "[XUnity.AutoTranslator][ERROR]: An unexpected error occurred during initialization of endpoint." + Environment.NewLine + e );
+            Logger.Current.Error( e, "An unexpected error occurred during initialization of endpoint." );
          }
 
          _symbolCheck = TextHelper.GetSymbolCheck( Settings.FromLanguage );
@@ -142,7 +141,7 @@ namespace XUnity.AutoTranslator.Plugin.Core
             }
             catch( Exception e )
             {
-               Console.WriteLine( "[XUnity.AutoTranslator][ERROR]: An unexpected error occurred while removing GC'ed resources." + Environment.NewLine + e );
+               Logger.Current.Error( e, "An unexpected error occurred while removing GC'ed resources." );
             }
 
             Thread.Sleep( 1000 * 60 );
@@ -182,7 +181,7 @@ namespace XUnity.AutoTranslator.Plugin.Core
          }
          catch( Exception e )
          {
-            Console.WriteLine( "[XUnity.AutoTranslator][ERROR]: An error occurred while saving translations to disk. " + Environment.NewLine + e );
+            Logger.Current.Error( e, "An error occurred while saving translations to disk."  );
          }
       }
 
@@ -223,7 +222,7 @@ namespace XUnity.AutoTranslator.Plugin.Core
          }
          catch( Exception e )
          {
-            Console.WriteLine( "[XUnity.AutoTranslator][ERROR]: An error occurred while loading translations. " + Environment.NewLine + e );
+            Logger.Current.Error( e, "An error occurred while loading translations." );
          }
       }
 
@@ -242,6 +241,8 @@ namespace XUnity.AutoTranslator.Plugin.Core
             }
          }
 
+         Logger.Current.Debug( "Queued translation for: " + key.GetDictionaryLookupKey() );
+
          job = new TranslationJob( key );
          _unstartedJobs.Add( key.GetDictionaryLookupKey(), job );
 
@@ -258,7 +259,7 @@ namespace XUnity.AutoTranslator.Plugin.Core
             _completedJobs.Clear();
             Settings.IsShutdown = true;
 
-            Console.WriteLine( $"[XUnity.AutoTranslator][ERROR]: SPAM DETECTED: More than {Settings.MaxUnstartedJobs} queued for translations due to unknown reasons. Shutting down plugin." );
+            Logger.Current.Error( $"SPAM DETECTED: More than {Settings.MaxUnstartedJobs} queued for translations due to unknown reasons. Shutting down plugin." );
          }
 
          var previousIdx = ( (int)( Time.time - Time.deltaTime ) ) % Settings.TranslationQueueWatchWindow;
@@ -284,7 +285,7 @@ namespace XUnity.AutoTranslator.Plugin.Core
                _completedJobs.Clear();
                Settings.IsShutdown = true;
 
-               Console.WriteLine( $"[XUnity.AutoTranslator][ERROR]: SPAM DETECTED: More than {Settings.MaxTranslationsQueuedPerSecond} translations per seconds queued for a {Settings.MaxSecondsAboveTranslationThreshold} second period. Shutting down plugin." );
+               Logger.Current.Error( $"SPAM DETECTED: More than {Settings.MaxTranslationsQueuedPerSecond} translations per seconds queued for a {Settings.MaxSecondsAboveTranslationThreshold} second period. Shutting down plugin." );
             }
          }
          else
@@ -427,7 +428,7 @@ namespace XUnity.AutoTranslator.Plugin.Core
             }
             catch( Exception e )
             {
-               Console.WriteLine( "[XUnity.AutoTranslator][ERROR]: An error occurred while setting text on a component." + Environment.NewLine + e );
+               Logger.Current.Error( e, "An error occurred while setting text on a component." );
             }
             finally
             {
@@ -715,7 +716,7 @@ namespace XUnity.AutoTranslator.Plugin.Core
          }
          catch( Exception e )
          {
-            Console.WriteLine( "[XUnity.AutoTranslator][ERROR]: An error occurred in Update callback. " + Environment.NewLine + e );
+            Logger.Current.Error( e, "An error occurred in Update callback. " );
          }
       }
 
@@ -746,7 +747,7 @@ namespace XUnity.AutoTranslator.Plugin.Core
                   if( Settings.TranslationCount > Settings.MaxTranslationsBeforeShutdown )
                   {
                      Settings.IsShutdown = true;
-                     Console.WriteLine( $"[XUnity.AutoTranslator][ERROR]: Maximum translations ({Settings.MaxTranslationsBeforeShutdown}) per session reached. Shutting plugin down." );
+                     Logger.Current.Error( $"Maximum translations ({Settings.MaxTranslationsBeforeShutdown}) per session reached. Shutting plugin down." );
                   }
                }
 
@@ -776,13 +777,13 @@ namespace XUnity.AutoTranslator.Plugin.Core
                   {
                      if( _endpoint.ShouldGetSecondChanceAfterFailure() )
                      {
-                        Console.WriteLine( $"[XUnity.AutoTranslator][WARN]: More than {Settings.MaxErrors} consecutive errors occurred. Entering fallback mode." );
+                        Logger.Current.Warn( $"More than {Settings.MaxErrors} consecutive errors occurred. Entering fallback mode." );
                         _consecutiveErrors = 0;
                      }
                      else
                      {
                         Settings.IsShutdown = true;
-                        Console.WriteLine( $"[XUnity.AutoTranslator][ERROR]: More than {Settings.MaxErrors} consecutive errors occurred. Shutting down plugin." );
+                        Logger.Current.Error( $"More than {Settings.MaxErrors} consecutive errors occurred. Shutting down plugin." );
                      }
                   }
                }
@@ -952,7 +953,7 @@ namespace XUnity.AutoTranslator.Plugin.Core
             }
             catch( Exception e )
             {
-               Console.WriteLine( "[XUnity.AutoTranslator][ERROR]: An error while copying text to clipboard. " + Environment.NewLine + e );
+               Logger.Current.Error( e, "An error while copying text to clipboard." );
             }
             finally
             {

+ 3 - 4
src/XUnity.AutoTranslator.Plugin.Core/Configuration/Settings.cs

@@ -32,6 +32,7 @@ namespace XUnity.AutoTranslator.Plugin.Core.Configuration
       public static int MaxCharactersPerTranslation;
       public static bool EnablePrintHierarchy;
       public static bool EnableConsole;
+      public static bool EnableDebugLogs;
       public static string AutoTranslationsFilePath;
       public static bool EnableIMGUI;
       public static bool EnableUGUI;
@@ -65,10 +66,7 @@ namespace XUnity.AutoTranslator.Plugin.Core.Configuration
             Config.Current.Preferences.DeleteSection( "AutoTranslator" );
             Config.Current.Preferences[ "Service" ].DeleteKey( "EnableSSL" );
          }
-         catch( Exception e )
-         {
-            Console.WriteLine( "[XUnity.AutoTranslator][ERROR]: An error occurred while removing legacy configuration. " + Environment.NewLine + e );
-         }
+         catch { }
 
 
 
@@ -105,6 +103,7 @@ namespace XUnity.AutoTranslator.Plugin.Core.Configuration
 
          EnablePrintHierarchy = Config.Current.Preferences[ "Debug" ][ "EnablePrintHierarchy" ].GetOrDefault( false );
          EnableConsole = Config.Current.Preferences[ "Debug" ][ "EnableConsole" ].GetOrDefault( false );
+         EnableDebugLogs = Config.Current.Preferences[ "Debug" ][ "EnableLog" ].GetOrDefault( false );
 
          AutoTranslationsFilePath = Path.Combine( Config.Current.DataPath, OutputFile.Replace( "{lang}", Language ) );
 

+ 12 - 0
src/XUnity.AutoTranslator.Plugin.Core/ConsoleLogger.cs

@@ -0,0 +1,12 @@
+using System;
+
+namespace XUnity.AutoTranslator.Plugin.Core
+{
+   public class ConsoleLogger : Logger
+   {
+      protected override void Write( string formattedMessage )
+      {
+         Console.WriteLine( formattedMessage );
+      }
+   }
+}

+ 4 - 4
src/XUnity.AutoTranslator.Plugin.Core/Hooks/HooksSetup.cs

@@ -37,7 +37,7 @@ namespace XUnity.AutoTranslator.Plugin.Core.Hooks
          }
          catch( Exception e )
          {
-            Console.WriteLine( "[XUnity.AutoTranslator][ERROR]: An error occurred while setting up hooks for UGUI. " + Environment.NewLine + e );
+            Logger.Current.Error( e, "An error occurred while setting up hooks for UGUI."  );
          }
 
          try
@@ -53,7 +53,7 @@ namespace XUnity.AutoTranslator.Plugin.Core.Hooks
          }
          catch( Exception e )
          {
-            Console.WriteLine( "[XUnity.AutoTranslator][ERROR]: An error occurred while setting up hooks for TextMeshPro. " + Environment.NewLine + e );
+            Logger.Current.Error( e, "An error occurred while setting up hooks for TextMeshPro." );
          }
 
          try
@@ -69,7 +69,7 @@ namespace XUnity.AutoTranslator.Plugin.Core.Hooks
          }
          catch( Exception e )
          {
-            Console.WriteLine( "[XUnity.AutoTranslator][ERROR]: An error occurred while setting up hooks for NGUI. " + Environment.NewLine + e );
+            Logger.Current.Error( e, "An error occurred while setting up hooks for NGUI." );
          }
 
          try
@@ -92,7 +92,7 @@ namespace XUnity.AutoTranslator.Plugin.Core.Hooks
          }
          catch( Exception e )
          {
-            Console.WriteLine( "[XUnity.AutoTranslator][ERROR]: An error occurred while setting up hooks for IMGUI. " + Environment.NewLine + e );
+            Logger.Current.Error( e, "An error occurred while setting up hooks for IMGUI." );
          }
       }
 

+ 61 - 0
src/XUnity.AutoTranslator.Plugin.Core/Logger.cs

@@ -0,0 +1,61 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using XUnity.AutoTranslator.Plugin.Core.Configuration;
+
+namespace XUnity.AutoTranslator.Plugin.Core
+{
+   public abstract class Logger
+   {
+      public static Logger Current;
+
+      public void Error( Exception e, string message )
+      {
+         Write( $"[XUnity.AutoTranslator][ERROR]: {message}" + Environment.NewLine + e );
+      }
+
+      public void Error( string message )
+      {
+         Write( $"[XUnity.AutoTranslator][ERROR]: {message}" );
+      }
+
+      public void Warn( Exception e, string message )
+      {
+         Write( $"[XUnity.AutoTranslator][WARN]: {message}" + Environment.NewLine + e );
+      }
+
+      public void Warn( string message )
+      {
+         Write( $"[XUnity.AutoTranslator][WARN]: {message}" );
+      }
+
+      public void Info( Exception e, string message )
+      {
+         Write( $"[XUnity.AutoTranslator][INFO]: {message}" + Environment.NewLine + e );
+      }
+
+      public void Info( string message )
+      {
+         Write( $"[XUnity.AutoTranslator][INFO]: {message}" );
+      }
+
+      public void Debug( Exception e, string message )
+      {
+         if( Settings.EnableDebugLogs )
+         {
+            Write( $"[XUnity.AutoTranslator][DEBUG]: {message}" + Environment.NewLine + e );
+         }
+      }
+
+      public void Debug( string message )
+      {
+         if( Settings.EnableDebugLogs )
+         {
+            Write( $"[XUnity.AutoTranslator][DEBUG]: {message}" );
+         }
+      }
+
+      protected abstract void Write( string formattedMessage );
+   }
+}

+ 1 - 1
src/XUnity.AutoTranslator.Plugin.Core/Web/GoogleTranslateEndpoint.cs

@@ -111,7 +111,7 @@ namespace XUnity.AutoTranslator.Plugin.Core.Web
 
          if( error != null )
          {
-            Console.WriteLine( "[XUnity.AutoTranslator][ERROR]: An error occurred while setting up GoogleTranslate Cookie/TKK." + Environment.NewLine + error );
+            Logger.Current.Error( "An error occurred while setting up GoogleTranslate Cookie/TKK." + Environment.NewLine + error );
          }
       }
 

+ 44 - 18
src/XUnity.AutoTranslator.Plugin.Core/Web/KnownHttpEndpoint.cs

@@ -45,37 +45,63 @@ namespace XUnity.AutoTranslator.Plugin.Core.Web
 
          var client = GetClient();
          var url = GetServiceUrl( untranslatedText, from, to );
-         ApplyHeaders( client.Headers );
-         var result = client.GetDownloadResult( new Uri( url ) );
+         Logger.Current.Debug( "Starting translation for: " + untranslatedText );
+
+         DownloadResult result = null;
          try
          {
-            _runningTranslations++;
-            yield return result;
-            _runningTranslations--;
+            ApplyHeaders( client.Headers );
+            result = client.GetDownloadResult( new Uri( url ) );
+         }
+         catch( Exception e )
+         {
+            Logger.Current.Error( e, "Error occurred while retrieving translation." );
+         }
 
-            if( result.Succeeded )
+         if( result != null )
+         {
+            try
             {
-               if( TryExtractTranslated( result.Result, out var translatedText ) )
+               Logger.Current.Debug( "Yielding for translation." );
+               _runningTranslations++;
+               yield return result;
+               _runningTranslations--;
+               Logger.Current.Debug( "Yield completed." );
+
+               try
                {
-                  translatedText = translatedText ?? string.Empty;
-                  success( translatedText );
+                  if( result.Succeeded )
+                  {
+                     if( TryExtractTranslated( result.Result, out var translatedText ) )
+                     {
+                        Logger.Current.Debug( $"Translation for '{untranslatedText}' succeded. Result: {translatedText}" );
+
+                        translatedText = translatedText ?? string.Empty;
+                        success( translatedText );
+                     }
+                     else
+                     {
+                        Logger.Current.Error( "Error occurred while extracting translation." );
+                        failure();
+                     }
+                  }
+                  else
+                  {
+                     Logger.Current.Error( "Error occurred while retrieving translation." + Environment.NewLine + result.Error );
+                     failure();
+                  }
                }
-               else
+               catch( Exception e )
                {
-                  Console.WriteLine( "[XUnity.AutoTranslator][ERROR]: Error occurred while extracting translation." );
+                  Logger.Current.Error( e, "Error occurred while retrieving translation." );
                   failure();
                }
             }
-            else
+            finally
             {
-               Console.WriteLine( "[XUnity.AutoTranslator][ERROR]: Error occurred while retrieving translation." + Environment.NewLine + result.Error );
-               failure();
+               _clientLastUse = DateTime.UtcNow;
             }
          }
-         finally
-         {
-            _clientLastUse = DateTime.UtcNow;
-         }
       }
 
       public virtual void OnUpdate()