LogSystem

自定义输出系统。

1、方便测试,获取出错信息。

2、打包测试,方便获取出错信息。

3、发包时,可以屏蔽Unity的Log信息


代码如下:都有注释的

using System;
using System.IO;
using System.Text;
using UnityEngine;


/// <summary>
/// 输出
/// </summary>
public class LogSystem
{
    #region 内部实现
    //声明委托
    public delegate void LogSystemDelegate(LogSystemType type, string msg, Exception exception = null);
    public static LogSystemDelegate printLog = null;


    //向文件中写入输出
    public static StreamWriter streamWriter = null;
    public static StringBuilder LogInfo = new StringBuilder();


    //是否输出信息
    public static bool IsPrintLog = true;
    //是否是编辑器
    public static bool IsEditor = true;


    //输出目录
    public static string LogPath = "";


    static LogSystem()
    {
        if (Application.platform == RuntimePlatform.IPhonePlayer)
        {
            string path = Application.persistentDataPath.Substring(0, Application.persistentDataPath.Length - 5);
            path = path.Substring(0, path.LastIndexOf('/'));
            LogPath = Path.Combine(Path.Combine(path, "Documents"), String.Format("{0}{1}", System.DateTime.Now.ToFileTime(), "Log.txt"));


        }
        else
        {
            LogPath = String.Format("{0}{1}{2}", Application.persistentDataPath, System.DateTime.Now.ToFileTime(), "Log.txt");
        }




        //Test
        string logFolder = @"C:\Users\admin\Desktop\Log";
        if (!Directory.Exists(logFolder))
            Directory.CreateDirectory(logFolder);
        LogPath = @"C:\Users\admin\Desktop\Log\Log.txt";
        if (File.Exists(LogPath) == false)
            File.Create(LogPath).Close();


        streamWriter = File.AppendText(LogPath);




        //注册委托
        printLog = LogFunction;
    }


    //输出方法
    public static void LogFunction(LogSystemType type, string msg, Exception exception = null)
    {
        LogInfo.Append(msg);
        LogInfo.Append("\n");


        if (exception != null)
        {
            LogInfo.Append(exception.ToString());
            LogInfo.Append("\n");
        }


        if (streamWriter != null)
        {
            streamWriter.Write(msg);
            streamWriter.Write("\r\n");
            if (exception != null)
            {
                streamWriter.Write(exception.ToString());
                streamWriter.Write("\r\n");
            }
            streamWriter.Flush();//清理当前编写器的所有缓冲区,并使所有缓冲数据写入基础流。
        }


        if (IsPrintLog)
        {
            if (IsEditor)
            {
                switch (type)
                {
                    case LogSystemType.Error:
                        UnityEngine.Debug.LogError(msg);
                        break;
                    case LogSystemType.Warning:
                        UnityEngine.Debug.LogWarning(msg);
                        break;
                    default:
                        UnityEngine.Debug.Log(msg);
                        break;
                }
            }
        }
        else
        {
            if (IsEditor && type == LogSystemType.Error)
                UnityEngine.Debug.LogError(msg);
        }


    }


    private static void Output(LogSystemType type, string msg, Exception exception = null)
    {
        if (printLog != null)
            printLog(type, msg, exception);
    }


    #endregion


    #region 外部调用方法


    public static void Error(string format, params object[] args)
    {
        string str = string.Format("[Error]:" + format, args);
        Output(LogSystemType.Error, str);
    }


    public static void Assert(string format, params object[] args)
    {
        string str = string.Format("[Assert]:" + format, args);
        Output(LogSystemType.Assert, str);
    }


    public static void Warning(string format, params object[] args)
    {
        string str = string.Format("[Warning]:" + format, args);
        Output(LogSystemType.Warning, str);
    }


    public static void Log(string format, params object[] args)
    {
        string str = string.Format("[Log]:" + format, args);
        Output(LogSystemType.Log, str);
    }


