Debug_unity

using System;
using System.IO;

namespace UnityEngine
{
    public class Debuger
    {
        public static bool EnableLog = true;
        public static bool EnableTime = true;
        public static bool EnableSave = false;
        public static bool EnableStack = false;
        public static string LogFileDir = Application.persistentDataPath + "/DebugerLog";
        public static string LogFileName = "";
        public static string Prefix = "> ";
        public static StreamWriter LogFileWriter = null;

        public static void Log(object message)
        {
            if (!Debuger.EnableLog) return;
            string msg = GetLogTime() + message;
            Debug.Log(Prefix + msg, null);
            LogToFile("[I]" + msg);
        }
        public static void Log(object message, Object context)
        {
            if (!Debuger.EnableLog) return;
            string msg = GetLogTime() + message;
            Debug.Log(Prefix + msg, context);
            LogToFile("[I]" + msg);
        }
        public static void LogError(object message)
        {
            string msg = GetLogTime() + message;
            Debug.LogError(Prefix + msg, null);
            LogToFile("[E]" + msg);
        }
        public static void LogError(object message, Object context)
        {
            string msg = GetLogTime() + message;
            Debug.LogError(Prefix + msg, context);
            LogToFile("[E]" + msg);
        }
        public static void LogWarning(object message)
        {
            string msg = GetLogTime() + message;
            Debug.LogWarning(Prefix + msg, null);
            LogToFile("[W]" + msg);
        }
        public static void LogWarning(object message, Object context)
        {
            string msg = GetLogTime() + message;
            Debug.LogWarning(Prefix + msg, context);
            LogToFile("[W]" + msg);
        }

        public static void Log(string tag, string message)
        {
            if (!Debuger.EnableLog) return;
            message = GetLogText(tag, message);
            Debug.Log(Prefix + message);
            LogToFile("[I]" + message);
        }
        public static void Log(string tag, string format, params object[] args)
        {
            if (!Debuger.EnableLog) return;
            string message = GetLogText(tag, string.Format(format, args));
            Debug.Log(Prefix + message);
            LogToFile("[I]" + message);
        }
        public static void LogError(string tag, string message)
        {
            message = GetLogText(tag, message);
            Debug.LogError(Prefix + message);
            LogToFile("[E]" + message, true);
        }
        public static void LogError(string tag, string format, params object[] args)
        {
            string message = GetLogText(tag, string.Format(format, args));
            Debug.LogError(Prefix + message);
            LogToFile("[E]" + message, true);
        }
        public static void LogWarning(string tag, string message)
        {
            message = GetLogText(tag, message);
            Debug.LogWarning(Prefix + message);
            LogToFile("[W]" + message);
        }
        public static void LogWarning(string tag, string message, params object[] args)
        {
            message = GetLogText(tag, message);
            Debug.LogWarning(Prefix + message);
            LogToFile("[W]" + message);
        }
        private static string GetLogText(string tag, string message)
        {
            string str = "";
            if (Debuger.EnableTime)
            {
                DateTime now = DateTime.Now;
                str = now.ToString("HH:mm:ss.fff") + " ";
            }
            str = str + tag + "::" + message;
            return str;
        }
        private static String GetLogTime()
        {
            string str = "";
            if (Debuger.EnableTime)
            {
                DateTime now = DateTime.Now;
                str = now.ToString("HH:mm:ss.fff") + " ";
            }
            return str;
        }
        private static void LogToFile(string message, bool EnableStack = false)
        {
            if (!EnableSave) return;
            if (LogFileWriter == null)
            {
                DateTime now = DateTime.Now;
                LogFileName = now.GetDateTimeFormats('s')[0].ToString();
                LogFileName = LogFileName.Replace("-", "_");
                LogFileName = LogFileName.Replace(":", "_");
                LogFileName = LogFileName.Replace(" ", "");
                LogFileName += ".log";
                string fullpath = LogFileDir + LogFileName;
                Debug.Log(fullpath);
                try
                {
                    if (!Directory.Exists(LogFileDir))
                    {
                        Directory.CreateDirectory(LogFileDir);
                    }

                    LogFileWriter = File.AppendText(fullpath);
                    LogFileWriter.AutoFlush = true;
                }
                catch (Exception e)
                {
                    LogFileWriter = null;
                    Debug.LogError("LogToCache() " + e.Message + e.StackTrace);
                    return;
                }

                if (LogFileWriter != null)
                {
                    try
                    {
                        LogFileWriter.WriteLine(message);
                        if (EnableStack || Debuger.EnableStack)
                        {
                            LogFileWriter.WriteLine(StackTraceUtility.ExtractStackTrace());
                        }
                    }
                    catch (Exception)
                    {
                        return;
                    }
                }
            }
        }
    }
}
未完待续

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值