UnityLog.cs 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. using Newtonsoft.Json;
  2. using System;
  3. using System.IO;
  4. using System.Text;
  5. using UnityEngine;
  6. namespace XRTool.Util
  7. {
  8. /// <summary>
  9. /// 日志输出,请勿使用Debug或者Print等自带的日志
  10. /// 日志分级,普通日志,错误日志,异常日志
  11. /// debug模式可以输出普通日志,其他日志必输出
  12. /// 通过配置参数实现不同的日志等级
  13. /// 为了避免大量日志输出,采用优化方案写入日志
  14. /// </summary>
  15. public static class UnityLog
  16. {
  17. private static string logBuilder;
  18. private static FileStream logStream;
  19. private static FileStream errorStream;
  20. private static FileStream exceptStream;
  21. public static event Action<string> MessageReceived;
  22. /// <summary>
  23. /// 在编辑器中是否输出日志文件
  24. /// </summary>
  25. public static bool isOutLogEditor = true;
  26. /// <summary>
  27. /// 日志等级,默认为1,正式发布版本为0
  28. /// 等级越高代表可输出的日志越多
  29. /// </summary>
  30. public static int logLevel = 1;
  31. public const string NULL = "null";
  32. private static bool isFristLog = true;
  33. private static bool isFristLogError = true;
  34. private static bool isFristLogExcept = true;
  35. private static string logPath;
  36. public static FileStream LogStream
  37. {
  38. get
  39. {
  40. if (logStream == null)
  41. {
  42. logStream = new FileStream(Path.Combine(LogPath, "log.txt"), FileMode.Append);
  43. }
  44. return logStream;
  45. }
  46. }
  47. public static FileStream ErrorStream
  48. {
  49. get
  50. {
  51. if (errorStream == null)
  52. {
  53. errorStream = new FileStream(Path.Combine(LogPath, "error.txt"), FileMode.Append);
  54. }
  55. return errorStream;
  56. }
  57. }
  58. public static FileStream ExceptStream
  59. {
  60. get
  61. {
  62. if (exceptStream == null)
  63. {
  64. exceptStream = new FileStream(Path.Combine(LogPath, "except.txt"), FileMode.Append);
  65. }
  66. return exceptStream;
  67. }
  68. }
  69. public static string LogPath { get => logPath; set => logPath = value; }
  70. static UnityLog()
  71. {
  72. if (BuildConfigMgr.Instance.IsInit)
  73. {
  74. }
  75. LogPath = Path.Combine(BuildConfig.Instance.UserPath, "LogData");
  76. if (!Directory.Exists(LogPath))
  77. {
  78. Directory.CreateDirectory(LogPath);
  79. }
  80. Application.logMessageReceived += LogMessageReceived;
  81. //InitLog();
  82. }
  83. /// <summary>
  84. /// 接受系统日志
  85. /// </summary>
  86. /// <param name="condition"></param>
  87. /// <param name="stackTrace"></param>
  88. /// <param name="type"></param>
  89. private static void LogMessageReceived(string condition, string stackTrace, LogType type)
  90. {
  91. if (!isOutLogEditor)
  92. {
  93. return;
  94. }
  95. logBuilder = null;
  96. FileStream fileStream = null;
  97. if (string.IsNullOrEmpty(condition) && string.IsNullOrEmpty(stackTrace))
  98. {
  99. return;
  100. }
  101. bool isWriteFrist = false;
  102. #if UNITY_EDITOR || !BUILDTYPE_RELEASE //Release
  103. if (type == LogType.Log)
  104. {
  105. ///日志等级大于0时才会开启日志
  106. if (logLevel >= 1)
  107. {
  108. fileStream = LogStream;
  109. }
  110. if (isFristLog)
  111. {
  112. isWriteFrist = true;
  113. isFristLog = false;
  114. }
  115. }
  116. #endif
  117. if (type == LogType.Exception)
  118. {
  119. fileStream = ExceptStream;
  120. if (isFristLogExcept)
  121. {
  122. isWriteFrist = true;
  123. isFristLogExcept = false;
  124. }
  125. }
  126. else if (type == LogType.Error)
  127. {
  128. fileStream = ErrorStream;
  129. if (isFristLogError)
  130. {
  131. isWriteFrist = true;
  132. isFristLogError = false;
  133. }
  134. }
  135. if (fileStream != null)
  136. {
  137. DateTime time = DateTime.Now;
  138. logBuilder = time + ": " + condition + stackTrace + "\n";
  139. MessageReceived?.Invoke(logBuilder);
  140. if (isWriteFrist)
  141. {
  142. if (BuildConfigMgr.Instance.IsInit)
  143. {
  144. ReleaseLog("\n----------Version Log------------\n" +
  145. Application.productName + "_" + Application.version + "_" + JsonConvert.SerializeObject(BuildConfig.Instance) + "\n",
  146. fileStream);
  147. }
  148. }
  149. ReleaseLog(logBuilder, fileStream);
  150. fileStream.Flush();
  151. }
  152. fileStream = null;
  153. logBuilder = null;
  154. }
  155. /// <summary>
  156. /// 释放资源
  157. /// </summary>
  158. public static void ReleaseLog()
  159. {
  160. Application.logMessageReceived -= LogMessageReceived;
  161. if (logStream != null)
  162. {
  163. logStream.Close();
  164. }
  165. if (errorStream != null)
  166. {
  167. errorStream.Close();
  168. }
  169. if (exceptStream != null)
  170. {
  171. exceptStream.Close();
  172. }
  173. logStream = null;
  174. errorStream = null;
  175. exceptStream = null;
  176. }
  177. public static void Log(object log, int level = 1)
  178. {
  179. //Log(log != null ? log.ToString() : "null", logLevel);
  180. ///如果是编辑器模式或者非商用APP版本,可以打日志文件
  181. #if UNITY_EDITOR || !BuildType_RELEASE //Release
  182. bool isForce = false;
  183. #if UNITY_EDITOR
  184. isForce = logLevel >= 4;
  185. #endif
  186. if (isForce || level <= logLevel)
  187. {
  188. Debug.Log(log != null ? log.ToString() : NULL);
  189. }
  190. #endif
  191. }
  192. /// <summary>
  193. /// 正常的日志输出
  194. /// </summary>
  195. public static void Log(string log, int level = 1)
  196. {
  197. ///如果是编辑器模式或者非商用APP版本,可以打日志文件
  198. #if UNITY_EDITOR || !BuildType_RELEASE //Release
  199. bool isForce = false;
  200. #if UNITY_EDITOR
  201. isForce = logLevel >= 4;
  202. #endif
  203. if (isForce || level <= logLevel)
  204. {
  205. Debug.Log(log);
  206. }
  207. #endif
  208. }
  209. /// <summary>
  210. /// 错误日志输出
  211. /// </summary>
  212. public static void LogError(string log, int logLevel = 1)
  213. {
  214. ///错误日志任何模式都会输出
  215. Debug.LogError(log);
  216. }
  217. /// <summary>
  218. /// 错误日志输出
  219. /// </summary>
  220. public static void LogException(Exception ex, int level = 1)
  221. {
  222. ///异常日志由系统自动输出
  223. Debug.LogException(ex);
  224. }
  225. /// <summary>
  226. /// 日志输出,替换对应的日志文件
  227. /// </summary>
  228. public static void Log(string log, LogType logType, int logLevel = 1)
  229. {
  230. if (logType == LogType.Log)
  231. {
  232. Log(log, logLevel);
  233. }
  234. else if (logType == LogType.Error)
  235. {
  236. LogError(log, logLevel);
  237. }
  238. }
  239. /// <summary>
  240. /// 释放资源
  241. /// </summary>
  242. public static void ReleaseLog(string msg, FileStream stream)
  243. {
  244. if (!string.IsNullOrEmpty(msg))
  245. {
  246. WriteLogToFile(msg, stream);
  247. }
  248. }
  249. /// <summary>
  250. /// 将日志写入到文件中
  251. /// </summary>
  252. /// <param name="log"></param>
  253. /// <param name="logType"></param>
  254. public static void WriteLogToFile(string log, FileStream stream)
  255. {
  256. if (stream != null)
  257. {
  258. ///UTF8的编码格式转化成byte字节流
  259. byte[] buttf = Encoding.Default.GetBytes(log);
  260. stream.Write(buttf, 0, log.Length);
  261. }
  262. }
  263. public static void ClearAllLog()
  264. {
  265. ReleaseLog();
  266. if (Directory.Exists(LogPath))
  267. {
  268. Directory.Delete(LogPath, true);
  269. }
  270. Directory.CreateDirectory(LogPath);
  271. //InitLog(FileMode.Create);
  272. }
  273. }
  274. }