Add log levels to logger, update formats, use string as input argument for the message.

This commit is contained in:
2024-01-20 17:11:26 +01:00
parent 8abc17034c
commit a6e2bee975
2 changed files with 54 additions and 29 deletions

View File

@@ -1,67 +1,97 @@
#pragma warning disable CA2211
using System.Runtime.CompilerServices;
namespace DaggerFramework.Utils namespace DaggerFramework.Utils
{ {
public class Logger public class Logger
{ {
public static Action<string, LogType>? OnLog; public static Action<string, LogLevel>? OnLog;
public static string LogPath = "Logs/";
/// <summary>
/// Specifies the logging level. In release builds, the log level is <c>Error</c>. In debug, the log level is <c>Echo</c>.
/// </summary>
public static LogLevel LogLevel = LogLevel.Error;
public Logger(string className) public Logger(string className)
{ {
_className = className; _className = className;
#if DEBUG
LogLevel = LogLevel.Echo;
#endif
} }
public void Echo(object what) public void Echo(string what)
{ {
LogConsole((string)what, LogType.Echo); if (LogLevel < LogLevel.Echo) return;
OnLog?.Invoke((string)what, LogType.Echo);
string message = string.Format(EchoFormat, DateFormat, what);
LogConsole(what, LogLevel.Echo);
OnLog?.Invoke(what, LogLevel.Echo);
} }
public void Info(object what) public void Info(string what, [CallerMemberName] string method = "")
{ {
LogType logType = LogType.Info; if (LogLevel < LogLevel.Info) return;
string message = $"({DateFormat}) [{logType.ToString().ToUpper()}/{_className}] {what}";
LogLevel logType = LogLevel.Info;
string message = string.Format(LogFormat, DateFormat, logType.ToString(), _className, method, what);
LogConsole(message, logType); LogConsole(message, logType);
OnLog?.Invoke(message, logType); OnLog?.Invoke(message, logType);
} }
public void Warn(object what) public void Warn(string what, [CallerMemberName] string method = "")
{ {
LogType logType = LogType.Warn; if (LogLevel < LogLevel.Warn) return;
string message = $"({DateFormat}) [{logType.ToString().ToUpper()}/{_className}] {what}";
LogLevel logType = LogLevel.Warn;
string message = string.Format(LogFormat, DateFormat, logType.ToString(), _className, method, what);
LogConsole(message, logType); LogConsole(message, logType);
OnLog?.Invoke(message, logType); OnLog?.Invoke(message, logType);
} }
public void Error(object what) public void Error(string what, [CallerMemberName] string method = "")
{ {
LogType logType = LogType.Error; if (LogLevel < LogLevel.Error) return;
string message = $"({DateFormat}) [{logType.ToString().ToUpper()}/{_className}] {what}";
LogLevel logType = LogLevel.Error;
string message = string.Format(LogFormat, DateFormat, logType.ToString(), _className, method, what);
LogConsole(message, logType); LogConsole(message, logType);
OnLog?.Invoke(message, logType); OnLog?.Invoke(message, logType);
} }
private static string DateFormat => $"{DateTime.Now:t}"; 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, LogType logType) private static void LogConsole(string what, LogLevel logType)
{ {
Console.ForegroundColor = GetConsoleColorForLog(logType); Console.ForegroundColor = ConsoleColorForLog(logType);
Console.WriteLine(what); Console.WriteLine(what);
Console.ForegroundColor = ConsoleColor.White; Console.ForegroundColor = ConsoleColor.White;
} }
private static ConsoleColor GetConsoleColorForLog(LogType logType) private static ConsoleColor ConsoleColorForLog(LogLevel logType)
{ {
ConsoleColor color = ConsoleColor.White; ConsoleColor color = ConsoleColor.White;
switch (logType) switch (logType)
{ {
case LogType.Info: case LogLevel.Info:
color = ConsoleColor.Cyan; color = ConsoleColor.Cyan;
break; break;
case LogType.Warn: case LogLevel.Warn:
color = ConsoleColor.Yellow; color = ConsoleColor.Yellow;
break; break;
case LogType.Error: case LogLevel.Error:
color = ConsoleColor.Red; color = ConsoleColor.Red;
break; break;
} }
@@ -72,11 +102,11 @@ namespace DaggerFramework.Utils
private readonly string _className; private readonly string _className;
} }
public enum LogType public enum LogLevel
{ {
Echo, Error,
Info,
Warn, Warn,
Error Info,
Echo,
} }
} }

View File

@@ -8,7 +8,6 @@ public class TestPlayer : RectangleShape2d
{ {
base.OnStart(); base.OnStart();
Color = Color.Cyan; Color = Color.Cyan;
_logger.Echo("OnStart");
_camera = new Camera2d() _camera = new Camera2d()
{ {
@@ -29,10 +28,6 @@ public class TestPlayer : RectangleShape2d
Position += velocity * (float)dt; Position += velocity * (float)dt;
_camera.Position = MathUtils.LerpVector2(_camera.Position, Position, dt * 5f); _camera.Position = MathUtils.LerpVector2(_camera.Position, Position, dt * 5f);
// var mousePos = Input.GetMousePosition();
// Position = MathUtils.LerpVector2(Position, mousePos, dt * 5f);
// Rotation += (float)dt * 100f;
} }
private Logger _logger = new(nameof(TestPlayer)); private Logger _logger = new(nameof(TestPlayer));