GameLog.cs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. using System;
  2. using System.IO;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. /// <summary>
  6. /// 系统日志模块
  7. /// </summary>
  8. public static class GameLog
  9. {
  10. public static bool EnableLog = true; // 是否启用日志,仅可控制普通级别的日志的启用与关闭,LogError和LogWarn都是始终启用的。
  11. public static bool EnableSave = false; // 是否允许保存日志,即把日志写入到文件中
  12. public static bool IsOpenNetPackageLog = false;// 是否开放网络日志
  13. public static string LogFileDir = null;
  14. public static string LogFileName = "";
  15. public static string Prefix = ""; // 用于与Unity默认的系统日志做区分。本日志系统输出的日志头部都会带上这个标记。
  16. public static StreamWriter LogFileWriter = null;
  17. /// <summary>
  18. /// 日志输出最大行数
  19. /// </summary>
  20. private static readonly int LineCount = 100;
  21. //日志列表
  22. private static List<KeyValuePair<int, string>> ListBugs = new List<KeyValuePair<int, string>>();
  23. //第一次执行打印log
  24. private static bool FirstLogTag = true;
  25. private static string GetLogTime()
  26. {
  27. string str = "";
  28. //str = DateTime.Now.ToString("HH:mm:ss.fff") + " ";
  29. return str;
  30. }
  31. private static void AddLogToList(int key, string str)
  32. {
  33. if (ListBugs.Count > LineCount)
  34. ListBugs.RemoveAt(0);
  35. ListBugs.Add(new KeyValuePair<int, string>(key, str));
  36. }
  37. public static void Log(string message, params object[] args)
  38. {
  39. if (!EnableLog)
  40. return;
  41. if (args != null && args.Length > 0)
  42. message = string.Format(message, args);
  43. string str = Prefix + message;
  44. AddLogToList(1, str);
  45. LogToFile("[I]" + str, false);
  46. }
  47. public static void RawLog(object message)
  48. {
  49. Debug.Log(message);
  50. }
  51. public static void LogException(Exception message)
  52. {
  53. string str = GetLogTime() + message.Message;
  54. AddLogToList(2, str);
  55. Debug.LogException(message);
  56. LogToFile("[E]" + str, true);
  57. }
  58. public static void LogError(string message, params object[] args)
  59. {
  60. if (args != null && args.Length > 0)
  61. {
  62. message = string.Format(message, args);
  63. }
  64. string str = Prefix + GetLogTime() + message;
  65. AddLogToList(3, str);
  66. Debug.LogError(str, null);
  67. LogToFile("[E]" + str, true);
  68. }
  69. public static void LogWarning(string message, params object[] args)
  70. {
  71. if (args != null && args.Length > 0)
  72. {
  73. message = string.Format(message, args);
  74. }
  75. string str = Prefix + GetLogTime() + message;
  76. AddLogToList(4, str);
  77. Debug.LogWarning(str, null);
  78. LogToFile("[W]" + str, true);
  79. }
  80. private static void Warning(object message)
  81. {
  82. string str = Prefix + message;
  83. AddLogToList(5, str);
  84. Debug.LogWarning(str, null);
  85. LogToFile("[W]" + str, false);
  86. }
  87. #region 战斗
  88. #endregion
  89. #region Format
  90. public static void LogFormat(string message, params object[] args)
  91. {
  92. Log(message, args);
  93. }
  94. public static void LogErrorFormat(string message, params object[] args)
  95. {
  96. LogError(message, args);
  97. }
  98. public static void LogWarningFormat(string message, params object[] args)
  99. {
  100. LogWarning(message, args);
  101. }
  102. #endregion
  103. /// <summary>
  104. /// 将日志写入到文件中
  105. /// </summary>
  106. /// <param name="message"></param>
  107. /// <param name="EnableStack"></param>
  108. private static void LogToFile(string message, bool EnableStack = false)
  109. {
  110. if (!EnableSave)
  111. return;
  112. if (LogFileWriter == null)
  113. {
  114. LogFileName = DateTime.Now.GetDateTimeFormats('s')[0].ToString();
  115. LogFileName = LogFileName.Replace("-", "_");
  116. LogFileName = LogFileName.Replace(":", "_");
  117. LogFileName = LogFileName.Replace(" ", "");
  118. LogFileName = LogFileName + ".log";
  119. if (string.IsNullOrEmpty(LogFileDir))
  120. {
  121. try
  122. {
  123. #if UNITY_EDITOR
  124. if (!Directory.Exists("d:/UnityLog"))
  125. {
  126. Directory.CreateDirectory("d:/UnityLog");
  127. }
  128. LogFileDir = "d:/UnityLog/";
  129. #else
  130. if ((Application.platform == RuntimePlatform.Android) || (Application.platform == RuntimePlatform.IPhonePlayer))
  131. {
  132. LogFileDir = Application.persistentDataPath + "/DebuggerLog/";
  133. }
  134. #endif
  135. }
  136. catch (Exception exception)
  137. {
  138. UnityEngine.Debug.Log(Prefix + "获取 Application.persistentDataPath 报错!" + exception.Message, null);
  139. return;
  140. }
  141. }
  142. string path = LogFileDir + LogFileName;
  143. try
  144. {
  145. if (!Directory.Exists(LogFileDir))
  146. {
  147. Directory.CreateDirectory(LogFileDir);
  148. }
  149. LogFileWriter = File.AppendText(path);
  150. LogFileWriter.AutoFlush = true;
  151. }
  152. catch (Exception exception2)
  153. {
  154. LogFileWriter = null;
  155. UnityEngine.Debug.Log("LogToCache() " + exception2.Message + exception2.StackTrace, null);
  156. return;
  157. }
  158. }
  159. if (LogFileWriter != null)
  160. {
  161. try
  162. {
  163. if (FirstLogTag)
  164. {
  165. FirstLogTag = false;
  166. PhoneSystemInfo(LogFileWriter);
  167. }
  168. LogFileWriter.WriteLine(message);
  169. if (EnableStack)
  170. {
  171. LogFileWriter.WriteLine(StackTraceUtility.ExtractStackTrace());
  172. }
  173. }
  174. catch (Exception)
  175. {
  176. }
  177. }
  178. }
  179. private static void PhoneSystemInfo(StreamWriter sw)
  180. {
  181. sw.WriteLine("*********************************************************************************************************start");
  182. sw.WriteLine("By " + SystemInfo.deviceName);
  183. DateTime now = DateTime.Now;
  184. sw.WriteLine(string.Concat(new object[] { now.Year.ToString(), "年", now.Month.ToString(), "月", now.Day, "日 ", now.Hour.ToString(), ":", now.Minute.ToString(), ":", now.Second.ToString() }));
  185. sw.WriteLine();
  186. sw.WriteLine("操作系统: " + SystemInfo.operatingSystem);
  187. sw.WriteLine("系统内存大小: " + SystemInfo.systemMemorySize);
  188. sw.WriteLine("设备模型: " + SystemInfo.deviceModel);
  189. sw.WriteLine("设备唯一标识符: " + SystemInfo.deviceUniqueIdentifier);
  190. sw.WriteLine("处理器数量: " + SystemInfo.processorCount);
  191. sw.WriteLine("处理器类型: " + SystemInfo.processorType);
  192. sw.WriteLine("显卡标识符: " + SystemInfo.graphicsDeviceID);
  193. sw.WriteLine("显卡名称: " + SystemInfo.graphicsDeviceName);
  194. sw.WriteLine("显卡标识符: " + SystemInfo.graphicsDeviceVendorID);
  195. sw.WriteLine("显卡厂商: " + SystemInfo.graphicsDeviceVendor);
  196. sw.WriteLine("显卡版本: " + SystemInfo.graphicsDeviceVersion);
  197. sw.WriteLine("显存大小: " + SystemInfo.graphicsMemorySize);
  198. sw.WriteLine("显卡着色器级别: " + SystemInfo.graphicsShaderLevel);
  199. //sw.WriteLine("是否图像效果: " + SystemInfo.supportsImageEffects);
  200. sw.WriteLine("是否支持内置阴影: " + SystemInfo.supportsShadows);
  201. sw.WriteLine("*********************************************************************************************************end");
  202. sw.WriteLine("LogInfo:");
  203. sw.WriteLine();
  204. }
  205. public static void CloseLog()
  206. {
  207. if (LogFileWriter != null)
  208. {
  209. try
  210. {
  211. LogFileWriter.Flush();
  212. LogFileWriter.Close();
  213. LogFileWriter.Dispose();
  214. LogFileWriter = null;
  215. }
  216. catch (Exception)
  217. {
  218. }
  219. }
  220. }
  221. public enum LogPackageType
  222. {
  223. ClientToServer = 0,
  224. ServerToClient = 1
  225. }
  226. public static void ShowPackageInfo(LogPackageType type, string info, int cmd)
  227. {
  228. if (IsOpenNetPackageLog)
  229. {
  230. if (type == LogPackageType.ClientToServer)
  231. {
  232. GameLog.Warning(string.Format("{0} <color=#ffff00>{1}</color> - {2}", info, cmd.ToString(), DateTime.Now.ToString()));
  233. }
  234. else
  235. {
  236. GameLog.Warning(string.Format("{0} <color=#01c00f>{1}</color> - {2}", info, cmd.ToString(), DateTime.Now.ToString()));
  237. }
  238. }
  239. }
  240. }