Changed from TextType to LogLevel

This commit is contained in:
2023-05-28 17:37:19 +03:00
parent f16c139362
commit 77f1bef862
13 changed files with 45 additions and 65 deletions

View File

@@ -32,7 +32,7 @@ namespace DiscordBot
Config.Data.Add("prefix", prefix);
Config.Data.Add("ServerID", serverId);
Config.Logger.Log("Config Saved", "Installer", TextType.NORMAL);
Config.Logger.Log("Config Saved", "Installer", LogLevel.INFO);
Config.Data.Save();

View File

@@ -14,7 +14,7 @@ using PluginManager.Online.Helpers;
using PluginManager.Others;
using DiscordBot.Utilities;
using Microsoft.VisualBasic.CompilerServices;
using OperatingSystem = PluginManager.Others.OperatingSystem;
using static PluginManager.Config;
@@ -121,7 +121,7 @@ public class Program
}
catch (Exception ex)
{
Config.Logger.Log(ex.ToString(), "Bot", TextType.ERROR);
Config.Logger.Log(ex.ToString(), "Bot", LogLevel.ERROR);
return null;
}
}
@@ -154,7 +154,7 @@ public class Program
if (Config.Data.ContainsKey("LaunchMessage"))
Config.Data.Add("LaunchMessage",
"An error occured while closing the bot last time. Please consider closing the bot using the &rsd&c method !\nThere is a risk of losing all data or corruption of the save file, which in some cases requires to reinstall the bot !");
Config.Logger.Log("An error occured while closing the bot last time. Please consider closing the bot using the &rsd&c method !\nThere is a risk of losing all data or corruption of the save file, which in some cases requires to reinstall the bot !", "Bot", TextType.ERROR);
Config.Logger.Log("An error occured while closing the bot last time. Please consider closing the bot using the &rsd&c method !\nThere is a risk of losing all data or corruption of the save file, which in some cases requires to reinstall the bot !", "Bot", LogLevel.ERROR);
}
}
return;
@@ -200,7 +200,7 @@ public class Program
}
catch (Exception ex)
{
Config.Logger.Log(ex.ToString(), "Bot", TextType.ERROR);
Config.Logger.Log(ex.ToString(), "Bot", LogLevel.ERROR);
}
}
@@ -284,7 +284,7 @@ public class Program
}
catch (Exception ex)
{
Config.Logger.Log(ex.Message, "Updater", TextType.ERROR);
Config.Logger.Log(ex.Message, "Updater", LogLevel.ERROR);
if (ex.Message.Contains("Access de"))
Config.Logger.Log("Please run the bot as root.");
}

View File

@@ -243,7 +243,7 @@ public class ConsoleCommandsHandler
if (File.Exists("./" + split[1])) File.Delete("./" + split[1]);
await ServerCom.DownloadFileAsync(split[0], "./" + split[1], null);
Console.WriteLine("Item " + split[1] + " downloaded !", this, TextType.SUCCESS);
Console.WriteLine("Item " + split[1] + " downloaded !");
Console.WriteLine();
if (split[0].EndsWith(".pak"))
@@ -254,7 +254,7 @@ public class ConsoleCommandsHandler
{
Console.WriteLine($"Extracting {split[1]} ...");
await ArchiveManager.ExtractArchive("./" + split[1], "./", null,
UnzipProgressType.PercentageFromTotalSize);
UnzipProgressType.PERCENTAGE_FROM_TOTAL_SIZE);
Console.WriteLine("\n");
File.Delete("./" + split[1]);
}
@@ -271,7 +271,7 @@ public class ConsoleCommandsHandler
isDownloading = false;
Config.Logger.Log("Plugin installed !", this, TextType.SUCCESS);
Config.Logger.Log("Plugin installed !", this, LogLevel.INFO);
//await ExecuteCommad("localload " + name);
}
@@ -373,7 +373,7 @@ public class ConsoleCommandsHandler
catch (Exception ex)
{
Console.WriteLine(ex.Message);
Config.Logger.Log(ex.Message, this, TextType.ERROR);
Config.Logger.Log(ex.Message, this, LogLevel.ERROR);
}
});

