Unity 游戏框架搭建 (八) 减少加班利器-QLog

为毛要实现这个工具?

在我小时候,每当游戏到了测试阶段,交给 QA 测试, QA 测试了一会儿拿着设备过来说游戏闪退了。。。。当我拿到设备后测了好久 Bug 也没有复现,排查了好久也没有头绪,就算接了 Bugly 拿到的也只是闪退的异常信息,或者干脆拿不到。很抓狂,因为这个我是没少加班。所以当时想着解决下这个小小的痛点。。。

现在框架中的QLog:

怎么用呢?在初始化的地方调用这句话就够了。

  1. QLog.Instance ();

其实做成单例也没有必要。。。。

日志获取方法:

PC端或者Mac端,日志存放在工程的如下位置:8.减少加班利器-QLog  - 图1打开之后是这样的:8.减少加班利器-QLog  - 图2最后一条信息是触发了一个空指针异常,堆栈信息一目了然。如果是iOS端,需要使用类似同步推或者iTools工具获取日志文件,路径是这样的:8.减少加班利器-QLog  - 图3Android端的话,类似的方式,但是具体路径没查过,不好意思。。。

初版

一开始想做一个保存 Debug.Log、Debug.LogWaring、Debug.LogError 信息奥本地文件的小工具。上网一查原来有大神实现了,贴上链接: http://www.xuanyusong.com/archives/2477。其思路是使用 Application.RegisterLocCallback 注册回调,每次使用 Debug.Log 等 API 时候会触发一次回调,在回调中将Log信息保存在本地。而且意外的发现,Application.RegisterLogCallback 也能接收到异常和错误信息。所以将这份实现作为 QLog 的初版用了一段时间,发现存在一个问题,如果游戏发生闪退,好多 Log 信息没来得及存到本地,因为刷入到本地操作是通过 Update 完成的,每帧之间的间隔,其实很长。

现在的版本:

后来找到了一份实现,思路和初版一样区别是将Update改成线程来刷。Application.RegisterLogCallback 这时候已经弃用了,改成了 Application.logMessageReceived,后来又找到了 Application.logMessageReceivedThreaded。如果只是使用 Application.logMessageReceived 的时候,在真机上如果发生 Error 或者 Exception 时,收不到堆栈信息。但是使用了 Application.logMessageReceivedThreaded 就可以接收到堆栈信息了,不过在处理 Log 信息的时候要保证线程安全。

说明部分就这些吧,实现起来其实没什么难点,主要就是好好利用 Application.logMessageReceived 和 Application.logMessageReceivedThreaded 这两个API就好了。

下面贴上我的框架中的实现,这里要注意一下,这份实现依赖于上篇文章介绍的App类(已经重命名为QApp了)。接口类ILogOutput:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace QFramework
  7. {
  8. /// <summary>
  9. /// 日志输出接口
  10. /// </summary>
  11. public interface ILogOutput
  12. {
  13. /// <summary>
  14. /// 输出日志数据
  15. /// </summary>
  16. /// <param name="logData">日志数据</param>
  17. void Log(QLog.LogData logData);
  18. /// <summary>
  19. /// 关闭
  20. /// </summary>
  21. void Close();
  22. }
  23. }

