Logging.cs 3.7 KB

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