zoukankan      html  css  js  c++  java
  • Unity3D Log 收集机制

        最近做项目的时候发现,需要有一个完整的log机制。这样不仅方便调试而且方便观察。

    一、需求

      目前我认为一个完善的log机制应该是这样的。

      一、双击定位

      二、生命周期是全局的

      三、输出包括consloe 和 log 日志,并且这些log的打印是可配置的。

      四、未完待续,如果你有更好的想法,请留言。

    二、代码实现

      不废话,直接上代码

      

    using UnityEngine;
    using System;
    using System.Text;
    using System.IO;
    using System.Collections;
    
    namespace LogUtil {
        
        public class LogManager : MonoBehaviour
        {
            //public 
            static string LogPath = Application.persistentDataPath + "/";
    
            //public 
            static string LogFilePath = string.Format("{0}{1}", LogPath, "info");
            //public 
            static int infoCount = 0;
    
            //public 
            static string LogWaringFilePath = string.Format("{0}{1}", LogPath, "waring");
            //public
            static int waringCount = 0;
    
            //public
            static string LogErrorFilePath = string.Format("{0}{1}", LogPath, "error");
            //public 
            static int errorCount = 0;
    
            //public 
            static string LogExceptionFilePath = string.Format("{0}{1}", LogPath, "exception");
            //public 
            static int exceptionCount = 0;
    
            static LogManager _instance = null;
    
            //创建第一个实例或引用任何静态成员之前,将自动调用静态构造函数
            static LogManager()
            {
                GameObject tmp = new GameObject("LogManager");
                DontDestroyOnLoad(tmp);
                _instance = tmp.AddComponent<LogManager>();
                //Debug.Log("LogManager");
            }
    
    		//配置是否生成Log日志
            public static bool IsRegister = true;
    		//配置是否控制台打印
            public static bool IsLog = true;
            void Awake()
            {
                //register callback
                if (IsRegister) Application.RegisterLogCallbackThreaded(OutPutLog);
            }
    
            public static void Log(object message)
            {
                if (IsLog) Debug.Log(message);
            }
    
            public static void LogError(object message)
            {
                if (IsLog) Debug.LogError(message);
            }
    
            public static void LogWarning(object message)
            {
                if (IsLog) Debug.LogWarning(message);
            }
    
            public static void LogException(Exception message)
            {
                if (IsLog) Debug.LogException(message);
            }
    
            public static void LogException(string message)
            {
                if (IsLog) Debug.LogException(new Exception(message));
            }
    
            FileStream logStream = new FileStream(string.Format("{0}.log", LogFilePath), FileMode.Create);
            FileStream warningStream = new FileStream(string.Format("{0}.log", LogWaringFilePath), FileMode.Create);
            FileStream errorStream = new FileStream(string.Format("{0}.log", LogErrorFilePath), FileMode.Create);
            FileStream exceptionStream = new FileStream(string.Format("{0}.log", LogExceptionFilePath), FileMode.Create);
    
            public void OutPutLog(string condition, string stackTrace, LogType type)
            {
    
                string curTime = DateTime.Now.ToString();
                string hint = string.Format("{0} [{1}] {2}
    {3}
    ", curTime, type.ToString(), condition, stackTrace);
                byte[] buffer = Encoding.UTF8.GetBytes(hint);
    
                switch (type)
                {
                    case LogType.Error:
    
                        if (errorStream != null)
                        {
                            errorStream.Write(buffer, 0, buffer.Length);
    
                            if (errorStream.Length > 1024 * 1024)
                            {
                                errorStream.Close();
    
                                File.Move(string.Format("{0}.log", LogErrorFilePath), string.Format("{0}_{1}.log", LogErrorFilePath, waringCount++));
    
                                errorStream = new FileStream(string.Format("{0}.log", LogErrorFilePath), FileMode.Create);
                            }
                        }
    
                        break;
                    case LogType.Exception:
    
                        if (exceptionStream != null)
                        {
                            exceptionStream.Write(buffer, 0, buffer.Length);
    
                            if (exceptionStream.Length > 1024 * 1024)
                            {
                                exceptionStream.Close();
    
                                File.Move(string.Format("{0}.log", LogExceptionFilePath), string.Format("{0}_{1}.log", LogExceptionFilePath, waringCount++));
    
                                exceptionStream = new FileStream(string.Format("{0}.log", LogExceptionFilePath), FileMode.Create);
                            }
                        }
    
                        break;
                    case LogType.Log:
    
                        if (logStream != null)
                        {
                            logStream.Write(buffer, 0, buffer.Length);
    
                            if (logStream.Length > 1024 * 1024)
                            {
                                logStream.Close();
    
                                File.Move(string.Format("{0}.log", LogFilePath), string.Format("{0}_{1}.log", LogFilePath, infoCount++));
    
                                logStream = new FileStream(string.Format("{0}.log", LogFilePath), FileMode.Create);
                            }
                        }
                        break;
                    case LogType.Warning:
    
                        if (warningStream != null)
                        {
                            warningStream.Write(buffer, 0, buffer.Length);
    
                            if (warningStream.Length > 1024 * 1024)
                            {
                                warningStream.Close();
    
                                File.Move(string.Format("{0}.log", LogWaringFilePath), string.Format("{0}_{1}.log", LogWaringFilePath, waringCount++));
    
                                warningStream = new FileStream(string.Format("{0}.log", LogWaringFilePath), FileMode.Create);
                            }
                        }
    
                        break;
                    default:
                        break;
                }
            }
    
        }
    
    }
    
    

     三、完善

      上述的代码基本实现了2,3的需求功能,但是1的双击定位问题却带来了。

      我们可以封装成一个dll,然后就解决了这个问题。

      参考链接如下 注意引用UnityEngine这个链接即可

       http://www.cnblogs.com/errorx/archive/2011/03/29/1999170.html
         

         http://game.ceeger.com/Manual/UsingDLL.html
         file:///C:/Unity3D/Editor/Data/Documentation/html/en/Manual/UsingDLL.html

      

      我把那个类库建在自己的工程目录下,按F6 生成dll

        

      

        放入自己的工程就可以了。原来的cs文件记得删除哟!

     

    四、资料

      我把dll与源码共享出来,如果有更好的修改方式,你也可以完善。

      http://yunpan.cn/Q7dukcFjA3tAv  访问密码 ed22

    2014-9-7 日 更新

      如果你在导入和使用dll编译的时候发现了错误

      

      

      估计是因为你.net框架过高,把你的类库的level 降到3.5即可

      

     

     

     

  • 相关阅读:
    SQLAlchemy介绍
    Flask介绍
    逆转的生殖——形而下的EOE补完仪式…
    huiyin
    实验课上
    我的博客今天1岁213天了,我领取了…
    关于直接写屏
    OceanBorn&nbsp;&nbsp;歌曲列表
    Gethsemane
    光辉岁月-Beyond
  • 原文地址:https://www.cnblogs.com/chongxin/p/3958617.html
Copyright © 2011-2022 走看看