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.

389 lines
15 KiB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
  1. using IPA.Config;
  2. using IPA.Logging.Printers;
  3. using System;
  4. using System.Collections.Concurrent;
  5. using System.Collections.Generic;
  6. using System.Diagnostics;
  7. using System.IO;
  8. using System.Linq;
  9. using System.Threading;
  10. namespace IPA.Logging
  11. {
  12. /// <summary>
  13. /// The default (and standard) <see cref="Logger"/> implementation.
  14. /// </summary>
  15. /// <remarks>
  16. /// <see cref="StandardLogger"/> uses a multi-threaded approach to logging. All actual I/O is done on another thread,
  17. /// where all messaged are guaranteed to be logged in the order they appeared. It is up to the printers to format them.
  18. ///
  19. /// This logger supports child loggers. Use <see cref="LoggerExtensions.GetChildLogger"/> to safely get a child.
  20. /// The modification of printers on a parent are reflected down the chain.
  21. /// </remarks>
  22. public class StandardLogger : Logger
  23. {
  24. private static readonly List<LogPrinter> defaultPrinters = new List<LogPrinter>()
  25. {
  26. new GlobalLogFilePrinter()
  27. };
  28. static StandardLogger()
  29. {
  30. ConsoleColorSupport();
  31. }
  32. private static bool addedConsolePrinters;
  33. private static bool finalizedDefaultPrinters;
  34. internal static void ConsoleColorSupport()
  35. {
  36. if (!addedConsolePrinters && !finalizedDefaultPrinters && WinConsole.IsInitialized )
  37. {
  38. defaultPrinters.AddRange(new []
  39. {
  40. new ColoredConsolePrinter()
  41. {
  42. Filter = LogLevel.DebugOnly,
  43. Color = ConsoleColor.Green,
  44. },
  45. new ColoredConsolePrinter()
  46. {
  47. Filter = LogLevel.InfoOnly,
  48. Color = ConsoleColor.White,
  49. },
  50. new ColoredConsolePrinter()
  51. {
  52. Filter = LogLevel.NoticeOnly,
  53. Color = ConsoleColor.Cyan
  54. },
  55. new ColoredConsolePrinter()
  56. {
  57. Filter = LogLevel.WarningOnly,
  58. Color = ConsoleColor.Yellow,
  59. },
  60. new ColoredConsolePrinter()
  61. {
  62. Filter = LogLevel.ErrorOnly,
  63. Color = ConsoleColor.Red,
  64. },
  65. new ColoredConsolePrinter()
  66. {
  67. Filter = LogLevel.CriticalOnly,
  68. Color = ConsoleColor.Magenta,
  69. }
  70. });
  71. addedConsolePrinters = true;
  72. }
  73. }
  74. /// <summary>
  75. /// The <see cref="TextWriter"/> for writing directly to the console window, or stdout if no window open.
  76. /// </summary>
  77. public static TextWriter ConsoleWriter { get; internal set; } = Console.Out;
  78. /// <summary>
  79. /// Adds to the default printer pool that all printers inherit from. Printers added this way will be passed every message from every logger.
  80. /// </summary>
  81. /// <param name="printer"></param>
  82. internal static void AddDefaultPrinter(LogPrinter printer)
  83. {
  84. defaultPrinters.Add(printer);
  85. }
  86. private readonly string logName;
  87. private static bool showSourceClass;
  88. /// <summary>
  89. /// All levels defined by this filter will be sent to loggers. All others will be ignored.
  90. /// </summary>
  91. public static LogLevel PrintFilter { get; set; } = LogLevel.All;
  92. private readonly List<LogPrinter> printers = new List<LogPrinter>();
  93. private readonly StandardLogger parent;
  94. private readonly Dictionary<string, StandardLogger> children = new Dictionary<string, StandardLogger>();
  95. /// <summary>
  96. /// Configures internal debug settings based on the config passed in.
  97. /// </summary>
  98. /// <param name="cfg"></param>
  99. internal static void Configure(SelfConfig cfg)
  100. {
  101. showSourceClass = cfg.Debug.ShowCallSource;
  102. PrintFilter = cfg.Debug.ShowDebug ? LogLevel.All : LogLevel.InfoUp;
  103. }
  104. private StandardLogger(StandardLogger parent, string subName)
  105. {
  106. logName = $"{parent.logName}/{subName}";
  107. this.parent = parent;
  108. printers = new List<LogPrinter>()
  109. {
  110. new PluginSubLogPrinter(parent.logName, subName)
  111. };
  112. if (logThread == null || !logThread.IsAlive)
  113. {
  114. logThread = new Thread(LogThread);
  115. logThread.Start();
  116. }
  117. }
  118. internal StandardLogger(string name)
  119. {
  120. ConsoleColorSupport();
  121. if (!finalizedDefaultPrinters)
  122. {
  123. if (!addedConsolePrinters)
  124. AddDefaultPrinter(new ColorlessConsolePrinter());
  125. finalizedDefaultPrinters = true;
  126. }
  127. logName = name;
  128. printers.Add(new PluginLogFilePrinter(name));
  129. if (logThread == null || !logThread.IsAlive)
  130. {
  131. logThread = new Thread(LogThread);
  132. logThread.Start();
  133. }
  134. }
  135. /// <summary>
  136. /// Gets a child printer with the given name, either constructing a new one or using one that was already made.
  137. /// </summary>
  138. /// <param name="name"></param>
  139. /// <returns>a child <see cref="StandardLogger"/> with the given sub-name</returns>
  140. internal StandardLogger GetChild(string name)
  141. {
  142. if (!children.TryGetValue(name, out var child))
  143. {
  144. child = new StandardLogger(this, name);
  145. children.Add(name, child);
  146. }
  147. return child;
  148. }
  149. /// <summary>
  150. /// Adds a log printer to the logger.
  151. /// </summary>
  152. /// <param name="printer">the printer to add</param>
  153. public void AddPrinter(LogPrinter printer)
  154. {
  155. printers.Add(printer);
  156. }
  157. /// <summary>
  158. /// Logs a specific message at a given level.
  159. /// </summary>
  160. /// <param name="level">the message level</param>
  161. /// <param name="message">the message to log</param>
  162. public override void Log(Level level, string message)
  163. {
  164. if (message == null)
  165. throw new ArgumentNullException(nameof(message));
  166. // make sure that the queue isn't being cleared
  167. logWaitEvent.Wait();
  168. logQueue.Add(new LogMessage
  169. {
  170. Level = level,
  171. Message = message,
  172. Logger = this,
  173. Time = DateTime.Now
  174. });
  175. }
  176. /// <inheritdoc />
  177. /// <summary>
  178. /// An override to <see cref="M:IPA.Logging.Logger.Debug(System.String)" /> which shows the method that called it.
  179. /// </summary>
  180. /// <param name="message">the message to log</param>
  181. public override void Debug(string message)
  182. {
  183. // add source to message
  184. var stackFrame = new StackTrace(true).GetFrame(1);
  185. var method = stackFrame.GetMethod();
  186. var lineNo = stackFrame.GetFileLineNumber();
  187. var paramString = string.Join(", ", method.GetParameters().Select(p => p.ParameterType.FullName));
  188. base.Debug(showSourceClass
  189. ? $"{{{method.DeclaringType?.FullName}::{method.Name}({paramString}):{lineNo}}} {message}"
  190. : message);
  191. }
  192. private struct LogMessage
  193. {
  194. public Level Level;
  195. public StandardLogger Logger;
  196. public string Message;
  197. public DateTime Time;
  198. }
  199. private static ManualResetEventSlim logWaitEvent = new ManualResetEventSlim(true);
  200. private static readonly BlockingCollection<LogMessage> logQueue = new BlockingCollection<LogMessage>();
  201. private static Thread logThread;
  202. private static StandardLogger loggerLogger;
  203. private const int LogCloseTimeout = 500;
  204. /// <summary>
  205. /// The log printer thread for <see cref="StandardLogger"/>.
  206. /// </summary>
  207. private static void LogThread()
  208. {
  209. AppDomain.CurrentDomain.ProcessExit += (sender, args) =>
  210. {
  211. StopLogThread();
  212. };
  213. loggerLogger = new StandardLogger("Log Subsystem");
  214. loggerLogger.printers.Clear(); // don't need a log file for this one
  215. var timeout = TimeSpan.FromMilliseconds(LogCloseTimeout);
  216. var started = new HashSet<LogPrinter>();
  217. while (logQueue.TryTake(out var msg, Timeout.Infinite))
  218. {
  219. do
  220. {
  221. var logger = msg.Logger;
  222. IEnumerable<LogPrinter> printers = logger.printers;
  223. do
  224. { // aggregate all printers in the inheritance chain
  225. logger = logger.parent;
  226. if (logger != null)
  227. printers = printers.Concat(logger.printers);
  228. } while (logger != null);
  229. foreach (var printer in printers.Concat(defaultPrinters))
  230. {
  231. try
  232. { // print to them all
  233. if (((byte) msg.Level & (byte) printer.Filter) != 0)
  234. {
  235. if (!started.Contains(printer))
  236. { // start printer if not started
  237. printer.StartPrint();
  238. started.Add(printer);
  239. }
  240. // update last use time and print
  241. printer.LastUse = DateTime.Now;
  242. printer.Print(msg.Level, msg.Time, msg.Logger.logName, msg.Message);
  243. }
  244. }
  245. catch (Exception e)
  246. {
  247. // do something sane in the face of an error
  248. Console.WriteLine($"printer errored: {e}");
  249. }
  250. }
  251. if (logQueue.Count > 512)
  252. { // spam filtering (if queue has more tha 512 elements)
  253. logWaitEvent.Reset(); // pause incoming log requests
  254. // clear loggers for this instance, to print the message to all affected logs
  255. loggerLogger.printers.Clear();
  256. var prints = new HashSet<LogPrinter>();
  257. // clear the queue
  258. while (logQueue.TryTake(out var message))
  259. { // aggregate loggers in the process
  260. var messageLogger = message.Logger;
  261. foreach (var print in messageLogger.printers)
  262. prints.Add(print);
  263. do
  264. {
  265. messageLogger = messageLogger.parent;
  266. if (messageLogger != null)
  267. foreach (var print in messageLogger.printers)
  268. prints.Add(print);
  269. } while (messageLogger != null);
  270. }
  271. // print using logging subsystem to all logger printers
  272. loggerLogger.printers.AddRange(prints);
  273. logQueue.Add(new LogMessage
  274. { // manually adding to the queue instead of using Warn() because calls to the logger are suspended here
  275. Level = Level.Warning,
  276. Logger = loggerLogger,
  277. Message = $"{loggerLogger.logName.ToUpper()}: Messages omitted to improve performance",
  278. Time = DateTime.Now
  279. });
  280. // resume log calls
  281. logWaitEvent.Set();
  282. }
  283. var now = DateTime.Now;
  284. var copy = new List<LogPrinter>(started);
  285. foreach (var printer in copy)
  286. {
  287. // close printer after 500ms from its last use
  288. if (now - printer.LastUse > timeout)
  289. {
  290. try
  291. {
  292. printer.EndPrint();
  293. }
  294. catch (Exception e)
  295. {
  296. Console.WriteLine($"printer errored: {e}");
  297. }
  298. started.Remove(printer);
  299. }
  300. }
  301. }
  302. // wait for messages for 500ms before ending the prints
  303. while (logQueue.TryTake(out msg, timeout));
  304. if (logQueue.Count == 0)
  305. { // when the queue has been empty for 500ms, end all prints
  306. foreach (var printer in started)
  307. {
  308. try
  309. {
  310. printer.EndPrint();
  311. }
  312. catch (Exception e)
  313. {
  314. Console.WriteLine($"printer errored: {e}");
  315. }
  316. }
  317. started.Clear();
  318. }
  319. }
  320. }
  321. /// <summary>
  322. /// Stops and joins the log printer thread.
  323. /// </summary>
  324. internal static void StopLogThread()
  325. {
  326. logQueue.CompleteAdding();
  327. logThread.Join();
  328. }
  329. }
  330. /// <summary>
  331. /// A class providing extensions for various loggers.
  332. /// </summary>
  333. public static class LoggerExtensions
  334. {
  335. /// <summary>
  336. /// Gets a child logger, if supported. Currently the only defined and supported logger is <see cref="StandardLogger"/>, and most plugins will only ever receive this anyway.
  337. /// </summary>
  338. /// <param name="logger">the parent <see cref="Logger"/></param>
  339. /// <param name="name">the name of the child</param>
  340. /// <returns>the child logger</returns>
  341. public static Logger GetChildLogger(this Logger logger, string name)
  342. {
  343. if (logger is StandardLogger standardLogger)
  344. return standardLogger.GetChild(name);
  345. throw new InvalidOperationException();
  346. }
  347. }
  348. }