LogTool.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. using System;
  2. using System.Text;
  3. using Core.Utility;
  4. #if COMBAT_SERVER
  5. using NLog;
  6. #endif
  7. #if !COMBAT_SERVER
  8. using UnityEngine;
  9. #endif
  10. using Object = System.Object;
  11. namespace Fort23.UTool
  12. {
  13. public class LogTool
  14. {
  15. public static ILogSend LogSend;
  16. public static string ColorForGreen = "green";
  17. public static string ColorForOrange = "orange";
  18. public static string ColorForRed = "red";
  19. #if COMBAT_SERVER
  20. public static NLog.Logger logger
  21. {
  22. get
  23. {
  24. if (_logger == null)
  25. {
  26. _logger = NLog.LogManager.GetCurrentClassLogger();
  27. }
  28. return _logger;
  29. }
  30. }
  31. private static NLog.Logger _logger;
  32. #endif
  33. #if UNITY_EDITOR
  34. public static bool IsDebug = true;
  35. #else
  36. #if COMBAT_SERVER
  37. public static bool IsDebug = true;
  38. #else
  39. public static bool IsDebug = false;
  40. #endif
  41. #endif
  42. private static StringBuilder ColorLog(object str, string color)
  43. {
  44. string colorFormat = color;
  45. StringBuilder sb = new StringBuilder();
  46. sb.AppendFormat("<color={1}>[{0}]</color>", str, colorFormat);
  47. return sb;
  48. }
  49. public static void Log(object msg)
  50. {
  51. if (!IsDebug)
  52. {
  53. return;
  54. }
  55. #if !COMBAT_SERVER
  56. Debug.Log(ColorLog(msg, ColorForGreen));
  57. #elif COMBAT_SERVER
  58. logger.Log(LogLevel.Info, msg);
  59. #endif
  60. }
  61. public static void Log(string msg)
  62. {
  63. if (!IsDebug)
  64. {
  65. return;
  66. }
  67. #if !COMBAT_SERVER
  68. Debug.Log(ColorLog(msg, ColorForGreen));
  69. #elif COMBAT_SERVER
  70. logger.Log(LogLevel.Info, msg);
  71. // logger.Log(LogLevel.Info, msg);
  72. #endif
  73. }
  74. public static void Warning(string msg)
  75. {
  76. #if!COMBAT_SERVER
  77. Debug.LogWarning(ColorLog(msg, ColorForOrange));
  78. #elif COMBAT_SERVER
  79. logger.Log(LogLevel.Warn, msg);
  80. #endif
  81. }
  82. /// <summary>
  83. /// 错误是任何情况下都会打印。
  84. /// </summary>
  85. /// <param name="msg"></param>
  86. public static void Error(string msg)
  87. {
  88. // System.Diagnostics.StackTrace stackTrace= new System.Diagnostics.StackTrace();
  89. LogSend?.SendError(msg);
  90. #if !COMBAT_SERVER
  91. Debug.LogError(ColorLog(msg, ColorForRed));
  92. // Debug.LogError(msg);
  93. #elif COMBAT_SERVER
  94. logger.Error(msg);
  95. #endif
  96. }
  97. /// <summary>
  98. /// 错误是任何情况下都会打印。
  99. /// </summary>
  100. /// <param name="e"></param>
  101. public static void Error(Exception e)
  102. {
  103. LogSend?.SendException(e);
  104. #if !COMBAT_SERVER
  105. Exception(e);
  106. #elif COMBAT_SERVER
  107. logger.Error(e);
  108. #endif
  109. }
  110. public static void Exception(Exception e)
  111. {
  112. LogSend?.SendException(e);
  113. #if !COMBAT_SERVER
  114. Debug.LogException(e);
  115. #elif COMBAT_SERVER
  116. Console.WriteLine(e);
  117. logger.Error(e);
  118. #endif
  119. }
  120. #if UNITY_EDITOR
  121. /// <summary>
  122. /// 暂停游戏
  123. /// </summary>
  124. public static void Paused()
  125. {
  126. UnityEditor.EditorApplication.isPaused = true;
  127. }
  128. #endif
  129. }
  130. }