接口实现类 QFileLogOutput

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Threading;
  5. using System.IO;
  6. using UnityEngine;
  7.  
  8. namespace QFramework
  9. {
  10. /// <summary>
  11. /// 文本日志输出
  12. /// </summary>
  13. public class QFileLogOutput : ILogOutput
  14. {
  15.  
  16. #if UNITY_EDITOR
  17. string mDevicePersistentPath = Application.dataPath + "/../PersistentPath";
  18. #elif UNITY_STANDALONE_WIN
  19. string mDevicePersistentPath = Application.dataPath + "/PersistentPath";
  20. #elif UNITY_STANDALONE_OSX
  21. string mDevicePersistentPath = Application.dataPath + "/PersistentPath";
  22. #else
  23. string mDevicePersistentPath = Application.persistentDataPath;
  24. #endif
  25.  
  26.  
  27. static string LogPath = "Log";
  28.  
  29. private Queue<QLog.LogData> mWritingLogQueue = null;
  30. private Queue<QLog.LogData> mWaitingLogQueue = null;
  31. private object mLogLock = null;
  32. private Thread mFileLogThread = null;
  33. private bool mIsRunning = false;
  34. private StreamWriter mLogWriter = null;
  35.  
  36. public QFileLogOutput()
  37. {
  38. QApp.Instance().onApplicationQuit += Close;
  39. this.mWritingLogQueue = new Queue<QLog.LogData>();
  40. this.mWaitingLogQueue = new Queue<QLog.LogData>();
  41. this.mLogLock = new object();
  42. System.DateTime now = System.DateTime.Now;
  43. string logName = string.Format("Q{0}{1}{2}{3}{4}{5}",
  44. now.Year, now.Month, now.Day, now.Hour, now.Minute, now.Second);
  45. string logPath = string.Format("{0}/{1}/{2}.txt", mDevicePersistentPath, LogPath, logName);
  46. if (File.Exists(logPath))
  47. File.Delete(logPath);
  48. string logDir = Path.GetDirectoryName(logPath);
  49. if (!Directory.Exists(logDir))
  50. Directory.CreateDirectory(logDir);
  51. this.mLogWriter = new StreamWriter(logPath);
  52. this.mLogWriter.AutoFlush = true;
  53. this.mIsRunning = true;
  54. this.mFileLogThread = new Thread(new ThreadStart(WriteLog));
  55. this.mFileLogThread.Start();
  56. }
  57.  
  58. void WriteLog()
  59. {
  60. while (this.mIsRunning)
  61. {
  62. if (this.mWritingLogQueue.Count == 0)
  63. {
  64. lock (this.mLogLock)
  65. {
  66. while (this.mWaitingLogQueue.Count == 0)
  67. Monitor.Wait(this.mLogLock);
  68. Queue<QLog.LogData> tmpQueue = this.mWritingLogQueue;
  69. this.mWritingLogQueue = this.mWaitingLogQueue;
  70. this.mWaitingLogQueue = tmpQueue;
  71. }
  72. }
  73. else
  74. {
  75. while (this.mWritingLogQueue.Count > 0)
  76. {
  77. QLog.LogData log = this.mWritingLogQueue.Dequeue();
  78. if (log.Level == QLog.LogLevel.ERROR)
  79. {
  80. this.mLogWriter.WriteLine("---------------------------------------------------------------------------------------------------------------------");
  81. this.mLogWriter.WriteLine(System.DateTime.Now.ToString() + "\t" + log.Log + "\n");
  82. this.mLogWriter.WriteLine(log.Track);
  83. this.mLogWriter.WriteLine("---------------------------------------------------------------------------------------------------------------------");
  84. }
  85. else
  86. {
  87. this.mLogWriter.WriteLine(System.DateTime.Now.ToString() + "\t" + log.Log);
  88. }
  89. }
  90. }
  91. }
  92. }
  93.  
  94. public void Log(QLog.LogData logData)
  95. {
  96. lock (this.mLogLock)
  97. {
  98. this.mWaitingLogQueue.Enqueue(logData);
  99. Monitor.Pulse(this.mLogLock);
  100. }
  101. }
  102.  
  103. public void Close()
  104. {
  105. this.mIsRunning = false;
  106. this.mLogWriter.Close();
  107. }
  108. }
  109. }

