Unity3D 日志优化

                                             Unity3D  日志优化

加入了 对移动端的 日志的查看

 本地日志问价 的创建 以天为 单文件

 

 犹豫: 一般 在 Debug.Log() 外边 加一层 双击日志  只会跳转到 Debug.log 的位置不会 真的跳转到  使用时日志的地方

这里 封装的 双击后 可以直接 跳转在 你是用 的地方

扫描二维码关注公众号,回复: 4576635 查看本文章
using UnityEngine;
/// <summary>
/// 日志
/// </summary>
public static class DebugPlus
{
    public static void Log(string info)
    { 
       Debug.Log(System.DateTime.Now.TimeFormat() + "  :" + info);
    }

    public static void LogError(string info)
    {
       Debug.LogError(System.DateTime.Now.TimeFormat() + "  :" + info);
    }

    public static void LogWarning(string info)
    {
       Debug.LogWarning(System.DateTime.Now.TimeFormat()+ "  :" + info);     
    }

    public static string TimeFormat(this System.DateTime now)
    {
        return now.ToString("yyyy-MM-dd HH:mm:ss:ffff");
    }

    public static string TimeDay(this System.DateTime now)
    {
        return now.ToString("yyyy-MM-dd");
    }
 
}

这个是 日志的处理, 以及写入 文件文件 的处理, 在这里做了优化  累计三条 才会写入文件 做了优化

using System.Collections;
using System.Collections.Generic;
using UnityEngine;


public class ManagerLoger : MonoBehaviour
{

    private System.IO.FileInfo  fileLogPath;
    private bool isDebug        = true;
    private Queue<string>       queueLogs;
    private int maxCount        = 3;
    private bool                isShowDetail = false;

    void Awake()
    {
        fileLogPath = new System.IO.FileInfo(Application.persistentDataPath + "/PlusLogs/" + System.DateTime.Now.TimeDay() + "_Log.txt");
        queueLogs = new Queue<string>();

        if (!fileLogPath.Exists)
        {
            if (!System.IO.Directory.Exists(fileLogPath.DirectoryName))
            {
                System.IO.Directory.CreateDirectory(fileLogPath.DirectoryName);
            }
            fileLogPath.Create();
        }
        //Application.RegisterLogCallback(HandleLog);老版本这么写的
        Application.logMessageReceived += HandleLog;
        if (isDebug)
        {
            DebugPlus.Log("成功初始化日志文件:" + fileLogPath.FullName);
        }

    }


    void HandleLog(string logString, string stackTrace, LogType type)
    {
        string[] s = stackTrace.Split('\n');
        stackTrace = string.Empty;
        foreach (string line in s)
        {
            stackTrace += "      " + line + '\n';
        }
        lock (queueLogs)
        {
            queueLogs.Enqueue(string.Format("{0} : {1}\r\n{2}  {3}", type, logString, stackTrace, "\n"));
            if (queueLogs.Count >= maxCount)
            {
                WriteLocal();
            }
        }
    }

    void WriteLocal()
    {
        try
        {
            using (System.IO.StreamWriter file = new System.IO.StreamWriter(fileLogPath.FullName, true))
            {
                while (queueLogs.Count > 0)
                {
                    file.Write(queueLogs.Dequeue());//直接追加文件末尾  
                }
            }
        }
        catch (System.Exception e)
        {
            throw (e);
        }
    }

    public string GetLogFile()
    {
        try
        {
            return System.IO.File.ReadAllText(fileLogPath.FullName);
        }
        catch (System.Exception e)
        {
            return "日志文件读取错误:" + e.Message;
        }

    }

 
    void OnGUI()
    {
        if (isDebug)
        {
            if (isShowDetail)
            {
                if (GUI.Button(new Rect(Screen.width - 50,50, 50, 50), "Hide"))
                {
                    isShowDetail = false;
                }          
                GUI.TextArea(new Rect(0, 100, Screen.width, Screen.height - 100), GetLogFile());        
            }
            else
            {
                if (GUI.Button(new Rect(Screen.width - 50, Screen.height - 50, 50, 50), "Show"))
                {
                    isShowDetail = true;
                }
            }
        }
    }


    void OnDestroy()
    {
        WriteLocal();
    }
}

这里是工程的下载路径

猜你喜欢

转载自blog.csdn.net/nicepainkiller/article/details/84958804