/ src / Ryujinx.UI.Common / Configuration / LoggerModule.cs
LoggerModule.cs
  1  using Ryujinx.Common;
  2  using Ryujinx.Common.Configuration;
  3  using Ryujinx.Common.Logging;
  4  using Ryujinx.Common.Logging.Targets;
  5  using System;
  6  using System.IO;
  7  
  8  namespace Ryujinx.UI.Common.Configuration
  9  {
 10      public static class LoggerModule
 11      {
 12          public static void Initialize()
 13          {
 14              ConfigurationState.Instance.Logger.EnableDebug.Event += ReloadEnableDebug;
 15              ConfigurationState.Instance.Logger.EnableStub.Event += ReloadEnableStub;
 16              ConfigurationState.Instance.Logger.EnableInfo.Event += ReloadEnableInfo;
 17              ConfigurationState.Instance.Logger.EnableWarn.Event += ReloadEnableWarning;
 18              ConfigurationState.Instance.Logger.EnableError.Event += ReloadEnableError;
 19              ConfigurationState.Instance.Logger.EnableTrace.Event += ReloadEnableTrace;
 20              ConfigurationState.Instance.Logger.EnableGuest.Event += ReloadEnableGuest;
 21              ConfigurationState.Instance.Logger.EnableFsAccessLog.Event += ReloadEnableFsAccessLog;
 22              ConfigurationState.Instance.Logger.FilteredClasses.Event += ReloadFilteredClasses;
 23              ConfigurationState.Instance.Logger.EnableFileLog.Event += ReloadFileLogger;
 24          }
 25  
 26          private static void ReloadEnableDebug(object sender, ReactiveEventArgs<bool> e)
 27          {
 28              Logger.SetEnable(LogLevel.Debug, e.NewValue);
 29          }
 30  
 31          private static void ReloadEnableStub(object sender, ReactiveEventArgs<bool> e)
 32          {
 33              Logger.SetEnable(LogLevel.Stub, e.NewValue);
 34          }
 35  
 36          private static void ReloadEnableInfo(object sender, ReactiveEventArgs<bool> e)
 37          {
 38              Logger.SetEnable(LogLevel.Info, e.NewValue);
 39          }
 40  
 41          private static void ReloadEnableWarning(object sender, ReactiveEventArgs<bool> e)
 42          {
 43              Logger.SetEnable(LogLevel.Warning, e.NewValue);
 44          }
 45  
 46          private static void ReloadEnableError(object sender, ReactiveEventArgs<bool> e)
 47          {
 48              Logger.SetEnable(LogLevel.Error, e.NewValue);
 49          }
 50  
 51          private static void ReloadEnableTrace(object sender, ReactiveEventArgs<bool> e)
 52          {
 53              Logger.SetEnable(LogLevel.Trace, e.NewValue);
 54          }
 55  
 56          private static void ReloadEnableGuest(object sender, ReactiveEventArgs<bool> e)
 57          {
 58              Logger.SetEnable(LogLevel.Guest, e.NewValue);
 59          }
 60  
 61          private static void ReloadEnableFsAccessLog(object sender, ReactiveEventArgs<bool> e)
 62          {
 63              Logger.SetEnable(LogLevel.AccessLog, e.NewValue);
 64          }
 65  
 66          private static void ReloadFilteredClasses(object sender, ReactiveEventArgs<LogClass[]> e)
 67          {
 68              bool noFilter = e.NewValue.Length == 0;
 69  
 70              foreach (var logClass in Enum.GetValues<LogClass>())
 71              {
 72                  Logger.SetEnable(logClass, noFilter);
 73              }
 74  
 75              foreach (var logClass in e.NewValue)
 76              {
 77                  Logger.SetEnable(logClass, true);
 78              }
 79          }
 80  
 81          private static void ReloadFileLogger(object sender, ReactiveEventArgs<bool> e)
 82          {
 83              if (e.NewValue)
 84              {
 85                  string logDir = AppDataManager.LogsDirPath;
 86                  FileStream logFile = null;
 87  
 88                  if (!string.IsNullOrEmpty(logDir))
 89                  {
 90                      logFile = FileLogTarget.PrepareLogFile(logDir);
 91                  }
 92  
 93                  if (logFile == null)
 94                  {
 95                      Logger.Error?.Print(LogClass.Application, "No writable log directory available. Make sure either the Logs directory, Application Data, or the Ryujinx directory is writable.");
 96                      Logger.RemoveTarget("file");
 97  
 98                      return;
 99                  }
100  
101                  Logger.AddTarget(new AsyncLogTargetWrapper(
102                      new FileLogTarget("file", logFile),
103                      1000,
104                      AsyncLogTargetOverflowAction.Block
105                  ));
106              }
107              else
108              {
109                  Logger.RemoveTarget("file");
110              }
111          }
112      }
113  }