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.

126 lines
4.5 KiB

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