    public static void Exception(string format, params object[] args)
    {
        string str = string.Format("[Exception]:" + format, args);
        Output(LogSystemType.Exception, str);
    }


    #endregion


}


public enum LogSystemType
{
    //
    // Summary:
    //     ///
    //     LogType used for Errors.
    //     ///
    Error = 0,
    //
    // Summary:
    //     ///
    //     LogType used for Asserts. (These could also indicate an error inside Unity itself.)
    //     ///
    Assert = 1,
    //
    // Summary:
    //     ///
    //     LogType used for Warnings.
    //     ///
    Warning = 2,
    //
    // Summary:
    //     ///
    //     LogType used for regular log messages.
    //     ///
    Log = 3,
    //
    // Summary:
    //     ///
    //     LogType used for Exceptions.
    //     ///
    Exception = 4
}


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


public class LogSystemGUI : MonoBehaviour {




    public bool canPrintLog = true;


    void Awake()
    {
        //注册信息
        Application.logMessageReceived += PrintLog;
    }




    void Start()
    {
        //允许输出
        LogSystem.IsPrintLog = canPrintLog;




        //Test1
        LogSystem.Error("This is a LogTest");


        //Test2
        List<int> testList = new List<int>();
        testList.Add(0);
        testList.Add(1);
        testList.Add(2);


        foreach (var item in testList)
        {
            testList.Remove(item);
        }
    }


    void PrintLog(string condition, string stackTrace, LogType type)
    {
        switch (type)
        {
            case LogType.Error:
                LogSystem.Error(condition + "\r\n" + stackTrace);
                break;
            case LogType.Assert:
                LogSystem.Assert(condition);
                break;
            case LogType.Exception:
                LogSystem.Exception(condition + "\r\n" + stackTrace);
                break;
            case LogType.Log:
                LogSystem.Log(condition);
                break;
            case LogType.Warning:
                LogSystem.Warning(condition);
                break;
        }
    }




    private bool ShowLog = true;
    Vector2 scrollPosition;
    int LogIndex = 0;


    void OnGUI()
    {
        if (LogSystem.IsPrintLog)
        {
            if (GUI.Button(new UnityEngine.Rect(Screen.width * 1 / 3, 0, 100, 80), "屏蔽日志"))
            {
                LogSystem.IsPrintLog = false;
            }
            if (ShowLog)
            {
                if (GUI.Button(new UnityEngine.Rect(Screen.width * 1 / 3 + 100, 0, 100, 80), "隐藏日志"))
                {
                    ShowLog = false;
                }
            }
            else
            {
                if (GUI.Button(new UnityEngine.Rect(Screen.width * 1 / 3 + 100, 0, 100, 80), "显示日志"))
                {
                    ShowLog = true;
                }
            }
        }
        else
        {
            if (GUI.Button(new UnityEngine.Rect(Screen.width * 1 / 3, 0, 100, 80), "打印日志"))
            {
                LogSystem.IsPrintLog = true;
            }
        }


        if (LogSystem.IsPrintLog)
        {
            if (String.IsNullOrEmpty(LogSystem.LogPath) == false)
                GUILayout.TextField(LogSystem.LogPath);
            if (LogSystem.LogInfo.Length > 0 && ShowLog)
            {
                scrollPosition = GUILayout.BeginScrollView(scrollPosition, GUILayout.Width(Screen.width * 9 / 10), GUILayout.Height(Screen.height));
                String str = LogSystem.LogInfo.ToString().Substring(LogIndex);
                if (str.Length > 10000)
                {
                    try
                    {
                        checked
                        {
                            LogIndex += 8000;
                        }
                    }
                    catch (Exception)
                    {
                        LogSystem.LogInfo.Remove(0, LogSystem.LogInfo.Length);
                        LogIndex = 0;
                    }
                    str = LogSystem.LogInfo.ToString().Substring(LogIndex);
                }
                GUILayout.TextField(str);
                GUI.EndScrollView();
            }
        }
    }

}









评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值