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.

116 lines
4.0 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Threading;
  5. using IllusionPlugin;
  6. namespace IllusionPlugin {
  7. public class Logger {
  8. private readonly Queue<logMessage> _logQueue;
  9. private readonly FileInfo _logFile;
  10. private readonly Thread _watcherThread;
  11. private bool _threadRunning;
  12. private logMessage oldLog;
  13. struct logMessage {
  14. public WarningLevel WarningLevel;
  15. public string Message;
  16. public logMessage(string msg, WarningLevel wl) {
  17. Message = msg;
  18. WarningLevel = wl;
  19. }
  20. }
  21. enum WarningLevel {
  22. Log, Error, Exception
  23. }
  24. Logger() {
  25. _logQueue = new Queue<logMessage>();
  26. _logFile = GetPath("Default");
  27. _watcherThread = new Thread(QueueWatcher) {IsBackground = true};
  28. _threadRunning = true;
  29. Start();
  30. }
  31. public Logger(IPlugin plugin) {
  32. _logQueue = new Queue<logMessage>();
  33. _logFile = GetPath(plugin);
  34. _watcherThread = new Thread(QueueWatcher) {IsBackground = true};
  35. _threadRunning = true;
  36. Start();
  37. }
  38. public void Log(string msg) {
  39. if(!_watcherThread.IsAlive) throw new Exception("Logger is Closed!");
  40. _logQueue.Enqueue(new logMessage($"[LOG @ {DateTime.Now:HH:mm:ss}] {msg}", WarningLevel.Log));
  41. }
  42. public void Error(string msg) {
  43. if(!_watcherThread.IsAlive) throw new Exception("Logger is Closed!");
  44. _logQueue.Enqueue(new logMessage($"[ERROR @ {DateTime.Now:HH:mm:ss}] {msg}", WarningLevel.Error));
  45. }
  46. public void Exception(string msg) {
  47. if(!_watcherThread.IsAlive) throw new Exception("Logger is Closed!");
  48. _logQueue.Enqueue(new logMessage($"[EXCEPTION @ {DateTime.Now:HH:mm:ss}] {msg}", WarningLevel.Exception));
  49. }
  50. void QueueWatcher() {
  51. _logFile.Create().Close();
  52. SetConsoleColour(WarningLevel.Log);
  53. while (_threadRunning) {
  54. if (_logQueue.Count > 0) {
  55. _watcherThread.IsBackground = false;
  56. using (var f = _logFile.AppendText()) {
  57. while (_logQueue.Count > 0) {
  58. var d = _logQueue.Dequeue();
  59. if (d.Message == oldLog.Message) return;
  60. oldLog = d;
  61. f.WriteLine(d.Message);
  62. if(d.WarningLevel != oldLog.WarningLevel) SetConsoleColour(d.WarningLevel);
  63. Console.WriteLine(d);
  64. }
  65. }
  66. _watcherThread.IsBackground = true;
  67. }
  68. }
  69. }
  70. void Start() => _watcherThread.Start();
  71. public void Stop() {
  72. _threadRunning = false;
  73. _watcherThread.Join();
  74. }
  75. void SetConsoleColour(WarningLevel level) {
  76. switch (level) {
  77. case WarningLevel.Log:
  78. Console.ForegroundColor = ConsoleColor.Green;
  79. break;
  80. case WarningLevel.Error:
  81. Console.ForegroundColor = ConsoleColor.Yellow;
  82. break;
  83. case WarningLevel.Exception:
  84. Console.ForegroundColor = ConsoleColor.Red;
  85. break;
  86. }
  87. }
  88. FileInfo GetPath(IPlugin plugin) => GetPath(plugin.Name);
  89. FileInfo GetPath(string modName) {
  90. var logsDir = new DirectoryInfo($"./Logs/{modName}/{DateTime.Now:dd-MM-yy}");
  91. logsDir.Create();
  92. return new FileInfo($"{logsDir.FullName}/{logsDir.GetFiles().Length}.txt");
  93. }
  94. }
  95. public static class DebugExtensions {
  96. public static Logger GetLogger(this IPlugin plugin) {
  97. return new Logger(plugin);
  98. }
  99. }
  100. }