BITKit/Packages/Runtime~/Core/BITLog/BIT4Log.cs

68 lines
1.9 KiB
C#

using System;
namespace BITKit
{
public static class BIT4Log
{
const string _debugLevel = "DebugLevel";
public static event Action<string> OnLog;
public static event Action<Exception> OnException;
public static event Action<string> OnWarning;
public static event Action<ConsoleColor> OnSetConsoleColor;
public static event Action OnNextLine;
private static bool LogTime;
static Type currentType;
[ExcuteOnAwake]
public static void Start()
{
}
[ExcuteOnStop]
public static void Stop()
{
OnLog = null;
OnException = null;
OnWarning = null;
OnNextLine = null;
}
[BITCommand]
public static void UseLogTime()
{
LogTime = true;
}
public static void Log(object x, int debugLevel = 0, ConsoleColor color = ConsoleColor.White)
{
if (x is not null)
{
OnSetConsoleColor?.Invoke(color);
OnLog?.Invoke( LogTime ? $"[{DateTime.Now}]{x}":x.ToString());
}
}
public static void Log<T>(object x, int debugLevel = 0, ConsoleColor color = ConsoleColor.White)
{
if (currentType == typeof(T))
{
Log(x, debugLevel);
}
else
{
OnNextLine?.Invoke();
Log($"{typeof(T).Name}:{x}", debugLevel);
}
currentType = typeof(T);
}
public static void LogException(Exception e)
{
OnException?.Invoke(e);
}
public static void Warnning(object x, int debugLevel = 0)
{
OnWarning?.Invoke(x.ToString());
}
public static void Warnning<T>(object x, int debugLevel = 0)
{
Warnning($"{typeof(T).Name}:{x}", debugLevel);
}
}
}