<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                ## 為毛要實現這個工具? 在我小時候,每當游戲到了測試階段,交給 QA 測試, QA 測試了一會兒拿著設備過來說游戲閃退了。。。。當我拿到設備后測了好久 Bug 也沒有復現,排查了好久也沒有頭緒,就算接了 Bugly 拿到的也只是閃退的異常信息,或者干脆拿不到。很抓狂,因為這個我是沒少加班。所以當時想著解決下這個小小的痛點。。。 ## 現在框架中的 QLog: 怎么用呢?在初始化的地方調用這句話就夠了。 ```cs QLog.Instance (); ``` 其實做成單例也沒有必要。。。。 ## 日志獲取方法: PC端或者Mac端,日志存放在工程的如下位置: ![DraggedImage.png](http://file.liangxiegame.com/e3e9ca4b-0d83-4c30-955e-a06e0ff0f053.png) 打開之后是這樣的: ![DraggedImage-1.png](http://file.liangxiegame.com/6d3e6522-f1f0-46e7-820f-bd4cecbe76b6.png) 最后一條信息是觸發了一個空指針異常,堆棧信息一目了然。 如果是iOS端,需要使用類似同步推或者iTools工具獲取日志文件,路徑是這樣的: ![image.png](http://file.liangxiegame.com/acaa5aa8-1999-4624-b753-bb6e63f6373f.png) Android端的話,類似的方式,但是具體路徑沒查過,不好意思。。。 ## 初版 一開始想做一個保存Debug.Log、Debug.LogWaring、Debug.LogErr信息到本地文件的小工具。上網一查原來有大神實現了,貼上鏈接: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: ```cs using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace QFramework { /// <summary> /// 日志輸出接口 /// </summary> public interface ILogOutput { /// <summary> /// 輸出日志數據 /// </summary> /// <param name="logData">日志數據</param> void Log(QLog.LogData logData); /// <summary> /// 關閉 /// </summary> void Close(); } } ``` 接口實現類 QFileLogOutput ```cs using System; using System.Collections.Generic; using System.Text; using System.Threading; using System.IO; using UnityEngine; namespace QFramework { /// <summary> /// 文本日志輸出 /// </summary> public class QFileLogOutput : ILogOutput { #if UNITY_EDITOR string mDevicePersistentPath = Application.dataPath + "/../PersistentPath"; #elif UNITY_STANDALONE_WIN string mDevicePersistentPath = Application.dataPath + "/PersistentPath"; #elif UNITY_STANDALONE_OSX string mDevicePersistentPath = Application.dataPath + "/PersistentPath"; #else string mDevicePersistentPath = Application.persistentDataPath; #endif static string LogPath = "Log"; private Queue<QLog.LogData> mWritingLogQueue = null; private Queue<QLog.LogData> mWaitingLogQueue = null; private object mLogLock = null; private Thread mFileLogThread = null; private bool mIsRunning = false; private StreamWriter mLogWriter = null; public QFileLogOutput() { QApp.Instance().onApplicationQuit += Close; this.mWritingLogQueue = new Queue<QLog.LogData>(); this.mWaitingLogQueue = new Queue<QLog.LogData>(); this.mLogLock = new object(); System.DateTime now = System.DateTime.Now; string logName = string.Format("Q{0}{1}{2}{3}{4}{5}", now.Year, now.Month, now.Day, now.Hour, now.Minute, now.Second); string logPath = string.Format("{0}/{1}/{2}.txt", mDevicePersistentPath, LogPath, logName); if (File.Exists(logPath)) File.Delete(logPath); string logDir = Path.GetDirectoryName(logPath); if (!Directory.Exists(logDir)) Directory.CreateDirectory(logDir); this.mLogWriter = new StreamWriter(logPath); this.mLogWriter.AutoFlush = true; this.mIsRunning = true; this.mFileLogThread = new Thread(new ThreadStart(WriteLog)); this.mFileLogThread.Start(); } void WriteLog() { while (this.mIsRunning) { if (this.mWritingLogQueue.Count == 0) { lock (this.mLogLock) { while (this.mWaitingLogQueue.Count == 0) Monitor.Wait(this.mLogLock); Queue<QLog.LogData> tmpQueue = this.mWritingLogQueue; this.mWritingLogQueue = this.mWaitingLogQueue; this.mWaitingLogQueue = tmpQueue; } } else { while (this.mWritingLogQueue.Count > 0) { QLog.LogData log = this.mWritingLogQueue.Dequeue(); if (log.Level == QLog.LogLevel.ERROR) { this.mLogWriter.WriteLine("---------------------------------------------------------------------------------------------------------------------"); this.mLogWriter.WriteLine(System.DateTime.Now.ToString() + "\t" + log.Log + "\n"); this.mLogWriter.WriteLine(log.Track); this.mLogWriter.WriteLine("---------------------------------------------------------------------------------------------------------------------"); } else { this.mLogWriter.WriteLine(System.DateTime.Now.ToString() + "\t" + log.Log); } } } } } public void Log(QLog.LogData logData) { lock (this.mLogLock) { this.mWaitingLogQueue.Enqueue(logData); Monitor.Pulse(this.mLogLock); } } public void Close() { this.mIsRunning = false; this.mLogWriter.Close(); } } } ``` QLog類 ```cs using UnityEngine; using System.Collections; using System.Text; using System.Collections.Generic; using System.Threading; namespace QFramework { /// <summary> /// 封裝日志模塊 /// </summary> public class QLog : QSingleton<QLog> { /// <summary> /// 日志等級,為不同輸出配置用 /// </summary> public enum LogLevel { LOG = 0, WARNING = 1, ASSERT = 2, ERROR = 3, MAX = 4, } /// <summary> /// 日志數據類 /// </summary> public class LogData { public string Log { get; set; } public string Track { get; set; } public LogLevel Level { get; set; } } /// <summary> /// OnGUI回調 /// </summary> public delegate void OnGUICallback(); /// <summary> /// UI輸出日志等級,只要大于等于這個級別的日志,都會輸出到屏幕 /// </summary> public LogLevel uiOutputLogLevel = LogLevel.LOG; /// <summary> /// 文本輸出日志等級,只要大于等于這個級別的日志,都會輸出到文本 /// </summary> public LogLevel fileOutputLogLevel = LogLevel.MAX; /// <summary> /// unity日志和日志輸出等級的映射 /// </summary> private Dictionary<LogType, LogLevel> logTypeLevelDict = null; /// <summary> /// OnGUI回調 /// </summary> public OnGUICallback onGUICallback = null; /// <summary> /// 日志輸出列表 /// </summary> private List<ILogOutput> logOutputList = null; private int mainThreadID = -1; /// <summary> /// Unity的Debug.Assert()在發布版本有問題 /// </summary> /// <param name="condition">條件</param> /// <param name="info">輸出信息</param> public static void Assert(bool condition, string info) { if (condition) return; Debug.LogError(info); } private QLog() { Application.logMessageReceived += LogCallback; Application.logMessageReceivedThreaded += LogMultiThreadCallback; this.logTypeLevelDict = new Dictionary<LogType, LogLevel> { { LogType.Log, LogLevel.LOG }, { LogType.Warning, LogLevel.WARNING }, { LogType.Assert, LogLevel.ASSERT }, { LogType.Error, LogLevel.ERROR }, { LogType.Exception, LogLevel.ERROR }, }; this.uiOutputLogLevel = LogLevel.LOG; this.fileOutputLogLevel = LogLevel.ERROR; this.mainThreadID = Thread.CurrentThread.ManagedThreadId; this.logOutputList = new List<ILogOutput> { new QFileLogOutput(), }; QApp.Instance().onGUI += OnGUI; QApp.Instance().onDestroy += OnDestroy; } void OnGUI() { if (this.onGUICallback != null) this.onGUICallback(); } void OnDestroy() { Application.logMessageReceived -= LogCallback; Application.logMessageReceivedThreaded -= LogMultiThreadCallback; } /// <summary> /// 日志調用回調,主線程和其他線程都會回調這個函數,在其中根據配置輸出日志 /// </summary> /// <param name="log">日志</param> /// <param name="track">堆棧追蹤</param> /// <param name="type">日志類型</param> void LogCallback(string log, string track, LogType type) { if (this.mainThreadID == Thread.CurrentThread.ManagedThreadId) Output(log, track, type); } void LogMultiThreadCallback(string log, string track, LogType type) { if (this.mainThreadID != Thread.CurrentThread.ManagedThreadId) Output(log, track, type); } void Output(string log, string track, LogType type) { LogLevel level = this.logTypeLevelDict[type]; LogData logData = new LogData { Log = log, Track = track, Level = level, }; for (int i = 0; i < this.logOutputList.Count; ++i) this.logOutputList[i].Log(logData); } } } ``` ## 歡迎討論! 轉載請注明地址:涼鞋的筆記:[liangxiegame.com](http://liangxiegame.com) ## 更多內容 * QFramework 地址:[https://github.com/liangxiegame/QFramework](https://github.com/liangxiegame/QFramework) * QQ 交流群:[623597263](http://shang.qq.com/wpa/qunwpa?idkey=706b8eef0fff3fe4be9ce27c8702ad7d8cc1bceabe3b7c0430ec9559b3a9ce66) * **Unity 進階小班**: * 主要訓練內容: * 框架搭建訓練(第一年) * 跟著案例學 Shader(第一年) * 副業的孵化(第二年、第三年) * 權益、授課形式等具體詳情請查看[《小班產品手冊》](https://liangxiegame.com/master/intro):https://liangxiegame.com/master/intro * 關注公眾號:liangxiegame 獲取第一時間更新通知及更多的免費內容。 ![](http://file.liangxiegame.com/38eccb55-40b2-4845-93d6-f5fb50ff9492.png)
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看