Log.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. using Ryujinx.Common.Logging;
  2. using System;
  3. using System.Collections.Concurrent;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. using System.Reflection;
  7. using System.Text;
  8. using System.Threading;
  9. namespace Ryujinx
  10. {
  11. static class Log
  12. {
  13. private static readonly string _path;
  14. private static StreamWriter _logWriter;
  15. private static Thread _messageThread;
  16. private static BlockingCollection<LogEventArgs> _messageQueue;
  17. private static Dictionary<LogLevel, ConsoleColor> _logColors;
  18. static Log()
  19. {
  20. _logColors = new Dictionary<LogLevel, ConsoleColor>()
  21. {
  22. { LogLevel.Stub, ConsoleColor.DarkGray },
  23. { LogLevel.Info, ConsoleColor.White },
  24. { LogLevel.Warning, ConsoleColor.Yellow },
  25. { LogLevel.Error, ConsoleColor.Red }
  26. };
  27. _messageQueue = new BlockingCollection<LogEventArgs>(10);
  28. _messageThread = new Thread(() =>
  29. {
  30. while (!_messageQueue.IsCompleted)
  31. {
  32. try
  33. {
  34. PrintLog(_messageQueue.Take());
  35. }
  36. catch (InvalidOperationException)
  37. {
  38. // IOE means that Take() was called on a completed collection.
  39. // Some other thread can call CompleteAdding after we pass the
  40. // IsCompleted check but before we call Take.
  41. // We can simply catch the exception since the loop will break
  42. // on the next iteration.
  43. }
  44. }
  45. });
  46. _path = Path.Combine(Environment.CurrentDirectory, "Ryujinx.log");
  47. if (Logger.EnableFileLog)
  48. {
  49. _logWriter = new StreamWriter(File.Open(_path,FileMode.Create, FileAccess.Write));
  50. }
  51. _messageThread.IsBackground = true;
  52. _messageThread.Start();
  53. }
  54. private static void PrintLog(LogEventArgs e)
  55. {
  56. StringBuilder sb = new StringBuilder();
  57. sb.AppendFormat(@"{0:hh\:mm\:ss\.fff}", e.Time);
  58. sb.Append(" | ");
  59. sb.AppendFormat("{0:d4}", e.ThreadId);
  60. sb.Append(' ');
  61. sb.Append(e.Message);
  62. if (e.Data != null)
  63. {
  64. PropertyInfo[] props = e.Data.GetType().GetProperties();
  65. sb.Append(' ');
  66. foreach (var prop in props)
  67. {
  68. sb.Append(prop.Name);
  69. sb.Append(": ");
  70. sb.Append(prop.GetValue(e.Data));
  71. sb.Append(" - ");
  72. }
  73. // We remove the final '-' from the string
  74. if (props.Length > 0)
  75. {
  76. sb.Remove(sb.Length - 3, 3);
  77. }
  78. }
  79. string message = sb.ToString();
  80. if (_logColors.TryGetValue(e.Level, out ConsoleColor color))
  81. {
  82. Console.ForegroundColor = color;
  83. Console.WriteLine(message);
  84. Console.ResetColor();
  85. }
  86. else
  87. {
  88. Console.WriteLine(message);
  89. }
  90. if (Logger.EnableFileLog)
  91. {
  92. _logWriter.WriteLine(message);
  93. }
  94. }
  95. public static void LogMessage(object sender, LogEventArgs e)
  96. {
  97. if (!_messageQueue.IsAddingCompleted)
  98. {
  99. _messageQueue.Add(e);
  100. }
  101. }
  102. public static void Close()
  103. {
  104. _messageQueue.CompleteAdding();
  105. _messageThread.Join();
  106. if (Logger.EnableFileLog)
  107. {
  108. _logWriter.Flush();
  109. _logWriter.Close();
  110. _logWriter.Dispose();
  111. }
  112. }
  113. }
  114. }