#pragma warning disable CA2211 using System.Runtime.CompilerServices; namespace DaggerFramework.Utils { public class Logger { public static Action? OnLog; public static string LogPath = "Logs/"; /// /// Specifies the logging level. In release builds, the log level is Error. In debug, the log level is Echo. /// public static LogLevel LogLevel = LogLevel.Error; public Logger(string className) { _className = className; #if DEBUG LogLevel = LogLevel.Echo; #endif } public void Echo(string what) { if (LogLevel < LogLevel.Echo) return; string message = string.Format(EchoFormat, DateFormat, what); LogConsole(what, LogLevel.Echo); OnLog?.Invoke(what, LogLevel.Echo); } public void Info(string what, [CallerMemberName] string method = "") { if (LogLevel < LogLevel.Info) return; LogLevel logType = LogLevel.Info; string message = string.Format(LogFormat, DateFormat, logType.ToString(), _className, method, what); LogConsole(message, logType); OnLog?.Invoke(message, logType); } public void Warn(string what, [CallerMemberName] string method = "") { if (LogLevel < LogLevel.Warn) return; LogLevel logType = LogLevel.Warn; string message = string.Format(LogFormat, DateFormat, logType.ToString(), _className, method, what); LogConsole(message, logType); OnLog?.Invoke(message, logType); } public void Error(string what, [CallerMemberName] string method = "") { if (LogLevel < LogLevel.Error) return; LogLevel logType = LogLevel.Error; string message = string.Format(LogFormat, DateFormat, logType.ToString(), _className, method, what); LogConsole(message, logType); OnLog?.Invoke(message, logType); } private static string DateFormat => $"{DateTime.Now:HH:mm:ffff}"; private static string LogFormat => "({0}) [{1}/{2}/{3}] {4}"; private static string EchoFormat => "({0}) {1}"; private static void LogConsole(string what, LogLevel logType) { Console.ForegroundColor = ConsoleColorForLog(logType); Console.WriteLine(what); Console.ForegroundColor = ConsoleColor.White; } private static ConsoleColor ConsoleColorForLog(LogLevel logType) { ConsoleColor color = ConsoleColor.White; switch (logType) { case LogLevel.Info: color = ConsoleColor.Cyan; break; case LogLevel.Warn: color = ConsoleColor.Yellow; break; case LogLevel.Error: color = ConsoleColor.Red; break; } return color; } private readonly string _className; } public enum LogLevel { Error, Warn, Info, Echo, } }