You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

134 lines
3.9 KiB

  1. using IllusionInjector.Logging.Printers;
  2. using IllusionPlugin.Logging;
  3. using IllusionPlugin.Logging.Printers;
  4. using System;
  5. using System.Collections.Concurrent;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading;
  10. using System.Threading.Tasks;
  11. using LoggerBase = IllusionPlugin.Logging.Logger;
  12. namespace IllusionInjector.Logging
  13. {
  14. internal static class Logger
  15. {
  16. private static LoggerBase _log;
  17. internal static LoggerBase log
  18. {
  19. get
  20. {
  21. if (_log == null)
  22. _log = new StandardLogger("IllusionInjector");
  23. return _log;
  24. }
  25. }
  26. }
  27. public class StandardLogger : LoggerBase
  28. {
  29. private static readonly IReadOnlyList<LogPrinter> defaultPrinters = new List<LogPrinter>()
  30. {
  31. new ColoredConsolePrinter()
  32. {
  33. Filter = LogLevel.DebugOnly,
  34. Color = ConsoleColor.Green,
  35. },
  36. new ColoredConsolePrinter()
  37. {
  38. Filter = LogLevel.InfoOnly,
  39. Color = ConsoleColor.White,
  40. },
  41. new ColoredConsolePrinter()
  42. {
  43. Filter = LogLevel.WarningOnly,
  44. Color = ConsoleColor.Yellow,
  45. },
  46. new ColoredConsolePrinter()
  47. {
  48. Filter = LogLevel.ErrorOnly,
  49. Color = ConsoleColor.Red,
  50. },
  51. new ColoredConsolePrinter()
  52. {
  53. Filter = LogLevel.CriticalOnly,
  54. Color = ConsoleColor.Magenta,
  55. },
  56. new GlobalZFIlePrinter()
  57. };
  58. private string logName;
  59. private static LogLevel showFilter = LogLevel.InfoUp;
  60. public static LogLevel Filter { get => showFilter; set => showFilter = value; }
  61. private List<LogPrinter> printers = new List<LogPrinter>(defaultPrinters);
  62. internal StandardLogger(string name)
  63. {
  64. logName = name;
  65. printers.Add(new PluginLogFilePrinter(name));
  66. if (_logThread == null || !_logThread.IsAlive)
  67. {
  68. _logThread = new Thread(LogThread);
  69. _logThread.Start();
  70. }
  71. }
  72. public override void Log(Level level, string message)
  73. {
  74. _logQueue.Add(new LogMessage
  75. {
  76. level = level,
  77. message = message,
  78. logger = this,
  79. time = DateTime.Now
  80. });
  81. }
  82. internal struct LogMessage
  83. {
  84. public Level level;
  85. public StandardLogger logger;
  86. public string message;
  87. public DateTime time;
  88. }
  89. private static BlockingCollection<LogMessage> _logQueue = new BlockingCollection<LogMessage>();
  90. private static Thread _logThread;
  91. private static void LogThread()
  92. {
  93. HashSet<LogPrinter> started = new HashSet<LogPrinter>();
  94. while (_logQueue.TryTake(out LogMessage msg, Timeout.Infinite)) {
  95. foreach (var printer in msg.logger.printers)
  96. {
  97. if (((byte)msg.level & (byte)printer.Filter) != 0)
  98. {
  99. if (!started.Contains(printer))
  100. {
  101. printer.StartPrint();
  102. started.Add(printer);
  103. }
  104. printer.Print(msg.level, msg.time, msg.logger.logName, msg.message);
  105. }
  106. }
  107. if (_logQueue.Count == 0)
  108. {
  109. foreach (var printer in started)
  110. printer.EndPrint();
  111. started.Clear();
  112. }
  113. }
  114. }
  115. public static void StopLogThread()
  116. {
  117. _logQueue.CompleteAdding();
  118. _logThread.Join();
  119. }
  120. }
  121. }