博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Unity 游戏框架搭建 (八) 减少加班利器-QLog
阅读量:7079 次
发布时间:2019-06-28

本文共 8402 字,大约阅读时间需要 28 分钟。

为毛要实现这个工具?

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

现在框架中的QLog:

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

QLog.Instance ();复制代码

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

日志获取方法:

PC端或者Mac端,日志存放在工程的如下位置:

打开之后是这样的:
最后一条信息是触发了一个空指针异常,堆栈信息一目了然。 如果是iOS端,需要使用类似同步推或者iTools工具获取日志文件,路径是这样的:
Android端的话,类似的方式,但是具体路径没查过,不好意思。。。

初版

一开始想做一个保存 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:

using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace QFramework {	/// 	/// 日志输出接口	/// 	public interface ILogOutput	{		/// 		/// 输出日志数据		/// 		/// 日志数据		void Log(QLog.LogData logData);		/// 		/// 关闭		/// 		void Close();	}}复制代码

接口实现类 QFileLogOutput

using System;using System.Collections.Generic;using System.Text;using System.Threading;using System.IO;using UnityEngine;namespace QFramework {	/// 	/// 文本日志输出	/// 	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
mWritingLogQueue = null; private Queue
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
(); this.mWaitingLogQueue = new Queue
(); 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
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类

using UnityEngine;using System.Collections;using System.Text;using System.Collections.Generic;using System.Threading;namespace  QFramework {	/// 	/// 封装日志模块	/// 	public class QLog : QSingleton
{ ///
/// 日志等级,为不同输出配置用 /// public enum LogLevel { LOG = 0, WARNING = 1, ASSERT = 2, ERROR = 3, MAX = 4, } ///
/// 日志数据类 /// public class LogData { public string Log { get; set; } public string Track { get; set; } public LogLevel Level { get; set; } } ///
/// OnGUI回调 /// public delegate void OnGUICallback(); ///
/// UI输出日志等级,只要大于等于这个级别的日志,都会输出到屏幕 /// public LogLevel uiOutputLogLevel = LogLevel.LOG; ///
/// 文本输出日志等级,只要大于等于这个级别的日志,都会输出到文本 /// public LogLevel fileOutputLogLevel = LogLevel.MAX; ///
/// unity日志和日志输出等级的映射 /// private Dictionary
logTypeLevelDict = null; ///
/// OnGUI回调 /// public OnGUICallback onGUICallback = null; ///
/// 日志输出列表 /// private List
logOutputList = null; private int mainThreadID = -1; ///
/// Unity的Debug.Assert()在发布版本有问题 /// ///
条件 ///
输出信息 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.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
{ 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; } ///
/// 日志调用回调,主线程和其他线程都会回调这个函数,在其中根据配置输出日志 /// ///
日志 ///
堆栈追踪 ///
日志类型 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); } }}复制代码

欢迎讨论!

相关链接:

:https://github.com/liangxiegame/QFramework

:https://github.com/liangxiegame/QFramework/tree/master/Assets/HowToWriteUnityGameFramework/

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

转载请注明地址:http://liangxiegame.com/

微信公众号:liangxiegame

如果有帮助到您:

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

  • 购买 gitchat 话题《Unity 游戏框架搭建:资源管理 与 ResKit 精讲》
    • 价格: 6 元,会员免费
    • 地址: http://gitbook.cn/gitchat/activity/5b29df073104f252297a779c
  • 给 QFramework 一个 Star
    • 地址: https://github.com/liangxiegame/QFramework
  • 给 Asset Store 上的 QFramework 并给个五星(需要先下载)
    • 地址: http://u3d.as/SJ9
  • 购买 gitchat 话题《Unity 游戏框架搭建:我所理解的框架》
    • 价格: 6 元,会员免费
    • 地址: http://gitbook.cn/gitchat/activity/5abc3f43bad4f418fb78ab77
  • 购买同名电子书 :https://www.kancloud.cn/liangxiegame/unity_framework_design( 29.9 元,内容会在 2018 年 10 月份完结)
你可能感兴趣的文章
默认值函数[Python读书]cookbook --20.1 在函数调用中获得常新的默认值默认值函数...
查看>>
字符和文档识别的四十年研究
查看>>
最后一行数据
查看>>
Linux 下smi/mdio总线通信
查看>>
java
查看>>
SPREAD for Windows Forms 控制输入法
查看>>
用语音编程 你想过没?
查看>>
android应用推荐
查看>>
ZeroMQ接口函数之 :zmq_curve_keypair - 生成一个新的CURVE 密钥对
查看>>
Linux文件锁flock
查看>>
135. Candy
查看>>
试想一下,在代码学习Swift!
查看>>
provider: 命名管道提供, error: 40 - 无法打开 SQL Server 联系)
查看>>
lintcode : 跳跃游戏
查看>>
远程方法调用(RMI)原理与示例 (转)
查看>>
项目技术团队
查看>>
commons dbcp.jar有什么用
查看>>
STM32 Unicode 与 GBK 转换 .bin文件放到SD卡是啥意思
查看>>
nginx + tomcat配置负载均衡
查看>>
ARM-Linux (临时,正式) 建立页表的比较【转】
查看>>