QLog类

  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Text;
  4. using System.Collections.Generic;
  5. using System.Threading;
  6. namespace QFramework {
  7. /// <summary>
  8. /// 封装日志模块
  9. /// </summary>
  10. public class QLog : QSingleton<QLog>
  11. {
  12. /// <summary>
  13. /// 日志等级,为不同输出配置用
  14. /// </summary>
  15. public enum LogLevel
  16. {
  17. LOG = 0,
  18. WARNING = 1,
  19. ASSERT = 2,
  20. ERROR = 3,
  21. MAX = 4,
  22. }
  23.  
  24. /// <summary>
  25. /// 日志数据类
  26. /// </summary>
  27. public class LogData
  28. {
  29. public string Log { get; set; }
  30. public string Track { get; set; }
  31. public LogLevel Level { get; set; }
  32. }
  33.  
  34. /// <summary>
  35. /// OnGUI回调
  36. /// </summary>
  37. public delegate void OnGUICallback();
  38.  
  39. /// <summary>
  40. /// UI输出日志等级,只要大于等于这个级别的日志,都会输出到屏幕
  41. /// </summary>
  42. public LogLevel uiOutputLogLevel = LogLevel.LOG;
  43. /// <summary>
  44. /// 文本输出日志等级,只要大于等于这个级别的日志,都会输出到文本
  45. /// </summary>
  46. public LogLevel fileOutputLogLevel = LogLevel.MAX;
  47. /// <summary>
  48. /// unity日志和日志输出等级的映射
  49. /// </summary>
  50. private Dictionary<LogType, LogLevel> logTypeLevelDict = null;
  51. /// <summary>
  52. /// OnGUI回调
  53. /// </summary>
  54. public OnGUICallback onGUICallback = null;
  55. /// <summary>
  56. /// 日志输出列表
  57. /// </summary>
  58. private List<ILogOutput> logOutputList = null;
  59. private int mainThreadID = -1;
  60.  
  61. /// <summary>
  62. /// Unity的Debug.Assert()在发布版本有问题
  63. /// </summary>
  64. /// <param name="condition">条件</param>
  65. /// <param name="info">输出信息</param>
  66. public static void Assert(bool condition, string info)
  67. {
  68. if (condition)
  69. return;
  70. Debug.LogError(info);
  71. }
  72.  
  73. private QLog()
  74. {
  75. Application.logMessageReceived += LogCallback;
  76. Application.logMessageReceivedThreaded += LogMultiThreadCallback;
  77.  
  78. this.logTypeLevelDict = new Dictionary<LogType, LogLevel>
  79. {
  80. { LogType.Log, LogLevel.LOG },
  81. { LogType.Warning, LogLevel.WARNING },
  82. { LogType.Assert, LogLevel.ASSERT },
  83. { LogType.Error, LogLevel.ERROR },
  84. { LogType.Exception, LogLevel.ERROR },
  85. };
  86.  
  87. this.uiOutputLogLevel = LogLevel.LOG;
  88. this.fileOutputLogLevel = LogLevel.ERROR;
  89. this.mainThreadID = Thread.CurrentThread.ManagedThreadId;
  90. this.logOutputList = new List<ILogOutput>
  91. {
  92. new QFileLogOutput(),
  93. };
  94.  
  95. QApp.Instance().onGUI += OnGUI;
  96. QApp.Instance().onDestroy += OnDestroy;
  97. }
  98.  
  99. void OnGUI()
  100. {
  101. if (this.onGUICallback != null)
  102. this.onGUICallback();
  103. }
  104.  
  105. void OnDestroy()
  106. {
  107. Application.logMessageReceived -= LogCallback;
  108. Application.logMessageReceivedThreaded -= LogMultiThreadCallback;
  109. }
  110.  
  111. /// <summary>
  112. /// 日志调用回调,主线程和其他线程都会回调这个函数,在其中根据配置输出日志
  113. /// </summary>
  114. /// <param name="log">日志</param>
  115. /// <param name="track">堆栈追踪</param>
  116. /// <param name="type">日志类型</param>
  117. void LogCallback(string log, string track, LogType type)
  118. {
  119. if (this.mainThreadID == Thread.CurrentThread.ManagedThreadId)
  120. Output(log, track, type);
  121. }
  122.  
  123. void LogMultiThreadCallback(string log, string track, LogType type)
  124. {
  125. if (this.mainThreadID != Thread.CurrentThread.ManagedThreadId)
  126. Output(log, track, type);
  127. }
  128.  
  129. void Output(string log, string track, LogType type)
  130. {
  131. LogLevel level = this.logTypeLevelDict[type];
  132. LogData logData = new LogData
  133. {
  134. Log = log,
  135. Track = track,
  136. Level = level,
  137. };
  138. for (int i = 0; i < this.logOutputList.Count; ++i)
  139. this.logOutputList[i].Log(logData);
  140. }
  141. }
  142. }

欢迎讨论!

相关链接:

我的框架地址:https://github.com/liangxiegame/QFramework

教程源码:https://github.com/liangxiegame/QFramework/tree/master/Assets/HowToWriteUnityGameFramework/

QFramework &游戏框架搭建QQ交流群: 623597263

转载请注明地址:凉鞋的笔记http://liangxiegame.com/

微信公众号:liangxiegame

8.减少加班利器-QLog  - 图4

如果有帮助到您:

如果觉得本篇教程对您有帮助,不妨通过以下方式赞助笔者一下,鼓励笔者继续写出更多高质量的教程,也让更多的力量加入 QFramework 。