UnityLog.cs 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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. /*
  81. if (BuildConfigMgr.Instance.IsInit) { }*/
  82. //LogPath = Path.Combine(BuildConfig.Instance.UserPath, "LogData");
  83. LogPath = Path.Combine(Application.persistentDataPath, "LogData");
  84. if (!Directory.Exists(LogPath))
  85. {
  86. Directory.CreateDirectory(LogPath);
  87. }
  88. else
  89. {
  90. ///缓存文件最大只留10M,超过10M后,清除日志
  91. DirectoryInfo di = new DirectoryInfo(LogPath);
  92. long len = 0;
  93. foreach (FileInfo item in di.GetFiles())
  94. {
  95. len += item.Length;
  96. }
  97. if (len > 10 * UnityUtil.MB)
  98. {
  99. Directory.Delete(LogPath, true);
  100. }
  101. Directory.CreateDirectory(LogPath);
  102. }
  103. Application.logMessageReceived += LogMessageReceived;
  104. //InitLog();
  105. }
  106. /// <summary>
  107. /// 接受系统日志
  108. /// </summary>
  109. /// <param name="condition"></param>
  110. /// <param name="stackTrace"></param>
  111. /// <param name="type"></param>
  112. private static void LogMessageReceived(string condition, string stackTrace, LogType type)
  113. {
  114. if (!isOutLogEditor)
  115. {
  116. return;
  117. }
  118. logBuilder = null;
  119. FileStream fileStream = null;
  120. if (string.IsNullOrEmpty(condition) && string.IsNullOrEmpty(stackTrace))
  121. {
  122. return;
  123. }
  124. bool isWriteFrist = false;
  125. #if UNITY_EDITOR || !BUILDTYPE_RELEASE //Release
  126. if (type == LogType.Log)
  127. {
  128. ///日志等级大于0时才会开启日志
  129. if (logLevel >= 1)
  130. {
  131. fileStream = LogStream;
  132. }
  133. if (isFristLog)
  134. {
  135. isWriteFrist = true;
  136. isFristLog = false;
  137. }
  138. }
  139. #endif
  140. if (type == LogType.Exception)
  141. {
  142. fileStream = ExceptStream;
  143. if (isFristLogExcept)
  144. {
  145. isWriteFrist = true;
  146. isFristLogExcept = false;
  147. }
  148. }
  149. else if (type == LogType.Error)
  150. {
  151. fileStream = ErrorStream;
  152. if (isFristLogError)
  153. {
  154. isWriteFrist = true;
  155. isFristLogError = false;
  156. }
  157. }
  158. if (fileStream != null)
  159. {
  160. DateTime time = DateTime.Now;
  161. logBuilder = time + ": " + condition + stackTrace + "\n";
  162. MessageReceived?.Invoke(logBuilder);
  163. if (isWriteFrist)
  164. {
  165. /*
  166. if (BuildConfigMgr.Instance.IsInit)
  167. {
  168. ReleaseLog("\n----------Version Log------------\n" +
  169. Application.productName + "_" + Application.version + "_" + JsonConvert.SerializeObject(BuildConfig.Instance) + "\n",
  170. fileStream);
  171. }*/
  172. }
  173. ReleaseLog(logBuilder, fileStream);
  174. fileStream.Flush();
  175. }
  176. fileStream = null;
  177. logBuilder = null;
  178. }
  179. /// <summary>
  180. /// 释放资源
  181. /// </summary>
  182. public static void ReleaseLog()
  183. {
  184. Application.logMessageReceived -= LogMessageReceived;
  185. if (logStream != null)
  186. {
  187. logStream.Close();
  188. }
  189. if (errorStream != null)
  190. {
  191. errorStream.Close();
  192. }
  193. if (exceptStream != null)
  194. {
  195. exceptStream.Close();
  196. }
  197. logStream = null;
  198. errorStream = null;
  199. exceptStream = null;
  200. }
  201. public static void Log(object log, int level = 1)
  202. {
  203. //Log(log != null ? log.ToString() : "null", logLevel);
  204. ///如果是编辑器模式或者非商用APP版本,可以打日志文件
  205. #if UNITY_EDITOR || !BuildType_RELEASE //Release
  206. bool isForce = false;
  207. #if UNITY_EDITOR
  208. isForce = logLevel >= 4;
  209. #endif
  210. if (isForce || level <= logLevel)
  211. {
  212. Debug.Log(log != null ? log.ToString() : NULL);
  213. }
  214. #endif
  215. }
  216. /// <summary>
  217. /// 正常的日志输出
  218. /// </summary>
  219. public static void Log(string log, int level = 1)
  220. {
  221. ///如果是编辑器模式或者非商用APP版本,可以打日志文件
  222. #if UNITY_EDITOR || !BuildType_RELEASE //Release
  223. bool isForce = false;
  224. #if UNITY_EDITOR
  225. isForce = logLevel >= 4;
  226. #endif
  227. if (isForce || level <= logLevel)
  228. {
  229. Debug.Log(log);
  230. }
  231. #endif
  232. }
  233. /// <summary>
  234. /// 错误日志输出
  235. /// </summary>
  236. public static void LogError(string log, int logLevel = 1)
  237. {
  238. ///错误日志任何模式都会输出
  239. Debug.Log(log);
  240. }
  241. /// <summary>
  242. /// 错误日志输出
  243. /// </summary>
  244. public static void LogException(Exception ex, int level = 1)
  245. {
  246. ///异常日志由系统自动输出
  247. Debug.LogException(ex);
  248. }
  249. /// <summary>
  250. /// 日志输出,替换对应的日志文件
  251. /// </summary>
  252. public static void Log(string log, LogType logType, int logLevel = 1)
  253. {
  254. if (logType == LogType.Log)
  255. {
  256. Log(log, logLevel);
  257. }
  258. else if (logType == LogType.Error)
  259. {
  260. LogError(log, logLevel);
  261. }
  262. }
  263. /// <summary>
  264. /// 释放资源
  265. /// </summary>
  266. public static void ReleaseLog(string msg, FileStream stream)
  267. {
  268. if (!string.IsNullOrEmpty(msg))
  269. {
  270. WriteLogToFile(msg, stream);
  271. }
  272. }
  273. /// <summary>
  274. /// 将日志写入到文件中
  275. /// </summary>
  276. /// <param name="log"></param>
  277. /// <param name="logType"></param>
  278. public static void WriteLogToFile(string log, FileStream stream)
  279. {
  280. if (stream != null)
  281. {
  282. ///UTF8的编码格式转化成byte字节流
  283. byte[] buttf = Encoding.Default.GetBytes(log);
  284. stream.Write(buttf, 0, log.Length);
  285. }
  286. }
  287. public static void ClearAllLog()
  288. {
  289. ReleaseLog();
  290. if (Directory.Exists(LogPath))
  291. {
  292. Directory.Delete(LogPath, true);
  293. }
  294. Directory.CreateDirectory(LogPath);
  295. //InitLog(FileMode.Create);
  296. }
  297. }
  298. }