| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150 |
- using System;
- using UnityEngine;
- namespace TapSDK.Core.Internal.Log
- {
- public class TapLog
- {
- private const string TAG = "TapSDK";
- // 颜色常量
- private const string InfoColor = "#FFFFFF"; // 白色
- private const string WarningColor = "#FFFF00"; // 黄色
- private const string ErrorColor = "#FF0000"; // 红色
- private const string MainThreadColor = "#00FF00"; // 绿色
- private const string IOThreadColor = "#FF00FF"; // 紫色
- private const string TagColor = "#00FFFF"; // 青色
- // 开关变量,控制是否启用日志输出
- public static bool Enabled = false;
- private string module;
- private string tag;
- public TapLog(string module, string tag = TAG)
- {
- this.tag = tag;
- this.module = module;
- }
- public void Log(string message, string detail = null)
- {
- TapLog.Log(message, detail, tag, module);
- }
- // 输出带有自定义颜色和标签的警告
- public void Warning(string message, string detail = null)
- {
- TapLog.Warning(message, detail, tag, module);
- }
- // 输出带有自定义颜色和标签的错误
- public void Error(string message, string detail = null)
- {
- TapLog.Error(message, detail, tag, module);
- }
- public static void Error(Exception e)
- {
- TapLog.Error(e?.Message ?? "");
- }
- // 输出带有自定义颜色和标签的普通日志
- public static void Log(string message, string detail = null, string tag = TAG, string module = null)
- {
- if (string.IsNullOrEmpty(message))
- {
- return;
- }
- string msg = GetFormattedMessage(message: message, detail: detail, colorHex: InfoColor, tag: tag, module: module);
- if (TapLogger.LogDelegate != null)
- {
- TapLogger.Debug(msg);
- return;
- }
- if (Enabled)
- {
- Debug.Log(msg);
- }
- }
- // 输出带有自定义颜色和标签的警告
- public static void Warning(string message, string detail = null, string tag = TAG, string module = null)
- {
- if (string.IsNullOrEmpty(message))
- {
- return;
- }
- string msg = GetFormattedMessage(message: message, detail: detail, colorHex: WarningColor, tag: tag, module: module);
- if (TapLogger.LogDelegate != null)
- {
- TapLogger.Warn(msg);
- return;
- }
- if (Enabled)
- {
- Debug.LogWarning(msg);
- }
- }
- // 输出带有自定义颜色和标签的错误
- public static void Error(string message, string detail = null, string tag = TAG, string module = null)
- {
- if (string.IsNullOrEmpty(message))
- {
- return;
- }
- string msg = GetFormattedMessage(message: message, detail: detail, colorHex: ErrorColor, tag: tag, module: module);
- if (TapLogger.LogDelegate != null)
- {
- TapLogger.Error(msg);
- return;
- }
- Debug.LogError(msg);
- }
- // 格式化带有颜色和标签的消息
- private static string GetFormattedMessage(string message, string detail, string colorHex, string tag, string module)
- {
- string threadInfo = GetThreadInfo();
- string tagColor = TagColor;
- if (module != null && module != "")
- {
- tag = $"{tag}.{module}";
- }
- if (IsMobilePlatform())
- {
- return $"[{tag}] {threadInfo} {message}\n{detail}";
- }
- else
- {
- return $"<color={tagColor}>[{tag}]</color> {threadInfo} <color={colorHex}>{message}</color>\n{detail}\n";
- }
- }
- // 获取当前线程信息
- private static string GetThreadInfo()
- {
- bool isMainThread = System.Threading.Thread.CurrentThread.IsAlive && System.Threading.Thread.CurrentThread.ManagedThreadId == 1;
- string threadInfo = isMainThread ? "Main" : $"IO {System.Threading.Thread.CurrentThread.ManagedThreadId}";
- if (IsMobilePlatform())
- {
- // 移动平台的线程信息不使用颜色
- return $"({threadInfo})";
- }
- else
- {
- // 其他平台的线程信息使用颜色
- string color = isMainThread ? MainThreadColor : IOThreadColor;
- return $"<color={color}>({threadInfo})</color>";
- }
- }
- // 检查是否是移动平台
- private static bool IsMobilePlatform()
- {
- return Application.platform == RuntimePlatform.Android || Application.platform == RuntimePlatform.IPhonePlayer;
- }
- }
- }
|