Logging.cs 10 KB

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