View File

@@ -106,7 +106,7 @@ public class Boot
if (arg.Message.Contains("401"))
{
Config.Data.Remove("token");
Config.Logger.Log("The token is invalid. Please restart the bot and enter a valid token.", this, Others.TextType.ERROR);
Config.Logger.Log("The token is invalid. Please restart the bot and enter a valid token.", this, Others.LogLevel.ERROR);
await Task.Delay(4000);
Environment.Exit(0);
}
@@ -136,7 +136,7 @@ public class Boot
{
case LogSeverity.Error:
case LogSeverity.Critical:
Config.Logger.Log(message.Message, this, Others.TextType.ERROR);
Config.Logger.Log(message.Message, this, Others.LogLevel.ERROR);
break;

View File

@@ -61,7 +61,7 @@ internal class CommandHandler
}
catch (Exception ex)
{
Config.Logger.Log(ex.Message, "CommandHandler", TextType.ERROR);
Config.Logger.Log(ex.Message, "CommandHandler", LogLevel.ERROR);
}
return Task.CompletedTask;
@@ -144,7 +144,7 @@ internal class CommandHandler
}
catch (Exception ex)
{
Config.Logger.Log(ex.Message, this, TextType.ERROR);
Config.Logger.Log(ex.Message, this, LogLevel.ERROR);
}
}
}

View File

@@ -41,7 +41,7 @@ public static class Config
IsLoaded = true;
Logger.Log("Config initialized", TextType.NORMAL);
Logger.Log("Config initialized", LogLevel.INFO);
}
public class Json<TKey, TValue> : IDictionary<TKey, TValue>

View File

@@ -60,7 +60,7 @@ namespace PluginManager.Loaders
}
catch (Exception ex)
{
Config.Logger.Log("PluginName: " + new FileInfo(file).Name.Split('.')[0] + " not loaded", this, Others.TextType.ERROR);
Config.Logger.Log("PluginName: " + new FileInfo(file).Name.Split('.')[0] + " not loaded", this, Others.LogLevel.ERROR);
continue;
}
if (FileLoaded != null)
@@ -131,7 +131,7 @@ namespace PluginManager.Loaders
}
catch (Exception ex)
{
Config.Logger.Log(ex.Message, this, Others.TextType.ERROR);
Config.Logger.Log(ex.Message, this, Others.LogLevel.ERROR);
return null;
}

View File

@@ -87,10 +87,10 @@ public class PluginLoader
Events = new List<DBEvent>();
SlashCommands = new List<DBSlashCommand>();
Config.Logger.Log("Starting plugin loader ... Client: " + _client.CurrentUser.Username, this, Others.TextType.NORMAL);
Config.Logger.Log("Starting plugin loader ... Client: " + _client.CurrentUser.Username, this, Others.LogLevel.INFO);
var loader = new Loader("./Data/Plugins", "dll");
loader.FileLoaded += (args) => Config.Logger.Log($"{args.PluginName} file Loaded", this , Others.TextType.SUCCESS);
loader.FileLoaded += (args) => Config.Logger.Log($"{args.PluginName} file Loaded", this , Others.LogLevel.INFO);
loader.PluginLoaded += Loader_PluginLoaded;
var res = loader.Load();
Events = res.Item1;
@@ -116,7 +116,7 @@ public class PluginLoader
}
catch (Exception ex)
{
Config.Logger.Log(ex.Message, this, Others.TextType.ERROR);
Config.Logger.Log(ex.Message, this, Others.LogLevel.ERROR);
}
break;
case "DBSlashCommand":
@@ -146,13 +146,13 @@ public class PluginLoader
var instance = (DBEvent)Activator.CreateInstance(type);
instance.Start(client);
PluginLoader.Events.Add(instance);
Config.Logger.Log($"[EVENT] Loaded external {type.FullName}!", Others.TextType.SUCCESS);
Config.Logger.Log($"[EVENT] Loaded external {type.FullName}!", Others.LogLevel.INFO);
}
else if (type.IsClass && typeof(DBCommand).IsAssignableFrom(type))
{
var instance = (DBCommand)Activator.CreateInstance(type);
PluginLoader.Commands.Add(instance);
Config.Logger.Log($"[CMD] Instance: {type.FullName} loaded !", Others.TextType.SUCCESS);
Config.Logger.Log($"[CMD] Instance: {type.FullName} loaded !", Others.LogLevel.INFO);
}
else if (type.IsClass && typeof(DBSlashCommand).IsAssignableFrom(type))
{
@@ -165,7 +165,7 @@ public class PluginLoader
await client.CreateGlobalApplicationCommandAsync(builder.Build());
PluginLoader.SlashCommands.Add(instance);
Config.Logger.Log($"[SLASH] Instance: {type.FullName} loaded !", Others.TextType.SUCCESS);
Config.Logger.Log($"[SLASH] Instance: {type.FullName} loaded !", Others.LogLevel.INFO);
}
}

