Add log levels to logger, update formats, use string as input argument for the message.
This commit is contained in:
@@ -1,67 +1,97 @@
|
||||
#pragma warning disable CA2211
|
||||
|
||||
using System.Runtime.CompilerServices;
|
||||
|
||||
namespace DaggerFramework.Utils
|
||||
{
|
||||
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)
|
||||
{
|
||||
_className = className;
|
||||
|
||||
#if DEBUG
|
||||
LogLevel = LogLevel.Echo;
|
||||
#endif
|
||||
}
|
||||
|
||||
public void Echo(object what)
|
||||
public void Echo(string what)
|
||||
{
|
||||
LogConsole((string)what, LogType.Echo);
|
||||
OnLog?.Invoke((string)what, LogType.Echo);
|
||||
if (LogLevel < LogLevel.Echo) return;
|
||||
|
||||
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;
|
||||
string message = $"({DateFormat}) [{logType.ToString().ToUpper()}/{_className}] {what}";
|
||||
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(object what)
|
||||
public void Warn(string what, [CallerMemberName] string method = "")
|
||||
{
|
||||
LogType logType = LogType.Warn;
|
||||
string message = $"({DateFormat}) [{logType.ToString().ToUpper()}/{_className}] {what}";
|
||||
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(object what)
|
||||
public void Error(string what, [CallerMemberName] string method = "")
|
||||
{
|
||||
LogType logType = LogType.Error;
|
||||
string message = $"({DateFormat}) [{logType.ToString().ToUpper()}/{_className}] {what}";
|
||||
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: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.ForegroundColor = ConsoleColor.White;
|
||||
}
|
||||
|
||||
private static ConsoleColor GetConsoleColorForLog(LogType logType)
|
||||
private static ConsoleColor ConsoleColorForLog(LogLevel logType)
|
||||
{
|
||||
ConsoleColor color = ConsoleColor.White;
|
||||
|
||||
switch (logType)
|
||||
{
|
||||
case LogType.Info:
|
||||
case LogLevel.Info:
|
||||
color = ConsoleColor.Cyan;
|
||||
break;
|
||||
|
||||
case LogType.Warn:
|
||||
case LogLevel.Warn:
|
||||
color = ConsoleColor.Yellow;
|
||||
break;
|
||||
case LogType.Error:
|
||||
case LogLevel.Error:
|
||||
color = ConsoleColor.Red;
|
||||
break;
|
||||
}
|
||||
@@ -72,11 +102,11 @@ namespace DaggerFramework.Utils
|
||||
private readonly string _className;
|
||||
}
|
||||
|
||||
public enum LogType
|
||||
public enum LogLevel
|
||||
{
|
||||
Echo,
|
||||
Info,
|
||||
Error,
|
||||
Warn,
|
||||
Error
|
||||
Info,
|
||||
Echo,
|
||||
}
|
||||
}
|
||||
@@ -8,7 +8,6 @@ public class TestPlayer : RectangleShape2d
|
||||
{
|
||||
base.OnStart();
|
||||
Color = Color.Cyan;
|
||||
_logger.Echo("OnStart");
|
||||
|
||||
_camera = new Camera2d()
|
||||
{
|
||||
@@ -29,10 +28,6 @@ public class TestPlayer : RectangleShape2d
|
||||
Position += velocity * (float)dt;
|
||||
|
||||
_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));
|
||||
|
||||
Reference in New Issue
Block a user