Logging.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. using System;
  2. using System.Diagnostics;
  3. using System.IO;
  4. namespace Ryujinx.Core
  5. {
  6. public static class Logging
  7. {
  8. private static Stopwatch ExecutionTime = new Stopwatch();
  9. private const string LogFileName = "Ryujinx.log";
  10. public static bool EnableInfo = Config.LoggingEnableInfo;
  11. public static bool EnableTrace = Config.LoggingEnableTrace;
  12. public static bool EnableDebug = Config.LoggingEnableDebug;
  13. public static bool EnableWarn = Config.LoggingEnableWarn;
  14. public static bool EnableError = Config.LoggingEnableError;
  15. public static bool EnableFatal = Config.LoggingEnableFatal;
  16. public static bool EnableLogFile = Config.LoggingEnableLogFile;
  17. static Logging()
  18. {
  19. ExecutionTime.Start();
  20. if (File.Exists(LogFileName)) File.Delete(LogFileName);
  21. }
  22. public static string GetExecutionTime()
  23. {
  24. return ExecutionTime.ElapsedMilliseconds.ToString().PadLeft(8, '0') + "ms";
  25. }
  26. private static string WhoCalledMe()
  27. {
  28. return new StackTrace().GetFrame(2).GetMethod().Name;
  29. }
  30. private static void LogFile(string Message)
  31. {
  32. if (EnableLogFile)
  33. {
  34. using (StreamWriter Writer = File.AppendText(LogFileName))
  35. {
  36. Writer.WriteLine(Message);
  37. }
  38. }
  39. }
  40. public static void Info(string Message)
  41. {
  42. if (EnableInfo)
  43. {
  44. string Text = $"{GetExecutionTime()} | INFO > {Message}";
  45. Console.ForegroundColor = ConsoleColor.White;
  46. Console.WriteLine(Text.PadLeft(Text.Length + 1, ' '));
  47. Console.ResetColor();
  48. LogFile(Text);
  49. }
  50. }
  51. public static void Trace(string Message)
  52. {
  53. if (EnableTrace)
  54. {
  55. string Text = $"{GetExecutionTime()} | TRACE > {WhoCalledMe()} - {Message}";
  56. Console.ForegroundColor = ConsoleColor.DarkGray;
  57. Console.WriteLine(Text.PadLeft(Text.Length + 1, ' '));
  58. Console.ResetColor();
  59. LogFile(Text);
  60. }
  61. }
  62. public static void Debug(string Message)
  63. {
  64. if (EnableDebug)
  65. {
  66. string Text = $"{GetExecutionTime()} | DEBUG > {WhoCalledMe()} - {Message}";
  67. Console.ForegroundColor = ConsoleColor.Gray;
  68. Console.WriteLine(Text.PadLeft(Text.Length + 1, ' '));
  69. Console.ResetColor();
  70. LogFile(Text);
  71. }
  72. }
  73. public static void Warn(string Message)
  74. {
  75. if (EnableWarn)
  76. {
  77. string Text = $"{GetExecutionTime()} | WARN > {WhoCalledMe()} - {Message}";
  78. Console.ForegroundColor = ConsoleColor.Yellow;
  79. Console.WriteLine(Text.PadLeft(Text.Length + 1, ' '));
  80. Console.ResetColor();
  81. LogFile(Text);
  82. }
  83. }
  84. public static void Error(string Message)
  85. {
  86. if (EnableError)
  87. {
  88. string Text = $"{GetExecutionTime()} | ERROR > {WhoCalledMe()} - {Message}";
  89. Console.ForegroundColor = ConsoleColor.Red;
  90. Console.WriteLine(Text.PadLeft(Text.Length + 1, ' '));
  91. Console.ResetColor();
  92. LogFile(Text);
  93. }
  94. }
  95. public static void Fatal(string Message)
  96. {
  97. if (EnableFatal)
  98. {
  99. string Text = $"{GetExecutionTime()} | FATAL > {WhoCalledMe()} - {Message}";
  100. Console.ForegroundColor = ConsoleColor.Magenta;
  101. Console.WriteLine(Text.PadLeft(Text.Length + 1, ' '));
  102. Console.ResetColor();
  103. LogFile(Text);
  104. }
  105. }
  106. }
  107. }