View File

@@ -14,7 +14,8 @@ public class PluginsManager
/// <summary>
/// The Plugin Manager constructor
/// </summary>
/// <param name="link">The link to the file where all plugins are stored</param>
/// <param name="plink">The link to the file where all plugins are stored</param>
/// <param name="vlink">The link to the file where all plugin versions are stored</param>
public PluginsManager(string plink, string vlink)
{
PluginsLink = plink;
@@ -82,7 +83,7 @@ public class PluginsManager
}
catch (Exception exception)
{
Config.Logger.Log("Failed to execute command: listplugs\nReason: " + exception.Message, this, TextType.ERROR);
Config.Logger.Log("Failed to execute command: listplugs\nReason: " + exception.Message, this, LogLevel.ERROR);
}
return null;
@@ -134,7 +135,7 @@ public class PluginsManager
}
catch (Exception exception)
{
Config.Logger.Log("Failed to execute command: listplugs\nReason: " + exception.Message, this, TextType.ERROR);
Config.Logger.Log("Failed to execute command: listplugs\nReason: " + exception.Message, this, LogLevel.ERROR);
}
return new string[] { null!, null!, null! };

View File

@@ -58,7 +58,7 @@ namespace PluginManager.Others
}
catch (Exception ex)
{
Config.Logger.Log(ex.Message, "Archive Manager", TextType.ERROR); // Write the error to a file
Config.Logger.Log(ex.Message, "Archive Manager", LogLevel.ERROR); // Write the error to a file
await Task.Delay(100);
return await ReadFromPakAsync(FileName, archFile);
}
@@ -79,7 +79,7 @@ namespace PluginManager.Others
Directory.CreateDirectory(folder);
using (var archive = ZipFile.OpenRead(zip))
{
if (type == UnzipProgressType.PercentageFromNumberOfFiles)
if (type == UnzipProgressType.PERCENTAGE_FROM_NUMBER_OF_FILES)
{
var totalZIPFiles = archive.Entries.Count();
var currentZIPFile = 0;
@@ -95,7 +95,7 @@ namespace PluginManager.Others
}
catch (Exception ex)
{
Config.Logger.Log($"Failed to extract {entry.Name}. Exception: {ex.Message}", "Archive Manager", TextType.ERROR);
Config.Logger.Log($"Failed to extract {entry.Name}. Exception: {ex.Message}", "Archive Manager", LogLevel.ERROR);
}
currentZIPFile++;
@@ -104,7 +104,7 @@ namespace PluginManager.Others
progress.Report((float)currentZIPFile / totalZIPFiles * 100);
}
}
else if (type == UnzipProgressType.PercentageFromTotalSize)
else if (type == UnzipProgressType.PERCENTAGE_FROM_TOTAL_SIZE)
{
ulong zipSize = 0;
@@ -127,7 +127,7 @@ namespace PluginManager.Others
}
catch (Exception ex)
{
Config.Logger.Log($"Failed to extract {entry.Name}. Exception: {ex.Message}", "Archive Manager", TextType.ERROR);
Config.Logger.Log($"Failed to extract {entry.Name}. Exception: {ex.Message}", "Archive Manager", LogLevel.ERROR);
}
await Task.Delay(10);

