Logging.cs 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. using Ryujinx.Core.OsHle.Ipc;
  2. using System;
  3. using System.Diagnostics;
  4. using System.IO;
  5. using System.Runtime.CompilerServices;
  6. using System.Text;
  7. namespace Ryujinx.Core
  8. {
  9. public static class Logging
  10. {
  11. private static Stopwatch ExecutionTime;
  12. private const string LogFileName = "Ryujinx.log";
  13. private static bool EnableInfo = Config.LoggingEnableInfo;
  14. private static bool EnableTrace = Config.LoggingEnableTrace;
  15. private static bool EnableDebug = Config.LoggingEnableDebug;
  16. private static bool EnableWarn = Config.LoggingEnableWarn;
  17. private static bool EnableError = Config.LoggingEnableError;
  18. private static bool EnableFatal = Config.LoggingEnableFatal;
  19. private static bool EnableIpc = Config.LoggingEnableIpc;
  20. private static bool EnableLogFile = Config.LoggingEnableLogFile;
  21. private enum LogLevel
  22. {
  23. Debug = 1,
  24. Error = 2,
  25. Fatal = 3,
  26. Info = 4,
  27. Trace = 5,
  28. Warn = 6
  29. }
  30. static Logging()
  31. {
  32. if (File.Exists(LogFileName)) File.Delete(LogFileName);
  33. ExecutionTime = new Stopwatch();
  34. ExecutionTime.Start();
  35. }
  36. public static string GetExecutionTime() => ExecutionTime.ElapsedMilliseconds.ToString().PadLeft(8, '0') + "ms";
  37. private static void LogMessage(LogEntry LogEntry)
  38. {
  39. ConsoleColor consoleColor = ConsoleColor.White;
  40. switch (LogEntry.LogLevel)
  41. {
  42. case LogLevel.Debug:
  43. consoleColor = ConsoleColor.Gray;
  44. break;
  45. case LogLevel.Error:
  46. consoleColor = ConsoleColor.Red;
  47. break;
  48. case LogLevel.Fatal:
  49. consoleColor = ConsoleColor.Magenta;
  50. break;
  51. case LogLevel.Info:
  52. consoleColor = ConsoleColor.White;
  53. break;
  54. case LogLevel.Trace:
  55. consoleColor = ConsoleColor.DarkGray;
  56. break;
  57. case LogLevel.Warn:
  58. consoleColor = ConsoleColor.Yellow;
  59. break;
  60. }
  61. string Text = $"{LogEntry.ExecutionTime} | {LogEntry.LogLevel.ToString()} > " +
  62. $"{LogEntry.CallingMember} > {LogEntry.Message}";
  63. Console.ForegroundColor = consoleColor;
  64. Console.WriteLine(Text.PadLeft(Text.Length + 1, ' '));
  65. Console.ResetColor();
  66. LogFile(Text);
  67. }
  68. private static void LogFile(string Message)
  69. {
  70. if (EnableLogFile)
  71. {
  72. using (StreamWriter Writer = File.AppendText(LogFileName))
  73. {
  74. Writer.WriteLine(Message);
  75. }
  76. }
  77. }
  78. public static void Info(string Message, [CallerMemberName] string CallingMember = "")
  79. {
  80. if (EnableInfo)
  81. {
  82. LogMessage(new LogEntry
  83. {
  84. CallingMember = CallingMember,
  85. LogLevel = LogLevel.Info,
  86. Message = Message,
  87. ExecutionTime = GetExecutionTime()
  88. });
  89. }
  90. }
  91. public static void Trace(string Message, [CallerMemberName] string CallingMember = "")
  92. {
  93. if (EnableTrace)
  94. {
  95. LogMessage(new LogEntry
  96. {
  97. CallingMember = CallingMember,
  98. LogLevel = LogLevel.Trace,
  99. Message = Message,
  100. ExecutionTime = GetExecutionTime()
  101. });
  102. }
  103. }
  104. public static void Debug(string Message, [CallerMemberName] string CallingMember = "")
  105. {
  106. if (EnableDebug)
  107. {
  108. LogMessage(new LogEntry
  109. {
  110. CallingMember = CallingMember,
  111. LogLevel = LogLevel.Debug,
  112. Message = Message,
  113. ExecutionTime = GetExecutionTime()
  114. });
  115. }
  116. }
  117. public static void Warn(string Message, [CallerMemberName] string CallingMember = "")
  118. {
  119. if (EnableWarn)
  120. {
  121. LogMessage(new LogEntry
  122. {
  123. CallingMember = CallingMember,
  124. LogLevel = LogLevel.Warn,
  125. Message = Message,
  126. ExecutionTime = GetExecutionTime()
  127. });
  128. }
  129. }
  130. public static void Error(string Message, [CallerMemberName] string CallingMember = "")
  131. {
  132. if (EnableError)
  133. {
  134. LogMessage(new LogEntry
  135. {
  136. CallingMember = CallingMember,
  137. LogLevel = LogLevel.Error,
  138. Message = Message,
  139. ExecutionTime = GetExecutionTime()
  140. });
  141. }
  142. }
  143. public static void Fatal(string Message, [CallerMemberName] string CallingMember = "")
  144. {
  145. if (EnableFatal)
  146. {
  147. LogMessage(new LogEntry
  148. {
  149. CallingMember = CallingMember,
  150. LogLevel = LogLevel.Fatal,
  151. Message = Message,
  152. ExecutionTime = GetExecutionTime()
  153. });
  154. }
  155. }
  156. public static void Ipc(byte[] Data, long CmdPtr, bool Domain)
  157. {
  158. if (EnableIpc)
  159. {
  160. Console.ForegroundColor = ConsoleColor.Cyan;
  161. Console.WriteLine(IpcLog.Message(Data, CmdPtr, Domain));
  162. Console.ResetColor();
  163. }
  164. }
  165. //https://www.codeproject.com/Articles/36747/Quick-and-Dirty-HexDump-of-a-Byte-Array
  166. public static string HexDump(byte[] bytes, int bytesPerLine = 16)
  167. {
  168. if (bytes == null) return "<null>";
  169. int bytesLength = bytes.Length;
  170. char[] HexChars = "0123456789ABCDEF".ToCharArray();
  171. int firstHexColumn =
  172. 8 // 8 characters for the address
  173. + 3; // 3 spaces
  174. int firstCharColumn = firstHexColumn
  175. + bytesPerLine * 3 // - 2 digit for the hexadecimal value and 1 space
  176. + (bytesPerLine - 1) / 8 // - 1 extra space every 8 characters from the 9th
  177. + 2; // 2 spaces
  178. int lineLength = firstCharColumn
  179. + bytesPerLine // - characters to show the ascii value
  180. + Environment.NewLine.Length; // Carriage return and line feed (should normally be 2)
  181. char[] line = (new String(' ', lineLength - Environment.NewLine.Length) + Environment.NewLine).ToCharArray();
  182. int expectedLines = (bytesLength + bytesPerLine - 1) / bytesPerLine;
  183. StringBuilder result = new StringBuilder(expectedLines * lineLength);
  184. for (int i = 0; i < bytesLength; i += bytesPerLine)
  185. {
  186. line[0] = HexChars[(i >> 28) & 0xF];
  187. line[1] = HexChars[(i >> 24) & 0xF];
  188. line[2] = HexChars[(i >> 20) & 0xF];
  189. line[3] = HexChars[(i >> 16) & 0xF];
  190. line[4] = HexChars[(i >> 12) & 0xF];
  191. line[5] = HexChars[(i >> 8) & 0xF];
  192. line[6] = HexChars[(i >> 4) & 0xF];
  193. line[7] = HexChars[(i >> 0) & 0xF];
  194. int hexColumn = firstHexColumn;
  195. int charColumn = firstCharColumn;
  196. for (int j = 0; j < bytesPerLine; j++)
  197. {
  198. if (j > 0 && (j & 7) == 0) hexColumn++;
  199. if (i + j >= bytesLength)
  200. {
  201. line[hexColumn] = ' ';
  202. line[hexColumn + 1] = ' ';
  203. line[charColumn] = ' ';
  204. }
  205. else
  206. {
  207. byte b = bytes[i + j];
  208. line[hexColumn] = HexChars[(b >> 4) & 0xF];
  209. line[hexColumn + 1] = HexChars[b & 0xF];
  210. line[charColumn] = (b < 32 ? '·' : (char)b);
  211. }
  212. hexColumn += 3;
  213. charColumn++;
  214. }
  215. result.Append(line);
  216. }
  217. return result.ToString();
  218. }
  219. private struct LogEntry
  220. {
  221. public string CallingMember;
  222. public string ExecutionTime;
  223. public string Message;
  224. public LogLevel LogLevel;
  225. }
  226. }
  227. }