Logger.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. using Ryujinx.Common.SystemInterop;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Diagnostics;
  5. using System.Runtime.CompilerServices;
  6. using System.Threading;
  7. namespace Ryujinx.Common.Logging
  8. {
  9. public static class Logger
  10. {
  11. private static readonly Stopwatch m_Time;
  12. private static readonly bool[] m_EnabledClasses;
  13. private static readonly List<ILogTarget> m_LogTargets;
  14. private static readonly StdErrAdapter _stdErrAdapter;
  15. public static event EventHandler<LogEventArgs> Updated;
  16. public readonly struct Log
  17. {
  18. internal readonly LogLevel Level;
  19. internal Log(LogLevel level)
  20. {
  21. Level = level;
  22. }
  23. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  24. public void PrintMsg(LogClass logClass, string message)
  25. {
  26. if (m_EnabledClasses[(int)logClass])
  27. {
  28. Updated?.Invoke(null, new LogEventArgs(Level, m_Time.Elapsed, Thread.CurrentThread.Name, FormatMessage(logClass, "", message)));
  29. }
  30. }
  31. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  32. public void Print(LogClass logClass, string message, [CallerMemberName] string caller = "")
  33. {
  34. if (m_EnabledClasses[(int)logClass])
  35. {
  36. Updated?.Invoke(null, new LogEventArgs(Level, m_Time.Elapsed, Thread.CurrentThread.Name, FormatMessage(logClass, caller, message)));
  37. }
  38. }
  39. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  40. public void Print(LogClass logClass, string message, object data, [CallerMemberName] string caller = "")
  41. {
  42. if (m_EnabledClasses[(int)logClass])
  43. {
  44. Updated?.Invoke(null, new LogEventArgs(Level, m_Time.Elapsed, Thread.CurrentThread.Name, FormatMessage(logClass, caller, message), data));
  45. }
  46. }
  47. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  48. public void PrintStub(LogClass logClass, string message = "", [CallerMemberName] string caller = "")
  49. {
  50. if (m_EnabledClasses[(int)logClass])
  51. {
  52. Updated?.Invoke(null, new LogEventArgs(Level, m_Time.Elapsed, Thread.CurrentThread.Name, FormatMessage(logClass, caller, "Stubbed. " + message)));
  53. }
  54. }
  55. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  56. public void PrintStub(LogClass logClass, object data, [CallerMemberName] string caller = "")
  57. {
  58. if (m_EnabledClasses[(int)logClass])
  59. {
  60. Updated?.Invoke(null, new LogEventArgs(Level, m_Time.Elapsed, Thread.CurrentThread.Name, FormatMessage(logClass, caller, "Stubbed."), data));
  61. }
  62. }
  63. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  64. public void PrintStub(LogClass logClass, string message, object data, [CallerMemberName] string caller = "")
  65. {
  66. if (m_EnabledClasses[(int)logClass])
  67. {
  68. Updated?.Invoke(null, new LogEventArgs(Level, m_Time.Elapsed, Thread.CurrentThread.Name, FormatMessage(logClass, caller, "Stubbed. " + message), data));
  69. }
  70. }
  71. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  72. public void PrintRawMsg(string message)
  73. {
  74. Updated?.Invoke(null, new LogEventArgs(Level, m_Time.Elapsed, Thread.CurrentThread.Name, message));
  75. }
  76. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  77. private static string FormatMessage(LogClass Class, string Caller, string Message) => $"{Class} {Caller}: {Message}";
  78. }
  79. public static Log? Debug { get; private set; }
  80. public static Log? Info { get; private set; }
  81. public static Log? Warning { get; private set; }
  82. public static Log? Error { get; private set; }
  83. public static Log? Guest { get; private set; }
  84. public static Log? AccessLog { get; private set; }
  85. public static Log? Stub { get; private set; }
  86. public static Log? Trace { get; private set; }
  87. public static Log Notice { get; } // Always enabled
  88. static Logger()
  89. {
  90. m_EnabledClasses = new bool[Enum.GetNames<LogClass>().Length];
  91. for (int index = 0; index < m_EnabledClasses.Length; index++)
  92. {
  93. m_EnabledClasses[index] = true;
  94. }
  95. m_LogTargets = new List<ILogTarget>();
  96. m_Time = Stopwatch.StartNew();
  97. // Logger should log to console by default
  98. AddTarget(new AsyncLogTargetWrapper(
  99. new ConsoleLogTarget("console"),
  100. 1000,
  101. AsyncLogTargetOverflowAction.Discard));
  102. Notice = new Log(LogLevel.Notice);
  103. // Enable important log levels before configuration is loaded
  104. Error = new Log(LogLevel.Error);
  105. Warning = new Log(LogLevel.Warning);
  106. Info = new Log(LogLevel.Info);
  107. Trace = new Log(LogLevel.Trace);
  108. _stdErrAdapter = new StdErrAdapter();
  109. }
  110. public static void RestartTime()
  111. {
  112. m_Time.Restart();
  113. }
  114. private static ILogTarget GetTarget(string targetName)
  115. {
  116. foreach (var target in m_LogTargets)
  117. {
  118. if (target.Name.Equals(targetName))
  119. {
  120. return target;
  121. }
  122. }
  123. return null;
  124. }
  125. public static void AddTarget(ILogTarget target)
  126. {
  127. m_LogTargets.Add(target);
  128. Updated += target.Log;
  129. }
  130. public static void RemoveTarget(string target)
  131. {
  132. ILogTarget logTarget = GetTarget(target);
  133. if (logTarget != null)
  134. {
  135. Updated -= logTarget.Log;
  136. m_LogTargets.Remove(logTarget);
  137. logTarget.Dispose();
  138. }
  139. }
  140. public static void Shutdown()
  141. {
  142. Updated = null;
  143. _stdErrAdapter.Dispose();
  144. foreach (var target in m_LogTargets)
  145. {
  146. target.Dispose();
  147. }
  148. m_LogTargets.Clear();
  149. }
  150. public static IReadOnlyCollection<LogLevel> GetEnabledLevels()
  151. {
  152. var logs = new Log?[] { Debug, Info, Warning, Error, Guest, AccessLog, Stub, Trace };
  153. List<LogLevel> levels = new List<LogLevel>(logs.Length);
  154. foreach (var log in logs)
  155. {
  156. if (log.HasValue)
  157. {
  158. levels.Add(log.Value.Level);
  159. }
  160. }
  161. return levels;
  162. }
  163. public static void SetEnable(LogLevel logLevel, bool enabled)
  164. {
  165. switch (logLevel)
  166. {
  167. case LogLevel.Debug : Debug = enabled ? new Log(LogLevel.Debug) : new Log?(); break;
  168. case LogLevel.Info : Info = enabled ? new Log(LogLevel.Info) : new Log?(); break;
  169. case LogLevel.Warning : Warning = enabled ? new Log(LogLevel.Warning) : new Log?(); break;
  170. case LogLevel.Error : Error = enabled ? new Log(LogLevel.Error) : new Log?(); break;
  171. case LogLevel.Guest : Guest = enabled ? new Log(LogLevel.Guest) : new Log?(); break;
  172. case LogLevel.AccessLog : AccessLog = enabled ? new Log(LogLevel.AccessLog): new Log?(); break;
  173. case LogLevel.Stub : Stub = enabled ? new Log(LogLevel.Stub) : new Log?(); break;
  174. case LogLevel.Trace : Trace = enabled ? new Log(LogLevel.Trace) : new Log?(); break;
  175. default: throw new ArgumentException("Unknown Log Level");
  176. }
  177. }
  178. public static void SetEnable(LogClass logClass, bool enabled)
  179. {
  180. m_EnabledClasses[(int)logClass] = enabled;
  181. }
  182. }
  183. }