Logger.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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 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 Notice { get; } // Always enabled
  80. static Logger()
  81. {
  82. m_EnabledClasses = new bool[Enum.GetNames(typeof(LogClass)).Length];
  83. for (int index = 0; index < m_EnabledClasses.Length; index++)
  84. {
  85. m_EnabledClasses[index] = true;
  86. }
  87. m_LogTargets = new List<ILogTarget>();
  88. m_Time = Stopwatch.StartNew();
  89. // Logger should log to console by default
  90. AddTarget(new AsyncLogTargetWrapper(
  91. new ConsoleLogTarget("console"),
  92. 1000,
  93. AsyncLogTargetOverflowAction.Discard));
  94. Notice = new Log(LogLevel.Notice);
  95. // Enable important log levels before configuration is loaded
  96. Error = new Log(LogLevel.Error);
  97. Warning = new Log(LogLevel.Warning);
  98. Info = new Log(LogLevel.Info);
  99. }
  100. public static void RestartTime()
  101. {
  102. m_Time.Restart();
  103. }
  104. private static ILogTarget GetTarget(string targetName)
  105. {
  106. foreach (var target in m_LogTargets)
  107. {
  108. if (target.Name.Equals(targetName))
  109. {
  110. return target;
  111. }
  112. }
  113. return null;
  114. }
  115. public static void AddTarget(ILogTarget target)
  116. {
  117. m_LogTargets.Add(target);
  118. Updated += target.Log;
  119. }
  120. public static void RemoveTarget(string target)
  121. {
  122. ILogTarget logTarget = GetTarget(target);
  123. if (logTarget != null)
  124. {
  125. Updated -= logTarget.Log;
  126. m_LogTargets.Remove(logTarget);
  127. logTarget.Dispose();
  128. }
  129. }
  130. public static void Shutdown()
  131. {
  132. Updated = null;
  133. foreach (var target in m_LogTargets)
  134. {
  135. target.Dispose();
  136. }
  137. m_LogTargets.Clear();
  138. }
  139. public static IReadOnlyCollection<LogLevel> GetEnabledLevels()
  140. {
  141. var logs = new Log?[] { Debug, Info, Warning, Error, Guest, AccessLog, Stub };
  142. List<LogLevel> levels = new List<LogLevel>(logs.Length);
  143. foreach (var log in logs)
  144. {
  145. if (log.HasValue)
  146. {
  147. levels.Add(log.Value.Level);
  148. }
  149. }
  150. return levels;
  151. }
  152. public static void SetEnable(LogLevel logLevel, bool enabled)
  153. {
  154. switch (logLevel)
  155. {
  156. case LogLevel.Debug : Debug = enabled ? new Log(LogLevel.Debug) : new Log?(); break;
  157. case LogLevel.Info : Info = enabled ? new Log(LogLevel.Info) : new Log?(); break;
  158. case LogLevel.Warning : Warning = enabled ? new Log(LogLevel.Warning) : new Log?(); break;
  159. case LogLevel.Error : Error = enabled ? new Log(LogLevel.Error) : new Log?(); break;
  160. case LogLevel.Guest : Guest = enabled ? new Log(LogLevel.Guest) : new Log?(); break;
  161. case LogLevel.AccessLog : AccessLog = enabled ? new Log(LogLevel.AccessLog): new Log?(); break;
  162. case LogLevel.Stub : Stub = enabled ? new Log(LogLevel.Stub) : new Log?(); break;
  163. default: throw new ArgumentException("Unknown Log Level");
  164. }
  165. }
  166. public static void SetEnable(LogClass logClass, bool enabled)
  167. {
  168. m_EnabledClasses[(int)logClass] = enabled;
  169. }
  170. }
  171. }