From a7a71bf49a49c335b31587f5526bf2f26f4b03b2 Mon Sep 17 00:00:00 2001 From: Andrei Tudor Date: Wed, 5 Jul 2023 19:30:38 +0300 Subject: [PATCH] The logger now supports colors --- DiscordBot/Bot/Actions/Exit.cs | 7 ++--- DiscordBot/Installer.cs | 2 +- DiscordBot/Program.cs | 19 +++++++++++++- PluginManager/Config.cs | 2 +- .../Others/Actions/InternalActionsManager.cs | 4 +-- PluginManager/Others/Enums.cs | 1 - PluginManager/Others/Logger/DBLogger.cs | 23 +++++++++++++--- PluginManager/Others/Logger/LogMessage.cs | 26 ++++++++++++------- 8 files changed, 63 insertions(+), 21 deletions(-) diff --git a/DiscordBot/Bot/Actions/Exit.cs b/DiscordBot/Bot/Actions/Exit.cs index 511b540..9fb9d5f 100644 --- a/DiscordBot/Bot/Actions/Exit.cs +++ b/DiscordBot/Bot/Actions/Exit.cs @@ -17,8 +17,9 @@ public class Exit : ICommandAction { if (args is null || args.Length == 0) { - Config.Logger.Log("Exiting...", "Exit"); - Config.Data.Save(); + Config.Logger.Log("Exiting...", "Exit", isInternal:false); + await Config.Data.Save(); + await Config.Logger.SaveToFile(); Environment.Exit(0); } else @@ -32,7 +33,7 @@ public class Exit : ICommandAction break; case "force": - Config.Logger.Log("Exiting...", "Exit"); + Config.Logger.Log("Exiting (FORCE)...", "Exit", LogLevel.WARNING, false); Environment.Exit(0); break; diff --git a/DiscordBot/Installer.cs b/DiscordBot/Installer.cs index 74c3aa6..62c5b01 100644 --- a/DiscordBot/Installer.cs +++ b/DiscordBot/Installer.cs @@ -36,7 +36,7 @@ public static class Installer Config.Data.Add("ServerID", serverId); } - Config.Logger.Log("Config Saved", "Installer"); + Config.Logger.Log("Config Saved", "Installer", isInternal:true); Config.Data.Save(); diff --git a/DiscordBot/Program.cs b/DiscordBot/Program.cs index cd0cd89..a1ef889 100644 --- a/DiscordBot/Program.cs +++ b/DiscordBot/Program.cs @@ -165,7 +165,24 @@ public class Program URLs = new Json("./Data/Resources/URLs.json"); - Logger.LogEvent += (message, type) => { Console.WriteLine(message); }; + Logger.LogEvent += (message, type, isInternal) => + { + if (isInternal) return; + if (type == LogLevel.INFO) + Console.ForegroundColor = ConsoleColor.Green; + else if (type == LogLevel.WARNING) + Console.ForegroundColor = ConsoleColor.DarkYellow; + else if (type == LogLevel.ERROR) + Console.ForegroundColor = ConsoleColor.Red; + else if(type == LogLevel.CRITICAL) + Console.ForegroundColor = ConsoleColor.DarkRed; + + Console.WriteLine($"[{type.ToString()}] {message}"); + Console.ResetColor(); + + + + }; Console.WriteLine("Loading resources ..."); diff --git a/PluginManager/Config.cs b/PluginManager/Config.cs index fa7d00e..3dbea61 100644 --- a/PluginManager/Config.cs +++ b/PluginManager/Config.cs @@ -144,7 +144,7 @@ public class Config return ((IEnumerable)_dictionary).GetEnumerator(); } - public async void Save() + public async Task Save() { if (!string.IsNullOrEmpty(_file)) await Functions.SaveToJsonFile(_file, _dictionary); diff --git a/PluginManager/Others/Actions/InternalActionsManager.cs b/PluginManager/Others/Actions/InternalActionsManager.cs index 0020793..b7b40c9 100644 --- a/PluginManager/Others/Actions/InternalActionsManager.cs +++ b/PluginManager/Others/Actions/InternalActionsManager.cs @@ -33,14 +33,14 @@ public class InternalActionManager return; } - Config.Logger.Log($"Action {name} loaded successfully", typeName); + Config.Logger.Log($"Action {name} loaded successfully", LogLevel.INFO, true); } public async Task Execute(string actionName, params string[]? args) { if (!Actions.ContainsKey(actionName)) { - Config.Logger.Log($"Action {actionName} not found", "InternalActionManager", LogLevel.WARNING); + Config.Logger.Log($"Action {actionName} not found", "InternalActionManager", LogLevel.WARNING, true); return "Action not found"; } diff --git a/PluginManager/Others/Enums.cs b/PluginManager/Others/Enums.cs index 65952ef..16b69e3 100644 --- a/PluginManager/Others/Enums.cs +++ b/PluginManager/Others/Enums.cs @@ -16,7 +16,6 @@ public enum OperatingSystem /// public enum LogLevel { - NONE, INFO, WARNING, ERROR, diff --git a/PluginManager/Others/Logger/DBLogger.cs b/PluginManager/Others/Logger/DBLogger.cs index 9c5b0a8..0e92308 100644 --- a/PluginManager/Others/Logger/DBLogger.cs +++ b/PluginManager/Others/Logger/DBLogger.cs @@ -1,11 +1,12 @@ using System; using System.Collections.Generic; +using System.Threading.Tasks; namespace PluginManager.Others.Logger; public class DBLogger { - public delegate void LogHandler(string message, LogLevel logType); + public delegate void LogHandler(string message, LogLevel logType, bool isInternal = false); private readonly string _errFolder; @@ -23,6 +24,21 @@ public class DBLogger public IReadOnlyList Errors => ErrorHistory; public event LogHandler LogEvent; + + public void Log(string message, LogLevel type = LogLevel.INFO) + { + Log(new LogMessage(message, type)); + } + + public void Log(string message, LogLevel type= LogLevel.INFO, bool isInternal = false) + { + Log(new LogMessage(message, type,"unknown", isInternal)); + } + + public void Log(string message, string sender = "unknown", LogLevel type = LogLevel.INFO, bool isInternal = false) + { + Log(new LogMessage(message, type,sender,isInternal)); + } public void Log(string message, string sender = "unknown", LogLevel type = LogLevel.INFO) { @@ -45,13 +61,14 @@ public class DBLogger ErrorHistory.Add(message); } - public void Log(string message, object sender, LogLevel type = LogLevel.NONE) + public void Log(string message, object sender, LogLevel type = LogLevel.INFO) { Log(message, sender.GetType().Name, type); } - public async void SaveToFile() + public async Task SaveToFile(bool ErrorsOnly = true) { + if(!ErrorsOnly) await Functions.SaveToJsonFile(_logFolder + "/" + DateTime.Now.ToString("yyyy-MM-dd") + ".json", LogHistory); await Functions.SaveToJsonFile(_errFolder + "/" + DateTime.Now.ToString("yyyy-MM-dd") + ".json", diff --git a/PluginManager/Others/Logger/LogMessage.cs b/PluginManager/Others/Logger/LogMessage.cs index 1dd75e0..1d15796 100644 --- a/PluginManager/Others/Logger/LogMessage.cs +++ b/PluginManager/Others/Logger/LogMessage.cs @@ -6,20 +6,28 @@ public class LogMessage { public LogMessage(string message, LogLevel type) { - Message = message; - Type = type; - Time = DateTime.Now.ToString("HH:mm:ss"); + Message = message; + Type = type; + Time = DateTime.Now.ToString("HH:mm:ss"); + isInternal = false; } - public LogMessage(string message, LogLevel type, string sender) : this(message, type) + public LogMessage(string message, LogLevel type, string sender, bool isInternal) : this(message, type) { - Sender = sender; + Sender = sender; + this.isInternal = isInternal; } - public string Message { get; set; } - public LogLevel Type { get; set; } - public string Time { get; set; } - public string Sender { get; set; } + public LogMessage(string message, LogLevel type, string sender) : this (message, type, sender, false) + { + + } + + public string Message { get; set; } + public LogLevel Type { get; set; } + public string Time { get; set; } + public string Sender { get; set; } + public bool isInternal { get; set; } public override string ToString() {