BITFALL/Assets/BITKit/Core/BITLog/BIT4Log.cs

76 lines
2.0 KiB
C#

using System;
using System.Diagnostics;
#if UNITY_64
using UnityEngine;
#endif
namespace BITKit
{
public static class BIT4Log
{
#if UNITY_EDITOR
[RuntimeInitializeOnLoadMethod]
private static void Reload()
{
OnLog = null;
OnException = null;
OnWarning = null;
OnSetConsoleColor = null;
OnNextLine = null;
}
#endif
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 Type currentType;
#if UNITY_64
[HideInCallstack]
#endif
public static void Log(object x, int debugLevel = 0, ConsoleColor color = ConsoleColor.White)
{
OnSetConsoleColor?.Invoke(color);
OnLog?.Invoke(x?.ToString());
}
#if UNITY_64
[HideInCallstack]
#endif
public static void Log<T>(object x, int debugLevel = 0, ConsoleColor color = ConsoleColor.White)
{
if (currentType != typeof(T))
{
OnNextLine?.Invoke();
}
#if NET5_0_OR_GREATER
Log($"[{DateTime.Now}]{typeof(T).Name}:{x}", debugLevel);
#else
Log($"{typeof(T).Name}:{x}", debugLevel);
#endif
currentType = typeof(T);
}
#if UNITY_64
[HideInCallstack]
#endif
public static void LogException(Exception e)
{
OnException?.Invoke(e);
}
#if UNITY_64
[HideInCallstack]
#endif
public static void Warning(object x, int debugLevel = 0)
{
OnWarning?.Invoke(x.ToString());
}
#if UNITY_64
[HideInCallstack]
#endif
public static void Warning<T>(object x, int debugLevel = 0)
{
Warning($"{typeof(T).Name}:{x}", debugLevel);
}
}
}