Logging.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. using Ryujinx.Core.OsHle.Ipc;
  2. using System;
  3. using System.Diagnostics;
  4. using System.IO;
  5. using System.Text;
  6. namespace Ryujinx.Core
  7. {
  8. public static class Logging
  9. {
  10. private static Stopwatch ExecutionTime = new Stopwatch();
  11. private const string LogFileName = "Ryujinx.log";
  12. private static bool EnableInfo = Config.LoggingEnableInfo;
  13. private static bool EnableTrace = Config.LoggingEnableTrace;
  14. private static bool EnableDebug = Config.LoggingEnableDebug;
  15. private static bool EnableWarn = Config.LoggingEnableWarn;
  16. private static bool EnableError = Config.LoggingEnableError;
  17. private static bool EnableFatal = Config.LoggingEnableFatal;
  18. private static bool EnableIpc = Config.LoggingEnableIpc;
  19. private static bool EnableLogFile = Config.LoggingEnableLogFile;
  20. static Logging()
  21. {
  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. public static void Ipc(byte[] Data, long CmdPtr, bool Domain)
  109. {
  110. if (EnableIpc)
  111. {
  112. Console.ForegroundColor = ConsoleColor.Cyan;
  113. Console.WriteLine(IpcLog.Message(Data, CmdPtr, Domain));
  114. Console.ResetColor();
  115. }
  116. }
  117. //https://www.codeproject.com/Articles/36747/Quick-and-Dirty-HexDump-of-a-Byte-Array
  118. public static string HexDump(byte[] bytes, int bytesPerLine = 16)
  119. {
  120. if (bytes == null) return "<null>";
  121. int bytesLength = bytes.Length;
  122. char[] HexChars = "0123456789ABCDEF".ToCharArray();
  123. int firstHexColumn =
  124. 8 // 8 characters for the address
  125. + 3; // 3 spaces
  126. int firstCharColumn = firstHexColumn
  127. + bytesPerLine * 3 // - 2 digit for the hexadecimal value and 1 space
  128. + (bytesPerLine - 1) / 8 // - 1 extra space every 8 characters from the 9th
  129. + 2; // 2 spaces
  130. int lineLength = firstCharColumn
  131. + bytesPerLine // - characters to show the ascii value
  132. + Environment.NewLine.Length; // Carriage return and line feed (should normally be 2)
  133. char[] line = (new String(' ', lineLength - Environment.NewLine.Length) + Environment.NewLine).ToCharArray();
  134. int expectedLines = (bytesLength + bytesPerLine - 1) / bytesPerLine;
  135. StringBuilder result = new StringBuilder(expectedLines * lineLength);
  136. for (int i = 0; i < bytesLength; i += bytesPerLine)
  137. {
  138. line[0] = HexChars[(i >> 28) & 0xF];
  139. line[1] = HexChars[(i >> 24) & 0xF];
  140. line[2] = HexChars[(i >> 20) & 0xF];
  141. line[3] = HexChars[(i >> 16) & 0xF];
  142. line[4] = HexChars[(i >> 12) & 0xF];
  143. line[5] = HexChars[(i >> 8) & 0xF];
  144. line[6] = HexChars[(i >> 4) & 0xF];
  145. line[7] = HexChars[(i >> 0) & 0xF];
  146. int hexColumn = firstHexColumn;
  147. int charColumn = firstCharColumn;
  148. for (int j = 0; j < bytesPerLine; j++)
  149. {
  150. if (j > 0 && (j & 7) == 0) hexColumn++;
  151. if (i + j >= bytesLength)
  152. {
  153. line[hexColumn] = ' ';
  154. line[hexColumn + 1] = ' ';
  155. line[charColumn] = ' ';
  156. }
  157. else
  158. {
  159. byte b = bytes[i + j];
  160. line[hexColumn] = HexChars[(b >> 4) & 0xF];
  161. line[hexColumn + 1] = HexChars[b & 0xF];
  162. line[charColumn] = (b < 32 ? '·' : (char)b);
  163. }
  164. hexColumn += 3;
  165. charColumn++;
  166. }
  167. result.Append(line);
  168. }
  169. return result.ToString();
  170. }
  171. }
  172. }