Logger.cs 7.7 KB

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