patch
This commit is contained in:
@@ -1,11 +1,34 @@
|
||||
using Discord;
|
||||
using System;
|
||||
using System.IO;
|
||||
|
||||
using Discord;
|
||||
|
||||
namespace PluginManager
|
||||
{
|
||||
public static class Logger
|
||||
{
|
||||
|
||||
public static bool isConsole = true;
|
||||
public static bool isConsole { get; private set; }
|
||||
private static bool isInitialized;
|
||||
|
||||
private static string? logFolder;
|
||||
private static string? errFolder;
|
||||
|
||||
public static void Initialize(bool console)
|
||||
{
|
||||
if (isInitialized) throw new Exception("Logger is already initialized");
|
||||
|
||||
if (!Config.Variables.Exists("LogFolder"))
|
||||
Config.Variables.Add("LogFolder", "./Data/Output/Logs/");
|
||||
|
||||
if (!Config.Variables.Exists("ErrorFolder"))
|
||||
Config.Variables.Add("ErrorFolder", "./Data/Output/Errors/");
|
||||
|
||||
isInitialized = true;
|
||||
logFolder = Config.Variables.GetValue("LogFolder");
|
||||
errFolder = Config.Variables.GetValue("ErrorFolder");
|
||||
isConsole = console;
|
||||
}
|
||||
|
||||
|
||||
public delegate void LogEventHandler(string Message);
|
||||
@@ -13,16 +36,19 @@ namespace PluginManager
|
||||
|
||||
public static void Log(string Message)
|
||||
{
|
||||
if (!isInitialized) throw new Exception("Logger is not initialized");
|
||||
LogEvent?.Invoke(Message);
|
||||
}
|
||||
|
||||
public static void Log(string Message, params object[] Args)
|
||||
{
|
||||
if (!isInitialized) throw new Exception("Logger is not initialized");
|
||||
LogEvent?.Invoke(string.Format(Message, Args));
|
||||
}
|
||||
|
||||
public static void Log(IMessage message, bool newLine)
|
||||
{
|
||||
if (!isInitialized) throw new Exception("Logger is not initialized");
|
||||
LogEvent?.Invoke(message.Content);
|
||||
if (newLine)
|
||||
LogEvent?.Invoke("\n");
|
||||
@@ -30,18 +56,21 @@ namespace PluginManager
|
||||
|
||||
public static void WriteLine(string? message)
|
||||
{
|
||||
if (!isInitialized) throw new Exception("Logger is not initialized");
|
||||
if (message is not null)
|
||||
LogEvent?.Invoke(message + '\n');
|
||||
}
|
||||
|
||||
public static void LogError(System.Exception ex)
|
||||
{
|
||||
if (!isInitialized) throw new Exception("Logger is not initialized");
|
||||
string message = "[ERROR]" + ex.Message;
|
||||
LogEvent?.Invoke(message + '\n');
|
||||
}
|
||||
|
||||
public static void LogError(string? message)
|
||||
{
|
||||
if (!isInitialized) throw new Exception("Logger is not initialized");
|
||||
if (message is not null)
|
||||
LogEvent?.Invoke("[ERROR]" + message + '\n');
|
||||
}
|
||||
@@ -49,17 +78,51 @@ namespace PluginManager
|
||||
|
||||
public static void WriteLine()
|
||||
{
|
||||
if (!isInitialized) throw new Exception("Logger is not initialized");
|
||||
LogEvent?.Invoke("\n");
|
||||
}
|
||||
|
||||
public static void Write(string message)
|
||||
{
|
||||
if (!isInitialized) throw new Exception("Logger is not initialized");
|
||||
LogEvent?.Invoke(message);
|
||||
}
|
||||
|
||||
public static void Write(char c)
|
||||
{
|
||||
if (!isInitialized) throw new Exception("Logger is not initialized");
|
||||
LogEvent?.Invoke($"{c}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Write logs to file
|
||||
/// </summary>
|
||||
/// <param name="LogMessage">The message to be wrote</param>
|
||||
public static void WriteLogFile(string LogMessage)
|
||||
{
|
||||
if (!isInitialized) throw new Exception("Logger is not initialized");
|
||||
var logsPath = logFolder + $"{DateTime.Today.ToShortDateString().Replace("/", "-").Replace("\\", "-")} Log.txt";
|
||||
Directory.CreateDirectory(logFolder);
|
||||
File.AppendAllTextAsync(logsPath, LogMessage + " \n").Wait();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Write error to file
|
||||
/// </summary>
|
||||
/// <param name="ErrMessage">The message to be wrote</param>
|
||||
public static void WriteErrFile(string ErrMessage)
|
||||
{
|
||||
if (!isInitialized) throw new Exception("Logger is not initialized");
|
||||
var errPath = errFolder +
|
||||
$"{DateTime.Today.ToShortDateString().Replace("/", "-").Replace("\\", "-")} Error.txt";
|
||||
Directory.CreateDirectory(errFolder);
|
||||
File.AppendAllText(errPath, ErrMessage + " \n");
|
||||
}
|
||||
|
||||
public static void WriteErrFile(this Exception ex)
|
||||
{
|
||||
if (!isInitialized) throw new Exception("Logger is not initialized");
|
||||
WriteErrFile(ex.ToString());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user