zoukankan      html  css  js  c++  java
  • 自己封装一个Log模块

    Unity自己有log系统,为什么要自己封装一个
    1.不好用,只能在pc上记录log文件,移动平台是没有的
    2.在开发时期的log,不想在正式版里面出现。没有一个统一的开关来控制是不是要显示log,要显示什么类型的log

    那么怎么做?
    我的思路是:Unity里面的所有的log,都会被 Application.logMessageReceived 捕获
    只要添加一个回调来把这些log信息记录下来保存就好了
    Application.logMessageReceived += ProcessExceptionReport;

    代码1:log等级枚举,被用来控制是否显示log,显示哪些类型的log

    namespace ShiHuanJue.Debuger
    {
        /// <summary>
        /// log等级
        /// </summary>
        public enum LogLevel
        {
            None = 0,
            Debug = 1,
            Error = 2,
            Warning = 4,
            Exception = 8,
            All = LogLevel.Debug | LogLevel.Error | LogLevel.Warning | LogLevel.Exception
        }
    }

    代码2:把log记录到文本里

    using UnityEngine;
    using System.Collections;
    using System.IO;
    using System;
    using System.Text;
     
    namespace ShiHuanJue.Debuger
    {
        public class LogWriter
        {
            private string m_logPath = Application.persistentDataPath + "/log/";
            private string m_logFileName = "log_{0}.txt";
            private string m_logFilePath = string.Empty;
     
            public LogWriter()
            {
                if (!Directory.Exists(m_logPath))
                {
                    Directory.CreateDirectory(m_logPath);
                }
                this.m_logFilePath = this.m_logPath + string.Format(this.m_logFileName, DateTime.Today.ToString("yyyyMMdd"));
            }
     
            public void ExcuteWrite(string content)
            {
                using (StreamWriter writer = new StreamWriter(m_logFilePath, true, Encoding.UTF8))
                {
                    writer.WriteLine(content);
                }
            }
        }
    }

    代码3:log帮助类

    using UnityEngine;
    using System;
     
    namespace ShiHuanJue.Debuger
    {
        public class LogHelper 
        {
            static public LogLevel m_logLevel = LogLevel.All;
            static LogWriter m_logWriter = new LogWriter();
     
            static LogHelper()
            {
                Application.logMessageReceived += ProcessExceptionReport;
            }
     
            private static void ProcessExceptionReport(string message, string stackTrace, LogType type)
            {
                LogLevel dEBUG = LogLevel.Debug;
                switch (type)
                {
                    case LogType.Error:
                        dEBUG = LogLevel.Error;
                        break;
                    case LogType.Assert:
                        dEBUG = LogLevel.Debug;
                        break;
                    case LogType.Warning:
                        dEBUG = LogLevel.Warning;
                        break;
                    case LogType.Log:
                        dEBUG = LogLevel.Debug;
                        break;
                    case LogType.Exception:
                        dEBUG = LogLevel.Exception;
                        break;
                }
     
                if (dEBUG == (m_logLevel & dEBUG))
                {
                    Log(string.Concat(new object[] { " [", dEBUG, "]: ", message, '
    ', stackTrace }));
                }
            }
     
     
            /// <summary>
            /// 加上时间戳
            /// </summary>
            /// <param name="message"></param>
            private static void Log(string message)
            {
                string msg = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss,fff") + message;
                m_logWriter.ExcuteWrite(msg);
            }
     
            static public void Log(object message)
            {
                Log(message, null);
            }
            static public void Log(object message, UnityEngine.Object context)
            {
                if (LogLevel.Debug == (m_logLevel & LogLevel.Debug))
                {
                    Debug.Log(message, context);
                }
            }
            static public void LogError(object message)
            {
                LogError(message, null);
            }
            static public void LogError(object message, UnityEngine.Object context)
            {
                if (LogLevel.Error == (m_logLevel & LogLevel.Error))
                {
                    Debug.LogError(message, context);
                }
            }
            static public void LogWarning(object message)
            {
                LogWarning(message, null);
            }
            static public void LogWarning(object message, UnityEngine.Object context)
            {
                if (LogLevel.Warning == (m_logLevel & LogLevel.Warning))
                {
                    Debug.LogWarning(message, context);
                }
            }
        }
    }

    游戏上线之前把LogHelper.m_logLevel = LogLevel.None;Log就不显示了。
    LogHelper.m_logLevel = LogLevel.Error;是显示Error。
    LogHelper.m_logLevel = LogLevel.Debug | LogLevel.Error;同时显示Debug和Error。

    log文件是保存在 private string m_logPath = Application.persistentDataPath + “/log/”;在手机上就是沙盒路径。
    log文件是以天为单位,同一天的log会被记录到一个文件里面。

    怎么用呢?

    using UnityEngine;
    using ShiHuanJue.Debuger;
     
    public class Test : MonoBehaviour
    {
        void Start()
        {
            LogHelper.m_logLevel = LogLevel.All;
            LogHelper.Log("debug");
            LogHelper.LogError("error");
            LogHelper.LogWarning("warning");
     
            GameObject go =  GameObject.Find("fsdfsd");
            GameObject.Instantiate(go);
        }
    }

    这里测试了几种典型的情况,包括找不到go,实例化的时候系统报的异常
    有图有真相~!

    查看一下log文件

    最后我封装了一个dll,直接放到工程下头就可以用了。
    大家也可以在这里下载:链接: http://pan.baidu.com/s/1qWFtQOG 密码: erpx
    代码都在上面,你们也可以去生成。

  • 相关阅读:
    常用算法解析-动态规划
    转载-通过ApplicationContext 去获取所有的Bean
    什么是crud?
    new 关键字 和 newInstance() 方法的 区别
    Java反射简单使用--第一次细致阅读底层代码
    动态创建管理定时任务-已完成
    springboot mail整合freemark实现动态生成模板
    20190930开始记录每天学习状态,更新至20200125结束
    hibernate的对象/关系映射结果为空,exists查不到值的问题-20190823
    转载-Java中LinkedList的一些方法—addFirst addFirst getFirst geLast removeFirst removeLast
  • 原文地址:https://www.cnblogs.com/joeshifu/p/5488799.html
Copyright © 2011-2022 走看看