zoukankan      html  css  js  c++  java
  • unity游戏框架学习-日志系统

    概述:https://www.cnblogs.com/wang-jin-fu/p/10975660.html

    日志系统功能包括:

    1.日志开关。只有开发版本开启日志,因为日志还是比较耗性能的。。。

    2.堆栈日志界面:ERROR时弹出界面,该界面显示错误的堆栈日志。大半部分错误日志是不会导致崩溃,如果不弹窗qa可能会漏掉一些重要的log信息。

    3.接入SRDebugger,方便在qa测试时,在测试机查看详细的日志信息,方便定位错误出现的原因。

    4.FPS帧率的显示

    5.游戏正式上线以后,我们很难拿到用户的错误日志,这时候我们需要把错误的日志上传到我们的服务器

    6.当游戏崩溃时我们是拿不到unity打印的日志的,这时候就需要接入FireBase了,它可以帮我们把崩溃的详细日志上传到网页上,方便我们查看

    日志系统目标用户:

    1.qa、运营等测试人员(可以拿到测试机),需要在手机上实现可视化的日志堆栈,方便查阅日志(当然你也可以把手机连到Android Studio和Xcode查看日志,就是比较麻烦而已),对定位bug有很大帮助。

    2.用户(获取不到测试机),需要把日志上传到服务器,崩溃日志需要接入FireBase,这样就可以在FB后台看到崩溃的堆栈信息。

    一、手机上显示log信息:SRDebugger插件

    SRDebugger文档:https://www.stompyrobot.uk/tools/srdebugger/documentation/

    SRDebugger下载:https://assetstore.unity.com/packages/tools/gui/srdebugger-console-tools-on-device-27688,嗯,这个24美刀~

    SRDebugger界面示例:SRDebugger可以获取当前运行系统的信息,包括操作系统、处理器、显卡等硬件信息。

    SRDebugger可以查看所有的程序运行日志,包括使用Debug.Log系列打印的日志,或是其他的未捕获异常。

    SRDebugger可以监控整个项目的内存使用信息,手动卸载资源,手动进行GC垃圾回收。

    二、日志开关及堆栈信息获取

    日志开关无非就是框架封装一层日志接口,所有业务的日志打印都走这个接口,在根据条件判断是否调用Debug.Log方法。例如:

    public void Log(string log)
        {
            if (Config.LogEnable)
            {
                Debug.Log(log);
            }
        }

    日志堆栈信息获取:Application.logMessageReceived接口。每次接收到日志消息,都会触发的事件。注意在logMessageReceived回调里打印任何日志都不会生效(避免死循环)。

    using UnityEngine;
    using System.Collections;
    
    public class ExampleClass : MonoBehaviour
    {
        public string output = "";
        public string stack = "";
    
        void OnEnable()
        {
            Application.logMessageReceived += HandleLog;
        }
    
        void OnDisable()
        {
            Application.logMessageReceived -= HandleLog;
        }
    
        void HandleLog(string logString, string stackTrace, LogType type)
        {
            output = logString;
            stack = stackTrace;
        }
    }
    //
        // 摘要:
        //     The type of the log message in Debug.logger.Log or delegate registered with Application.RegisterLogCallback.
        public enum LogType
        {
            //
            // 摘要:
            //     LogType used for Errors.
            Error = 0,
            //
            // 摘要:
            //     LogType used for Asserts. (These could also indicate an error inside Unity itself.)
            Assert = 1,
            //
            // 摘要:
            //     LogType used for Warnings.
            Warning = 2,
            //
            // 摘要:
            //     LogType used for regular log messages.
            Log = 3,
            //
            // 摘要:
            //     LogType used for Exceptions.
            Exception = 4
        }

    logString:日志信息。stackTrace:日志的详细堆栈信息。

    那么错误弹窗就是将错误信息以及错误的堆栈信息赋值给Text并显示在界面上。

    例如

    public void DevelopLog(string logString, string stackTrace, LogType type)
        {
            if (type == LogType.Error || type == LogType.Exception)
            {
                string result = "LogString:" + logString;
                result += "
    StackTrace:" + stackTrace;
                m_content.text = log;
    
            }
        }

    保存服务端的话就是将上面的result值传给服务端,由服务端保存。当然你也可以额外添加一些参数,例如版本、时间、机型等。例如

    string result = string.Format("Version:{0}
    Time:{0}
    LogString:{1}
    StackTrace:{2}", Config.Version, Time.time, logString, stackTrace);

    三、崩溃信息上传FireBase

    Android API文档:https://firebase.google.com/docs/android/setup?hl=zh-cn

    Ios API文档:https://firebase.google.com/docs/ios/setup?hl=zh-cn

    这个两个文档有详细的FireBase后台配置的步骤,以及GoogleService-Info.plist 、google-services.json文件的生成(这两个文件需要放在你的工程目录里)

    如果你的sdk或者运营大哥帮你配置好了并给了你GoogleService-Info.plist 、google-services.json文件的话,只需要参考以下步骤就好了。

    android:只需要两步,记得把你的google-services.json文件拷贝到项目里

    参考:https://firebase.google.com/docs/crashlytics/get-started?platform=android#android

    1.在项目级 build.gradle 中,将您的 google-services 更新为 3.1.2 或更高版本,然后添加 Crashlytics 代码库和依赖项:

    buildscript {
        repositories {
            // Add the following repositories:
            google()  // Google's Maven repository
    
            maven {
               url 'https://maven.fabric.io/public'
            }
        }
    
        dependencies {
            // ...
    
            // Check for v3.1.2 or higher
            classpath 'com.google.gms:google-services:4.2.0'  // Google Services plugin
    
            // Add dependency
            classpath 'io.fabric.tools:gradle:1.29.0'  // Crashlytics plugin
    
            
        }
    }
    
    
    allprojects {
        // ...
    
        repositories {
           // Check that you have the following line (if not, add it):
           google()  // Google's Maven repository
           // ...
        }
    }

    2.在应用级 build.gradle 中,将 firebase-core 更新为 v11.4.2 或更高版本,然后添加 Crashlytics 依赖项:

    apply plugin: 'com.android.application'
    apply plugin: 'io.fabric'
    
    dependencies {
        // ...
    
        // Check for v11.4.2 or higher
        implementation 'com.google.firebase:firebase-core:17.0.0'
    
        // Add dependency
        implementation 'com.crashlytics.sdk.android:crashlytics:2.10.1'
    }

    ios参考:https://firebase.google.com/docs/crashlytics/get-started?platform=ios#android

    1.拉取代码,打开 Podfile,然后添加以下行:

    pod 'Fabric', '~> 1.10.2'
    pod 'Crashlytics', '~> 3.13.3'

    2.初始化

     如果你的ios端提示dsym未上传,可以用命令行上传(比较耗时间,建议只在正式包上传),命令如下:

    /path/to/pods/directory/Fabric/upload-symbols -gsp /path/to/GoogleService-Info.plist -p ios /path/to/dSYMs

    //这边的self.OutProjectPath是你的xcode项目目录
    cmddSYM = "%sPods/Fabric/upload-symbols -gsp %sGoogleService-Info.plist -p ios %sarchive/SF.xcarchive/dSYMs" % (self.OutProjectPath, self.OutProjectPath, self.OutProjectPath)

    DSYM丢失参考api:https://firebase.google.com/docs/crashlytics/get-deobfuscated-reports?authuser=0

  • 相关阅读:
    Windows server 2016 解决“无法完成域加入,原因是试图加入的域的SID与本计算机的SID相同。”
    Windows Server 2016 辅助域控制器搭建
    Windows Server 2016 主域控制器搭建
    Net Framework 4.7.2 覆盖 Net Framework 4.5 解决办法
    SQL SERVER 2012更改默认的端口号为1772
    Windows下彻底卸载删除SQL Serever2012
    在Windows Server2016中安装SQL Server2016
    SQL Server 创建索引
    C#控制台或应用程序中两个多个Main()方法的设置
    Icon cache rebuilding with Delphi(Delphi 清除Windows 图标缓存源代码)
  • 原文地址:https://www.cnblogs.com/wang-jin-fu/p/11263310.html
Copyright © 2011-2022 走看看