DebugHelper.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System;
  6. using System.Diagnostics;
  7. using Debug = UnityEngine.Debug;
  8. using System.Runtime.InteropServices;
  9. using System.Net.Sockets;
  10. using System.Net;
  11. //adb logcat -s Unity
  12. //adb shell dumpsys meminfo com.tencent.tmgp.sgame
  13. //adb shell top -m 10 -s cpu
  14. //adb forward tcp:54999 localabstract:Unity-com.tencent.tmgp.sgame
  15. public enum SLogTypeDef
  16. {
  17. LogType_None, //不输出log
  18. LogType_System, //系统默认方式
  19. LogType_Custom, //自定义方式
  20. };
  21. public enum SLogCategory{
  22. Normal, //普通log
  23. Max
  24. };
  25. public enum LogLevel
  26. {
  27. Error,
  28. Warning,
  29. Info,
  30. }
  31. public class LogInfo
  32. {
  33. public LogType type;
  34. public string content;
  35. public string stackTree;
  36. public string channel;
  37. public LogInfo(string content_,string stackTree_,LogType type_)
  38. {
  39. content = content_;
  40. type = type_;
  41. stackTree = stackTree_;
  42. if(type == LogType.Log)
  43. {
  44. channel = "Log";
  45. }else if(type == LogType.Warning)
  46. {
  47. channel = "Warning";
  48. }else
  49. {
  50. channel = "Error";
  51. }
  52. }
  53. }
  54. public class DebugHelper : MonoBehaviour
  55. {
  56. public static string Tag_BHY = "BHY:";
  57. public static DebugHelper instance = null;
  58. public static LogLevel LogLevel = LogLevel.Warning;
  59. public static SLogTypeDef logMode = SLogTypeDef.LogType_None; //是否写入到文件中
  60. static SLogObj[] s_logers = new SLogObj[(int)SLogCategory.Max];
  61. static List<ILogger> s_loggerWnds = new List<ILogger>();
  62. //日志开关,外部可以通过这个控制日志的开启与关闭
  63. public static bool enableLog = true;
  64. static long logCnt = 1;
  65. static string s_path = "";
  66. public static void AddLoggerWnd(ILogger logger)
  67. {
  68. lock (s_loggerWnds)
  69. {
  70. if (!s_loggerWnds.Contains(logger))
  71. {
  72. s_loggerWnds.Add(logger);
  73. }
  74. }
  75. }
  76. public static void OpenLoger(SLogCategory logType, string logFile)
  77. {
  78. var idx = (int)logType;
  79. s_logers[idx].Flush();
  80. s_logers[idx].Close();
  81. s_logers[idx].TargetPath = logFile;
  82. }
  83. public static void CloseLoger(SLogCategory logType)
  84. {
  85. var idx = (int)logType;
  86. s_logers[idx].Flush();
  87. s_logers[idx].Close();
  88. s_logers[idx].TargetPath = null;
  89. }
  90. public static void BeginLogs()
  91. {
  92. ClearLogs(180);
  93. CloseLogs();
  94. if (string.IsNullOrEmpty(s_path))
  95. {
  96. string folder = logRootPath;
  97. string dt = DateTime.Now.ToString("yyyyMMdd_HHmmss");
  98. s_path = string.Format("{0}/{1}_normal.log", folder, dt);
  99. }
  100. OpenLoger(SLogCategory.Normal, string.Format("{0}_{1}",s_path,logCnt));
  101. logMode = SLogTypeDef.LogType_Custom;
  102. logCnt++;
  103. }
  104. public static void CloseLogs()
  105. {
  106. logMode = SLogTypeDef.LogType_None;
  107. CloseLoger(SLogCategory.Normal);
  108. }
  109. public static void ClearLogs(int passedMinutes = 60)
  110. {
  111. DateTime now = DateTime.Now;
  112. DirectoryInfo dirInfo = new DirectoryInfo(logRootPath);
  113. if (dirInfo.Exists)
  114. {
  115. string[] files = Directory.GetFiles(dirInfo.FullName, "*.log", SearchOption.TopDirectoryOnly);
  116. for (int i = 0; i < files.Length; ++i)
  117. {
  118. try
  119. {
  120. string filePath = files[i];
  121. FileInfo fi = new FileInfo(filePath);
  122. if (fi.Exists && (now - fi.CreationTime).TotalMinutes > passedMinutes)
  123. {
  124. File.Delete(filePath);
  125. }
  126. }
  127. catch
  128. {
  129. }
  130. }
  131. }
  132. }
  133. public static SLogObj GetLoger(SLogCategory logType)
  134. {
  135. return s_logers[(int)logType];
  136. }
  137. public static string GetLogerPath(SLogCategory logType)
  138. {
  139. return s_logers[(int)logType].LastTargetPath;
  140. }
  141. static public void EditorAssert(bool InCondition, string InFormat = null, params object[] InParameters)
  142. {
  143. Assert(InCondition, InFormat, InParameters);
  144. }
  145. static public void Assert(bool InCondition)
  146. {
  147. Assert(InCondition, null, null);
  148. }
  149. static public void Assert(bool InCondition, string InFormat )
  150. {
  151. Assert(InCondition, InFormat, null);
  152. }
  153. static public void Assert(bool InCondition, string InFormat, params object[] InParameters )
  154. {
  155. // 虽然有了Conditional了 但是保险起见 还是用宏包一层 双管齐下 双保险
  156. #if UNITY_EDITOR || UNITY_STANDALONE || UNITY_ANDROID || FORCE_LOG || UNITY_IPHONE
  157. if (!InCondition)
  158. {
  159. try
  160. {
  161. string failedMessage = null;
  162. if (!string.IsNullOrEmpty(InFormat))
  163. {
  164. try
  165. {
  166. if (InParameters != null)
  167. {
  168. failedMessage = string.Format(InFormat, InParameters);
  169. }
  170. else
  171. {
  172. failedMessage = InFormat;
  173. }
  174. }
  175. catch (Exception)
  176. {
  177. }
  178. }
  179. #if UNITY_ANDROID || UNITY_IPHONE
  180. else
  181. {
  182. failedMessage = string.Format(" no assert detail, stacktrace is :{0}", Environment.StackTrace);
  183. }
  184. #endif
  185. #if UNITY_EDITOR || UNITY_STANDALONE
  186. if (failedMessage != null)
  187. {
  188. Debug.LogError("Assert failed! " + failedMessage);
  189. }
  190. else
  191. {
  192. Debug.LogError("Assert failed!");
  193. }
  194. string msg = "Assert failed! ";
  195. if (!string.IsNullOrEmpty(failedMessage))
  196. {
  197. msg += failedMessage;
  198. }
  199. var trace = new System.Diagnostics.StackTrace();
  200. var frames = trace.GetFrames();
  201. for (int i = 0; i < frames.Length; ++i)
  202. {
  203. msg += frames[i].ToString();
  204. }
  205. try
  206. {
  207. LogInternal(SLogCategory.Normal, msg);
  208. }
  209. catch (Exception)
  210. {
  211. }
  212. #else
  213. if (failedMessage != null)
  214. {
  215. var str = "Assert : " + failedMessage;
  216. LogInternal(SLogCategory.Normal, str);
  217. }
  218. else
  219. {
  220. Debug.LogWarning("Assert failed!");
  221. }
  222. #endif
  223. }
  224. catch (Exception)
  225. {
  226. // ignore all exception in logger.
  227. }
  228. }
  229. #endif
  230. }
  231. public static void LogInternal(SLogCategory logType, string logmsg)
  232. {
  233. if (enableLog && DebugHelper.logMode == SLogTypeDef.LogType_Custom)
  234. {
  235. long curFileSize = s_logers[(int)logType].GetFileSize();
  236. #if UNITY_EDITOR
  237. if (curFileSize > 200000)
  238. #else
  239. if(curFileSize > 200)
  240. #endif
  241. {
  242. CloseLogs();
  243. BeginLogs();
  244. }
  245. string timeLogMsg = string.Format("{0}:{1}", DateTime.Now.ToString("yyyy.MM.dd-HH:mm:ss:ffff"),logmsg);
  246. s_logers[(int)logType].Log(timeLogMsg);
  247. }
  248. }
  249. private static string CachedLogRootPath;
  250. public static string logRootPath
  251. {
  252. get
  253. {
  254. if( CachedLogRootPath == null )
  255. {
  256. #if UNITY_EDITOR
  257. string folder = string.Format("{0}/../../Log/", Application.dataPath);
  258. #elif UNITY_STANDALONE
  259. string folder = string.Format("{0}/../Log/", Application.dataPath);
  260. #else
  261. string folder = string.Format("{0}/Log/", Application.persistentDataPath);
  262. #endif
  263. if (!Directory.Exists(folder))
  264. {
  265. Directory.CreateDirectory(folder);
  266. }
  267. CachedLogRootPath = folder;
  268. }
  269. return CachedLogRootPath;
  270. }
  271. }
  272. public static void Log(string logmsg)
  273. {
  274. if (enableLog && LogLevel >= LogLevel.Info)
  275. {
  276. Debug.Log(logmsg);
  277. }
  278. }
  279. public static void Log(object msg, params object[] args)
  280. {
  281. Log(_format(msg, args));
  282. }
  283. public static void LogError(string errmsg)
  284. {
  285. if (enableLog && LogLevel >= LogLevel.Error)
  286. {
  287. Debug.LogError(errmsg);
  288. }
  289. }
  290. public static void LogError(object msg, params object[] args)
  291. {
  292. LogError(_format(msg, args));
  293. }
  294. public static void LogWarning(string warmsg)
  295. {
  296. if (enableLog && LogLevel >= LogLevel.Warning)
  297. {
  298. Debug.LogWarning(warmsg);
  299. }
  300. }
  301. public static void LogWarning(object msg, params object[] args)
  302. {
  303. LogWarning(_format(msg, args));
  304. }
  305. public static void LogException(Exception ex)
  306. {
  307. if (enableLog && LogLevel >= LogLevel.Error)
  308. {
  309. Debug.LogException(ex);
  310. }
  311. }
  312. private void Awake()
  313. {
  314. DebugHelper.Assert(instance == null);
  315. instance = this;
  316. int cnt = (int)SLogCategory.Max;
  317. for (int i = 0; i < cnt; i++)
  318. {
  319. s_logers[i] = new SLogObj();
  320. }
  321. Application.logMessageReceivedThreaded += OnLogReceived;
  322. }
  323. protected void OnDestroy()
  324. {
  325. int cnt = (int)SLogCategory.Max;
  326. for (int i = 0; i < cnt; i++)
  327. {
  328. s_logers[i].Flush();
  329. s_logers[i].Close();
  330. }
  331. BackgroundWorker.DestroyInstance();
  332. Application.logMessageReceivedThreaded -= OnLogReceived;
  333. }
  334. void Update()
  335. {
  336. int cnt = (int)SLogCategory.Max;
  337. for (int i = 0; i < cnt; i++)
  338. {
  339. if (s_logers[i]!=null)
  340. {
  341. s_logers[i].Flush();
  342. }
  343. }
  344. }
  345. private void OnLogReceived(string condition, string stackTrace, LogType type)
  346. {
  347. bool canLogged = false;
  348. switch (type)
  349. {
  350. case LogType.Assert:
  351. case LogType.Exception:
  352. canLogged = true;
  353. break;
  354. case LogType.Error:
  355. canLogged = LogLevel >= LogLevel.Error;
  356. break;
  357. case LogType.Warning:
  358. canLogged = LogLevel >= LogLevel.Warning;
  359. break;
  360. case LogType.Log:
  361. default:
  362. canLogged = LogLevel >= LogLevel.Info;
  363. break;
  364. }
  365. s_loggerWnds.RemoveAll(l => l == null);
  366. //s_loggerWnds.ForEach(l => l.Log(condition,stackTrace,type));
  367. if (canLogged)
  368. {
  369. try
  370. {
  371. // 如果是异常的话,加上堆栈信息
  372. if (type == LogType.Exception)
  373. {
  374. condition = string.Format("{0}\r\n {1}", condition, stackTrace.Replace("\n", "\r\n "));
  375. }
  376. LogInternal(SLogCategory.Normal, condition);
  377. }
  378. catch (System.Exception ex)
  379. {
  380. Debug.LogException(ex); // this should at least print to Unity Editor (but may skip the file writing due to earlier writing failure)
  381. }
  382. }
  383. }
  384. private static string _format(object msg, params object[] args)
  385. {
  386. string fmt = msg as string;
  387. if (args == null || args.Length == 0 || string.IsNullOrEmpty(fmt))
  388. {
  389. return msg.ToString();
  390. }
  391. else
  392. {
  393. return string.Format(fmt, args);
  394. }
  395. }
  396. //#if UNITY_STANDALONE_WIN || UNITY_EDITOR || FORCE_LOG
  397. // private static Socket _socket = null;
  398. // private static IPEndPoint _remotePoint = null;
  399. // private static string _remoteHost = null;
  400. // private static uint _sequence = 0;
  401. // public static void LogToRemote(string logMsg, string remoteHost = "localhost")
  402. // {
  403. // try
  404. // {
  405. // if (null == _socket || remoteHost != _remoteHost)
  406. // {
  407. // _remoteHost = remoteHost;
  408. // if (null != _socket) _socket.Close();
  409. // IPHostEntry remoteEntry = Dns.GetHostEntry(_remoteHost);
  410. // IPAddress remoteAddr = null;
  411. // for (int i = 0; i < remoteEntry.AddressList.Length; ++i)
  412. // {
  413. // if (remoteEntry.AddressList[i].AddressFamily == AddressFamily.InterNetwork)
  414. // {
  415. // remoteAddr = remoteEntry.AddressList[i];
  416. // break;
  417. // }
  418. // }
  419. // if (null == remoteAddr)
  420. // {
  421. // Log("IPV4 network not found!");
  422. // return;
  423. // }
  424. // _remotePoint = new IPEndPoint(remoteAddr, 11557);
  425. // _socket = new Socket(remoteAddr.AddressFamily, SocketType.Dgram, ProtocolType.Udp);
  426. // _sequence = 0;
  427. // }
  428. // byte[] msg = System.Text.Encoding.UTF8.GetBytes((++_sequence) + ":" + logMsg);
  429. // _socket.SendTo(msg, SocketFlags.None, _remotePoint);
  430. // }
  431. // catch (Exception ecp)
  432. // {
  433. // Log(ecp.Message);
  434. // }
  435. // }
  436. //#endif
  437. }
  438. public class SLogObj
  439. {
  440. StreamWriterProxy streamWriter = null;
  441. string filePath = null;
  442. List<string> sLogTxt = new List<string>();
  443. string targetPath = null;
  444. string lastTargetPath = null;
  445. public string TargetPath
  446. {
  447. get
  448. {
  449. return targetPath;
  450. }
  451. set
  452. {
  453. targetPath = value;
  454. if (!string.IsNullOrEmpty(targetPath))
  455. lastTargetPath = targetPath;
  456. filePath = null;
  457. }
  458. }
  459. public string LastTargetPath
  460. {
  461. get { return lastTargetPath; }
  462. }
  463. bool Begin()
  464. {
  465. if (streamWriter != null)
  466. return true;
  467. bool createNew = false;
  468. if (TargetPath != null)
  469. {
  470. if (filePath != TargetPath)
  471. {
  472. filePath = targetPath;
  473. createNew = true;
  474. }
  475. }
  476. else
  477. {
  478. if (filePath == null)
  479. {
  480. createNew = true;
  481. //string dt = DateTime.Now.ToString("yyyyMMdd_HHmmss");
  482. string dt = "debug";
  483. filePath = string.Format("{0}/../ygame_{1}.txt", Application.dataPath, dt);
  484. }
  485. }
  486. try
  487. {
  488. streamWriter = new StreamWriterProxy(filePath, createNew);
  489. }
  490. catch (Exception)
  491. {
  492. return false;
  493. }
  494. return true;
  495. }
  496. void End()
  497. {
  498. streamWriter.Close();
  499. streamWriter = null;
  500. }
  501. public void Close()
  502. {
  503. Flush();
  504. if (streamWriter != null)
  505. {
  506. streamWriter.Close();
  507. }
  508. streamWriter = null;
  509. filePath = null;
  510. }
  511. public void Log(string str)
  512. {
  513. sLogTxt.Add(str);
  514. }
  515. public void Flush()
  516. {
  517. if (sLogTxt.Count > 0)
  518. {
  519. if (!Begin())
  520. return;
  521. for (int i = 0; i < sLogTxt.Count; ++i)
  522. {
  523. streamWriter.WriteLine(sLogTxt[i]);
  524. }
  525. sLogTxt.Clear();
  526. End();
  527. }
  528. }
  529. public long GetFileSize()
  530. {
  531. if (string.IsNullOrEmpty(targetPath)) return 0;
  532. FileInfo info = new FileInfo(targetPath);
  533. if (info != null && info.Exists)
  534. {
  535. return info.Length / 1024;
  536. }
  537. return 0;
  538. }
  539. }