View File

@@ -11,23 +11,10 @@ public enum OperatingSystem
UNKNOWN
}
/// <summary>
/// A list with all errors
/// </summary>
public enum Error
{
UNKNOWN_ERROR,
GUILD_NOT_FOUND,
STREAM_NOT_FOUND,
INVALID_USER,
INVALID_CHANNEL,
INVALID_PERMISSIONS
}
/// <summary>
/// The output log type
/// </summary>
public enum OutputLogLevel
public enum LogLevel
{
NONE,
INFO,
@@ -38,8 +25,8 @@ public enum OutputLogLevel
public enum UnzipProgressType
{
PercentageFromNumberOfFiles,
PercentageFromTotalSize
PERCENTAGE_FROM_NUMBER_OF_FILES,
PERCENTAGE_FROM_TOTAL_SIZE
}
public enum SaveType
@@ -48,11 +35,3 @@ public enum SaveType
BACKUP
}
public enum TextType
{
NORMAL,
ERROR,
WARNING,
SUCCESS
}

View File

@@ -16,7 +16,7 @@ namespace PluginManager.Others.Logger
public IReadOnlyList<LogMessage> Logs => LogHistory;
public IReadOnlyList<LogMessage> Errors => ErrorHistory;
public delegate void LogHandler(string message, TextType logType);
public delegate void LogHandler(string message, LogLevel logType);
public event LogHandler LogEvent;
private string _logFolder;
@@ -28,20 +28,20 @@ namespace PluginManager.Others.Logger
_errFolder = Config.Data["ErrorFolder"];
}
public void Log(string message, string sender = "unknown", TextType type = TextType.NORMAL) => Log(new LogMessage(message, type, sender));
public void Log(string message, string sender = "unknown", LogLevel type = LogLevel.INFO) => Log(new LogMessage(message, type, sender));
public void Log(LogMessage message)
{
if(LogEvent is not null)
LogEvent?.Invoke(message.Message, message.Type);
if (message.Type != TextType.ERROR)
if (message.Type != LogLevel.NONE)
LogHistory.Add(message);
else
ErrorHistory.Add(message);
}
public void Log(string message, object sender, TextType type = TextType.NORMAL) => Log(message, sender.GetType().Name, type);
public void Log(string message, object sender, LogLevel type = LogLevel.NONE) => Log(message, sender.GetType().Name, type);
public async void SaveToFile()
{

View File

@@ -9,17 +9,17 @@ namespace PluginManager.Others.Logger
public class LogMessage
{
public string Message { get; set; }
public TextType Type { get; set; }
public LogLevel Type { get; set; }
public string Time { get; set; }
public string Sender { get; set; }
public LogMessage(string message, TextType type)
public LogMessage(string message, LogLevel type)
{
Message = message;
Type = type;
Time = DateTime.Now.ToString("HH:mm:ss");
}
public LogMessage(string message, TextType type, string sender) : this(message, type)
public LogMessage(string message, LogLevel type, string sender) : this(message, type)
{
Sender = sender;
}
@@ -31,15 +31,15 @@ namespace PluginManager.Others.Logger
public static explicit operator LogMessage(string message)
{
return new LogMessage(message, TextType.NORMAL);
return new LogMessage(message, LogLevel.INFO);
}
public static explicit operator LogMessage((string message, TextType type) tuple)
public static explicit operator LogMessage((string message, LogLevel type) tuple)
{
return new LogMessage(tuple.message, tuple.type);
}
public static explicit operator LogMessage((string message, TextType type, string sender) tuple)
public static explicit operator LogMessage((string message, LogLevel type, string sender) tuple)
{
return new LogMessage(tuple.message, tuple.type, tuple.sender);
}