UnityLog.cs 8.5 KB

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