Logging.cs 9.7 KB

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