Logging.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. ExecutionTime.Start();
  23. if (File.Exists(LogFileName)) File.Delete(LogFileName);
  24. }
  25. public static string GetExecutionTime()
  26. {
  27. return ExecutionTime.ElapsedMilliseconds.ToString().PadLeft(8, '0') + "ms";
  28. }
  29. private static string WhoCalledMe()
  30. {
  31. return new StackTrace().GetFrame(2).GetMethod().Name;
  32. }
  33. private static void LogFile(string Message)
  34. {
  35. if (EnableLogFile)
  36. {
  37. using (StreamWriter Writer = File.AppendText(LogFileName))
  38. {
  39. Writer.WriteLine(Message);
  40. }
  41. }
  42. }
  43. public static void Info(string Message)
  44. {
  45. if (EnableInfo)
  46. {
  47. string Text = $"{GetExecutionTime()} | INFO > {Message}";
  48. Console.ForegroundColor = ConsoleColor.White;
  49. Console.WriteLine(Text.PadLeft(Text.Length + 1, ' '));
  50. Console.ResetColor();
  51. LogFile(Text);
  52. }
  53. }
  54. public static void Trace(string Message)
  55. {
  56. if (EnableTrace)
  57. {
  58. string Text = $"{GetExecutionTime()} | TRACE > {WhoCalledMe()} - {Message}";
  59. Console.ForegroundColor = ConsoleColor.DarkGray;
  60. Console.WriteLine(Text.PadLeft(Text.Length + 1, ' '));
  61. Console.ResetColor();
  62. LogFile(Text);
  63. }
  64. }
  65. public static void Debug(string Message)
  66. {
  67. if (EnableDebug)
  68. {
  69. string Text = $"{GetExecutionTime()} | DEBUG > {WhoCalledMe()} - {Message}";
  70. Console.ForegroundColor = ConsoleColor.Gray;
  71. Console.WriteLine(Text.PadLeft(Text.Length + 1, ' '));
  72. Console.ResetColor();
  73. LogFile(Text);
  74. }
  75. }
  76. public static void Warn(string Message)
  77. {
  78. if (EnableWarn)
  79. {
  80. string Text = $"{GetExecutionTime()} | WARN > {WhoCalledMe()} - {Message}";
  81. Console.ForegroundColor = ConsoleColor.Yellow;
  82. Console.WriteLine(Text.PadLeft(Text.Length + 1, ' '));
  83. Console.ResetColor();
  84. LogFile(Text);
  85. }
  86. }
  87. public static void Error(string Message)
  88. {
  89. if (EnableError)
  90. {
  91. string Text = $"{GetExecutionTime()} | ERROR > {WhoCalledMe()} - {Message}";
  92. Console.ForegroundColor = ConsoleColor.Red;
  93. Console.WriteLine(Text.PadLeft(Text.Length + 1, ' '));
  94. Console.ResetColor();
  95. LogFile(Text);
  96. }
  97. }
  98. public static void Fatal(string Message)
  99. {
  100. if (EnableFatal)
  101. {
  102. string Text = $"{GetExecutionTime()} | FATAL > {WhoCalledMe()} - {Message}";
  103. Console.ForegroundColor = ConsoleColor.Magenta;
  104. Console.WriteLine(Text.PadLeft(Text.Length + 1, ' '));
  105. Console.ResetColor();
  106. LogFile(Text);
  107. }
  108. }
  109. public static void Ipc(byte[] Data, long CmdPtr, bool Domain)
  110. {
  111. if (EnableIpc)
  112. {
  113. Console.ForegroundColor = ConsoleColor.Cyan;
  114. Console.WriteLine(IpcLog.Message(Data, CmdPtr, Domain));
  115. Console.ResetColor();
  116. }
  117. }
  118. //https://www.codeproject.com/Articles/36747/Quick-and-Dirty-HexDump-of-a-Byte-Array
  119. public static string HexDump(byte[] bytes, int bytesPerLine = 16)
  120. {
  121. if (bytes == null) return "<null>";
  122. int bytesLength = bytes.Length;
  123. char[] HexChars = "0123456789ABCDEF".ToCharArray();
  124. int firstHexColumn =
  125. 8 // 8 characters for the address
  126. + 3; // 3 spaces
  127. int firstCharColumn = firstHexColumn
  128. + bytesPerLine * 3 // - 2 digit for the hexadecimal value and 1 space
  129. + (bytesPerLine - 1) / 8 // - 1 extra space every 8 characters from the 9th
  130. + 2; // 2 spaces
  131. int lineLength = firstCharColumn
  132. + bytesPerLine // - characters to show the ascii value
  133. + Environment.NewLine.Length; // Carriage return and line feed (should normally be 2)
  134. char[] line = (new String(' ', lineLength - Environment.NewLine.Length) + Environment.NewLine).ToCharArray();
  135. int expectedLines = (bytesLength + bytesPerLine - 1) / bytesPerLine;
  136. StringBuilder result = new StringBuilder(expectedLines * lineLength);
  137. for (int i = 0; i < bytesLength; i += bytesPerLine)
  138. {
  139. line[0] = HexChars[(i >> 28) & 0xF];
  140. line[1] = HexChars[(i >> 24) & 0xF];
  141. line[2] = HexChars[(i >> 20) & 0xF];
  142. line[3] = HexChars[(i >> 16) & 0xF];
  143. line[4] = HexChars[(i >> 12) & 0xF];
  144. line[5] = HexChars[(i >> 8) & 0xF];
  145. line[6] = HexChars[(i >> 4) & 0xF];
  146. line[7] = HexChars[(i >> 0) & 0xF];
  147. int hexColumn = firstHexColumn;
  148. int charColumn = firstCharColumn;
  149. for (int j = 0; j < bytesPerLine; j++)
  150. {
  151. if (j > 0 && (j & 7) == 0) hexColumn++;
  152. if (i + j >= bytesLength)
  153. {
  154. line[hexColumn] = ' ';
  155. line[hexColumn + 1] = ' ';
  156. line[charColumn] = ' ';
  157. }
  158. else
  159. {
  160. byte b = bytes[i + j];
  161. line[hexColumn] = HexChars[(b >> 4) & 0xF];
  162. line[hexColumn + 1] = HexChars[b & 0xF];
  163. line[charColumn] = (b < 32 ? '·' : (char)b);
  164. }
  165. hexColumn += 3;
  166. charColumn++;
  167. }
  168. result.Append(line);
  169. }
  170. return result.ToString();
  171. }
  172. }
  173. }