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

76 lines
2.0 KiB
C#
Raw Normal View History

2023-08-12 01:43:24 +08:00
using System;
2023-10-20 19:31:12 +08:00
using System.Diagnostics;
#if UNITY_64
using UnityEngine;
#endif
2023-08-12 01:43:24 +08:00
namespace BITKit
{
public static class BIT4Log
{
#if UNITY_EDITOR
2023-10-20 19:31:12 +08:00
[RuntimeInitializeOnLoadMethod]
2023-08-12 01:43:24 +08:00
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;
2023-10-20 19:31:12 +08:00
#if UNITY_64
[HideInCallstack]
#endif
2023-08-12 01:43:24 +08:00
public static void Log(object x, int debugLevel = 0, ConsoleColor color = ConsoleColor.White)
{
2023-08-23 01:59:40 +08:00
OnSetConsoleColor?.Invoke(color);
OnLog?.Invoke(x?.ToString());
2023-08-12 01:43:24 +08:00
}
2023-10-20 19:31:12 +08:00
#if UNITY_64
[HideInCallstack]
#endif
2023-08-12 01:43:24 +08:00
public static void Log<T>(object x, int debugLevel = 0, ConsoleColor color = ConsoleColor.White)
{
2023-08-23 01:59:40 +08:00
if (currentType != typeof(T))
2023-08-12 01:43:24 +08:00
{
OnNextLine?.Invoke();
}
2023-08-23 01:59:40 +08:00
#if NET5_0_OR_GREATER
Log($"[{DateTime.Now}]{typeof(T).Name}:{x}", debugLevel);
#else
Log($"{typeof(T).Name}:{x}", debugLevel);
#endif
2023-08-12 01:43:24 +08:00
currentType = typeof(T);
}
2023-10-20 19:31:12 +08:00
#if UNITY_64
[HideInCallstack]
#endif
2023-08-12 01:43:24 +08:00
public static void LogException(Exception e)
{
OnException?.Invoke(e);
}
2023-10-20 19:31:12 +08:00
#if UNITY_64
[HideInCallstack]
#endif
2023-08-23 01:59:40 +08:00
public static void Warning(object x, int debugLevel = 0)
2023-08-12 01:43:24 +08:00
{
OnWarning?.Invoke(x.ToString());
}
2023-10-20 19:31:12 +08:00
#if UNITY_64
[HideInCallstack]
#endif
2023-08-23 01:59:40 +08:00
public static void Warning<T>(object x, int debugLevel = 0)
2023-08-12 01:43:24 +08:00
{
2023-08-23 01:59:40 +08:00
Warning($"{typeof(T).Name}:{x}", debugLevel);
2023-08-12 01:43:24 +08:00
}
}
}