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.

125 lines
4.4 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. }
  70. }
  71. _watcherThread.IsBackground = true;
  72. }
  73. }
  74. }
  75. void Start() => _watcherThread.Start();
  76. public void Stop() {
  77. _threadRunning = false;
  78. _watcherThread.Join();
  79. }
  80. ConsoleColor GetConsoleColour(WarningLevel level) {
  81. switch (level) {
  82. case WarningLevel.Log:
  83. return ConsoleColor.Green;
  84. case WarningLevel.Error:
  85. return ConsoleColor.Yellow;
  86. case WarningLevel.Exception:
  87. return ConsoleColor.Red;
  88. case WarningLevel.Warning:
  89. return ConsoleColor.Blue;
  90. default:
  91. return ConsoleColor.Gray;
  92. }
  93. }
  94. FileInfo GetPath(IPlugin plugin) => GetPath(plugin.Name);
  95. FileInfo GetPath(string modName) {
  96. ModName = modName;
  97. var logsDir = new DirectoryInfo($"./Logs/{modName}/{DateTime.Now:dd-MM-yy}");
  98. logsDir.Create();
  99. return new FileInfo($"{logsDir.FullName}/{logsDir.GetFiles().Length}.txt");
  100. }
  101. }
  102. public static class DebugExtensions {
  103. public static Logger GetLogger(this IPlugin plugin) {
  104. return new Logger(plugin);
  105. }
  106. }
  107. }