Logger.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 Stopwatch m_Time;
  11. private static readonly bool[] m_EnabledLevels;
  12. private static readonly bool[] m_EnabledClasses;
  13. private static readonly List<ILogTarget> m_LogTargets;
  14. public static event EventHandler<LogEventArgs> Updated;
  15. static Logger()
  16. {
  17. m_EnabledLevels = new bool[Enum.GetNames(typeof(LogLevel)).Length];
  18. m_EnabledClasses = new bool[Enum.GetNames(typeof(LogClass)).Length];
  19. m_EnabledLevels[(int)LogLevel.Stub] = true;
  20. m_EnabledLevels[(int)LogLevel.Info] = true;
  21. m_EnabledLevels[(int)LogLevel.Warning] = true;
  22. m_EnabledLevels[(int)LogLevel.Error] = true;
  23. for (int index = 0; index < m_EnabledClasses.Length; index++)
  24. {
  25. m_EnabledClasses[index] = true;
  26. }
  27. m_LogTargets = new List<ILogTarget>();
  28. m_Time = Stopwatch.StartNew();
  29. }
  30. public static void AddTarget(ILogTarget target)
  31. {
  32. m_LogTargets.Add(target);
  33. Updated += target.Log;
  34. }
  35. public static void Shutdown()
  36. {
  37. Updated = null;
  38. foreach(var target in m_LogTargets)
  39. {
  40. target.Dispose();
  41. }
  42. m_LogTargets.Clear();
  43. }
  44. public static void SetEnable(LogLevel logLevel, bool enabled)
  45. {
  46. m_EnabledLevels[(int)logLevel] = enabled;
  47. }
  48. public static void SetEnable(LogClass logClass, bool enabled)
  49. {
  50. m_EnabledClasses[(int)logClass] = enabled;
  51. }
  52. public static void PrintDebug(LogClass logClass, string message, [CallerMemberName] string caller = "")
  53. {
  54. Print(LogLevel.Debug, logClass, GetFormattedMessage(logClass, message, caller));
  55. }
  56. public static void PrintInfo(LogClass logClass, string message, [CallerMemberName] string Caller = "")
  57. {
  58. Print(LogLevel.Info, logClass, GetFormattedMessage(logClass, message, Caller));
  59. }
  60. public static void PrintWarning(LogClass logClass, string message, [CallerMemberName] string Caller = "")
  61. {
  62. Print(LogLevel.Warning, logClass, GetFormattedMessage(logClass, message, Caller));
  63. }
  64. public static void PrintError(LogClass logClass, string message, [CallerMemberName] string Caller = "")
  65. {
  66. Print(LogLevel.Error, logClass, GetFormattedMessage(logClass, message, Caller));
  67. }
  68. public static void PrintStub(LogClass logClass, string message = "", [CallerMemberName] string caller = "")
  69. {
  70. Print(LogLevel.Stub, logClass, GetFormattedMessage(logClass, "Stubbed. " + message, caller));
  71. }
  72. public static void PrintStub<T>(LogClass logClass, T obj, [CallerMemberName] string caller = "")
  73. {
  74. Print(LogLevel.Stub, logClass, GetFormattedMessage(logClass, "Stubbed.", caller), obj);
  75. }
  76. public static void PrintStub<T>(LogClass logClass, string message, T obj, [CallerMemberName] string caller = "")
  77. {
  78. Print(LogLevel.Stub, logClass, GetFormattedMessage(logClass, "Stubbed. " + message, caller), obj);
  79. }
  80. private static void Print(LogLevel logLevel, LogClass logClass, string message)
  81. {
  82. if (m_EnabledLevels[(int)logLevel] && m_EnabledClasses[(int)logClass])
  83. {
  84. Updated?.Invoke(null, new LogEventArgs(logLevel, m_Time.Elapsed, Thread.CurrentThread.ManagedThreadId, message));
  85. }
  86. }
  87. private static void Print(LogLevel logLevel, LogClass logClass, string message, object data)
  88. {
  89. if (m_EnabledLevels[(int)logLevel] && m_EnabledClasses[(int)logClass])
  90. {
  91. Updated?.Invoke(null, new LogEventArgs(logLevel, m_Time.Elapsed, Thread.CurrentThread.ManagedThreadId, message, data));
  92. }
  93. }
  94. private static string GetFormattedMessage(LogClass Class, string Message, string Caller)
  95. {
  96. return $"{Class} {Caller}: {Message}";
  97. }
  98. }
  99. }