Logging.cs 3.9 KB

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