WhiteEngine-专注图形渲染与游戏引擎技术的网站

新浪微薄腾讯微薄

最新碎语:暂无碎语

您的位置:WhiteEngine-专注图形渲染与游戏引擎技术的网站 >引擎相关> Debug日志抓取 输出 显示

Debug日志抓取 输出 显示

using UnityEngine;
using System.Collections;

using System.Collections.Generic;
using System.IO;
using System.Text;
using System;

public class OutLog : MonoBehaviour 
{
public static int mEnableLog = 2; /* 0 off log 1 on log 2 on log callback */
public static int mLogType = -1; /* -2 custom log -1 all on log 0 Error 1 Assert 2 Warning 3 Log 4 Exception */

    static List<string> mLines = new List<string>();
    static List<string> mWriteTxt = new List<string>();
    private string mOutPath;

private static OutLog mInstance = null;
public static OutLog Instance
{
get
{
  if (mInstance == null)
  {
GameObject go = new GameObject("OutLog", typeof(OutLog));
mInstance = go.GetComponent<OutLog>();
DontDestroyOnLoad(go);
  }
return mInstance;
}
}

    void Start ()
{
mInstance = this;

if (mEnableLog == 0)
{
return;

else if (mEnableLog == 2)
{
//在这里做一个Log的监听
Application.RegisterLogCallback(HandleLog);
}

        //Application.temporaryCachePath Unity中只有这个路径是既可以读也可以写的。
mOutPath = Application.temporaryCachePath + "/outLog.txt";
        //Bei Fen之前保存的Log
if (System.IO.File.Exists (mOutPath))
{
File.Move(mOutPath, Application.temporaryCachePath + "/outLog" + System.DateTime.Now.ToString("yyyyMMddHHmmss") + ".txt");

    }
 
    void Update () 
    {
if (mEnableLog == 0)
{
return;
}

        //因为写入文件的操作必须在主线程中完成,所以在Update中哦给你写入文件。
        if(mWriteTxt.Count > 0)
        {
            string[] temp = mWriteTxt.ToArray();
            foreach(string t in temp)
            {
using(StreamWriter writer = new StreamWriter(mOutPath, true, Encoding.UTF8))
                {
writer.WriteLine(DateTime.Now+"-----"+t+"\n");
                }
                mWriteTxt.Remove(t);
            }
        }
    }
 
    void HandleLog(string logString, string stackTrace, LogType type)
    {
if (mEnableLog == 0)
{
return;
}

        mWriteTxt.Add(logString);
        if (type == LogType.Error || type == LogType.Exception) 
        {
printf(logString+"\n"+stackTrace);
mWriteTxt.Add(logString+"\n"+stackTrace);
        }
    }

private void AddLog(string logString, object stackTrace, LogType type)
{
if (mEnableLog == 0)
{
return;
}

string tempStackTrace = "";
if (stackTrace != null)
{
tempStackTrace = stackTrace.ToString();
}

if (mLogType == (int)type || mLogType == -1 || mLogType == -2)
{
mWriteTxt.Add(logString);
if (tempStackTrace != "")
mWriteTxt.Add(tempStackTrace);
}
}
 
  //这里我把错误的信息保存起来,用来输出在手机屏幕上
    public void printf (params object[] objs)
    {
if (mEnableLog == 0)
{
return;
}

        string text = "";
        for (int i = 0; i < objs.Length; ++i)
        {
            if (i == 0)
            {
                text += objs[i].ToString();
            }
            else
            {
                text += ", " + objs[i].ToString();
            }
        }
        if (Application.isPlaying)
        {
            if (mLines.Count > 20) 
            {
                mLines.RemoveAt(0);
            }
            mLines.Add(text);
mWriteTxt.Add(text);
        }
    }
 
    void OnGUI()
    {
        GUI.color = Color.red;
        for (int i = 0, imax = mLines.Count; i < imax; ++i)
        {
if(mEnableLog != 0)
{
            GUILayout.Label(mLines[i]);
}
        }
    }

public void Log(string message)
{
Log(message,null);
}
public void Log(string message, string context)
{
AddLog(message,context, LogType.Log);
}
public void LogError(string message)
{
LogError(message,null);
}
public void LogError(string message, object context)
{
AddLog(message,context, LogType.Error);
}
public void LogWarning(string message)
{
LogWarning(message,null);
}
public void LogWarning(string message, object context)
{
AddLog(message,context,LogType.Warning);
}
public void LogException(string message)
{
LogException(message,null);
}
public void LogException(string message, object context)
{
AddLog(message,context,LogType.Exception);
}
public void LogCustom(string message)
{
LogCustom(message,null);
}
public void LogCustom(string message, object context)
{
AddLog(message,context,LogType.Exception);
}

// public void SetEnableLog(int enableLog)
// {
// mEnableLog = enableLog;
// }
// public void SetLogType(int logType)
// {
// mLogType = logType;
// }

}


附带git地址:https://github.com/unityengine/OutLog

---

转载请注明本文标题和链接:《Debug日志抓取 输出 显示

分享到:

发表评论

路人甲 表情
看不清楚?点图切换 Ctrl+Enter快速提交