BITKit/Src/Core/BITLog/BIT4Log.cs

76 lines
2.0 KiB
C#
Raw Normal View History

2023-06-05 19:57:17 +08:00
using System;
2023-10-24 23:38:22 +08:00
using System.Diagnostics;
2024-06-14 14:12:02 +08:00
#if UNITY_5_3_OR_NEWER
2023-10-24 23:38:22 +08:00
using UnityEngine;
#endif
2023-06-05 19:57:17 +08:00
namespace BITKit
{
public static class BIT4Log
{
2024-06-14 16:16:13 +08:00
#if UNITY_EDITOR && UNITY_5_3_OR_NEWER
2023-10-24 23:38:22 +08:00
[RuntimeInitializeOnLoadMethod]
2023-08-11 23:57:37 +08:00
private static void Reload()
2023-06-05 19:57:17 +08:00
{
OnLog = null;
OnException = null;
OnWarning = null;
2023-08-11 23:57:37 +08:00
OnSetConsoleColor = null;
2023-06-05 19:57:17 +08:00
OnNextLine = null;
}
2023-08-11 23:57:37 +08:00
#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;
2024-06-14 14:12:02 +08:00
#if UNITY_5_3_OR_NEWER
2023-11-15 23:55:06 +08:00
//[HideInCallstack]
2023-10-24 23:38:22 +08:00
#endif
2023-11-15 23:55:06 +08:00
public static void Log(object x, ConsoleColor color = ConsoleColor.White)
2023-06-05 19:57:17 +08:00
{
2023-08-23 01:59:26 +08:00
OnSetConsoleColor?.Invoke(color);
OnLog?.Invoke(x?.ToString());
2023-06-05 19:57:17 +08:00
}
2024-06-14 14:12:02 +08:00
#if UNITY_5_3_OR_NEWER
2023-10-24 23:38:22 +08:00
[HideInCallstack]
#endif
2023-11-15 23:55:06 +08:00
public static void Log<T>(object x, ConsoleColor color = ConsoleColor.White)
2023-06-05 19:57:17 +08:00
{
2023-08-23 01:59:26 +08:00
if (currentType != typeof(T))
2023-06-05 19:57:17 +08:00
{
OnNextLine?.Invoke();
}
2023-08-23 01:59:26 +08:00
#if NET5_0_OR_GREATER
2023-11-15 23:55:06 +08:00
Log($"[{DateTime.Now}]{typeof(T).Name}:{x}");
2023-08-23 01:59:26 +08:00
#else
2024-03-31 23:31:00 +08:00
Log($"<color=#add8e6ff><b>{typeof(T).Name}</b></color>:{x}");
2023-08-23 01:59:26 +08:00
#endif
2023-06-05 19:57:17 +08:00
currentType = typeof(T);
}
2024-06-14 14:12:02 +08:00
#if UNITY_5_3_OR_NEWER
2023-10-24 23:38:22 +08:00
[HideInCallstack]
#endif
2023-06-05 19:57:17 +08:00
public static void LogException(Exception e)
{
OnException?.Invoke(e);
}
2024-06-14 14:12:02 +08:00
#if UNITY_5_3_OR_NEWER
2023-10-24 23:38:22 +08:00
[HideInCallstack]
#endif
2023-11-15 23:55:06 +08:00
public static void Warning(object x)
2023-06-05 19:57:17 +08:00
{
OnWarning?.Invoke(x.ToString());
}
2024-06-14 14:12:02 +08:00
#if UNITY_5_3_OR_NEWER
2023-10-24 23:38:22 +08:00
[HideInCallstack]
#endif
2023-11-15 23:55:06 +08:00
public static void Warning<T>(object x)
2023-06-05 19:57:17 +08:00
{
2024-03-31 23:31:00 +08:00
Warning($"<color=#ffa500ff><b>{typeof(T).Name}</b></color>:{x}");
2023-06-05 19:57:17 +08:00
}
}
}