Logging.cs 7.1 KB

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