Unity3D Log 收集機制

來源:互聯網
上載者:User

標籤:des   style   blog   http   color   os   io   ar   for   

    最近做項目的時候發現,需要有一個完整的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}\n{3}\n", 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

Unity3D Log 收集機制

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.