TapLog.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. using System;
  2. using UnityEngine;
  3. namespace TapSDK.Core.Internal.Log
  4. {
  5. public class TapLog
  6. {
  7. private const string TAG = "TapSDK";
  8. // 颜色常量
  9. private const string InfoColor = "#FFFFFF"; // 白色
  10. private const string WarningColor = "#FFFF00"; // 黄色
  11. private const string ErrorColor = "#FF0000"; // 红色
  12. private const string MainThreadColor = "#00FF00"; // 绿色
  13. private const string IOThreadColor = "#FF00FF"; // 紫色
  14. private const string TagColor = "#00FFFF"; // 青色
  15. // 开关变量,控制是否启用日志输出
  16. public static bool Enabled = false;
  17. private string module;
  18. private string tag;
  19. public TapLog(string module, string tag = TAG)
  20. {
  21. this.tag = tag;
  22. this.module = module;
  23. }
  24. public void Log(string message, string detail = null)
  25. {
  26. TapLog.Log(message, detail, tag, module);
  27. }
  28. // 输出带有自定义颜色和标签的警告
  29. public void Warning(string message, string detail = null)
  30. {
  31. TapLog.Warning(message, detail, tag, module);
  32. }
  33. // 输出带有自定义颜色和标签的错误
  34. public void Error(string message, string detail = null)
  35. {
  36. TapLog.Error(message, detail, tag, module);
  37. }
  38. public static void Error(Exception e)
  39. {
  40. TapLog.Error(e?.Message ?? "");
  41. }
  42. // 输出带有自定义颜色和标签的普通日志
  43. public static void Log(string message, string detail = null, string tag = TAG, string module = null)
  44. {
  45. if (string.IsNullOrEmpty(message))
  46. {
  47. return;
  48. }
  49. string msg = GetFormattedMessage(message: message, detail: detail, colorHex: InfoColor, tag: tag, module: module);
  50. if (TapLogger.LogDelegate != null)
  51. {
  52. TapLogger.Debug(msg);
  53. return;
  54. }
  55. if (Enabled)
  56. {
  57. Debug.Log(msg);
  58. }
  59. }
  60. // 输出带有自定义颜色和标签的警告
  61. public static void Warning(string message, string detail = null, string tag = TAG, string module = null)
  62. {
  63. if (string.IsNullOrEmpty(message))
  64. {
  65. return;
  66. }
  67. string msg = GetFormattedMessage(message: message, detail: detail, colorHex: WarningColor, tag: tag, module: module);
  68. if (TapLogger.LogDelegate != null)
  69. {
  70. TapLogger.Warn(msg);
  71. return;
  72. }
  73. if (Enabled)
  74. {
  75. Debug.LogWarning(msg);
  76. }
  77. }
  78. // 输出带有自定义颜色和标签的错误
  79. public static void Error(string message, string detail = null, string tag = TAG, string module = null)
  80. {
  81. if (string.IsNullOrEmpty(message))
  82. {
  83. return;
  84. }
  85. string msg = GetFormattedMessage(message: message, detail: detail, colorHex: ErrorColor, tag: tag, module: module);
  86. if (TapLogger.LogDelegate != null)
  87. {
  88. TapLogger.Error(msg);
  89. return;
  90. }
  91. Debug.LogError(msg);
  92. }
  93. // 格式化带有颜色和标签的消息
  94. private static string GetFormattedMessage(string message, string detail, string colorHex, string tag, string module)
  95. {
  96. string threadInfo = GetThreadInfo();
  97. string tagColor = TagColor;
  98. if (module != null && module != "")
  99. {
  100. tag = $"{tag}.{module}";
  101. }
  102. if (IsMobilePlatform())
  103. {
  104. return $"[{tag}] {threadInfo} {message}\n{detail}";
  105. }
  106. else
  107. {
  108. return $"<color={tagColor}>[{tag}]</color> {threadInfo} <color={colorHex}>{message}</color>\n{detail}\n";
  109. }
  110. }
  111. // 获取当前线程信息
  112. private static string GetThreadInfo()
  113. {
  114. bool isMainThread = System.Threading.Thread.CurrentThread.IsAlive && System.Threading.Thread.CurrentThread.ManagedThreadId == 1;
  115. string threadInfo = isMainThread ? "Main" : $"IO {System.Threading.Thread.CurrentThread.ManagedThreadId}";
  116. if (IsMobilePlatform())
  117. {
  118. // 移动平台的线程信息不使用颜色
  119. return $"({threadInfo})";
  120. }
  121. else
  122. {
  123. // 其他平台的线程信息使用颜色
  124. string color = isMainThread ? MainThreadColor : IOThreadColor;
  125. return $"<color={color}>({threadInfo})</color>";
  126. }
  127. }
  128. // 检查是否是移动平台
  129. private static bool IsMobilePlatform()
  130. {
  131. return Application.platform == RuntimePlatform.Android || Application.platform == RuntimePlatform.IPhonePlayer;
  132. }
  133. }
  134. }