自己写得一个Unity 3d日志打印工具类

来源:互联网 发布:淘宝客返利订单查询 编辑:程序博客网 时间:2024/05/26 09:53
using UnityEngine;using System.IO;using System;public class Debuger : MonoBehaviour{    public static bool EnableLog;    public static bool EnableTime = false;    public static bool EnableSave = false;    public static bool EnableStack = false;    public static string Prefix = ">>";    public static string LogFilePath = Application.persistentDataPath + "/DebugerLog/";    public static string LogFileName = "";    public static StreamWriter LogFileWriter = null;    public static void Log(object message)    {        if (!Debuger.EnableLog) return;        string str = GetLogTime() + message;        Debug.Log(Prefix + str);        LogToFile("[I]"  + str);    }    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 message,params object[] args)    {        if (!Debuger.EnableLog) return;        message = GetLogText(tag, string.Format(message, args));        Debug.Log(Prefix + message);        LogToFile("[I]" + message);    }    public static void LogError(object message)    {        string str = GetLogTime() + message;        Debug.LogError(Prefix + str);        LogToFile("[E]" + str,true);    }    public static void LogError(object message ,UnityEngine.Object context)    {        string str = GetLogTime() + message;        Debug.LogError(Prefix + str ,context);        LogToFile("[E]" + str,true);    }    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 message,params object[] args)    {        message = GetLogText(tag, string.Format(message,args));        Debug.LogError(Prefix + message);        LogToFile("[E]" + message, true);    }    public static void LogWarning(object message)    {        string str = GetLogTime() + message;        Debug.LogWarning(Prefix + str);        LogToFile("[W]" + str);    }    public static void LogWarning(object message,UnityEngine.Object context)    {        string str = GetLogTime() + message;        Debug.LogWarning(Prefix + str, context);        LogToFile("[W]" + str,true);    }    public static void LogWarning(string tag, string message)    {        string str = GetLogText(tag,message);        Debug.LogWarning(Prefix + str);        LogToFile("[W]" + str, true);    }    public static void LogWarning(string tag, string message,params object[] args)    {        string str = GetLogText(tag, string.Format(message,args));        Debug.LogWarning(Prefix + str);        LogToFile("[W]" + str, true);    }    private static string GetLogText(string tag,string message)    {        string str = "";        str = string.Format("{0}{1}::{2}",GetLogTime(),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;    }    public static void LogToFile(string message, bool EnableStack = false)    {        if (!EnableSave) return;        if (LogFileWriter == null)        {            DateTime now = DateTime.Now;            LogFileName = now.GetDateTimeFormats('s')[0].ToString();//2017-08-30T15:26:51            LogFileName = LogFileName.Replace("-","_");            LogFileName = LogFileName.Replace(":", "_");            LogFileName = LogFileName.Replace(" ", "");            LogFileName += ".log";            string fullPath = LogFilePath + LogFileName;            Debug.Log(fullPath);            try            {                if (!Directory.Exists(LogFilePath))                {                    Directory.CreateDirectory(LogFilePath);                }                LogFileWriter = File.AppendText(fullPath);                LogFileWriter.AutoFlush = true;            }            catch(Exception e)            {                LogFileWriter = null;                Debug.LogError("LogToCache" + e.Message);            }        }        if (LogFileWriter != null)        {            try            {                LogFileWriter.WriteLine(message);                if (EnableStack || Debuger.EnableStack) return;                LogFileWriter.WriteLine(StackTraceUtility.ExtractStackTrace());            }            catch (Exception e)            {                Debug.LogError("LogToWrite" + e.Message);            }        }    }}
 

using System.Collections;using System.Collections.Generic;using UnityEngine;using System.Reflection;using System.Diagnostics;using System;using Debug = UnityEngine.Debug;using Object = System.Object;

public static class DebugerExtension{    [Conditional("EnableLog")]    public static void Log(this object obj,string message)    {        if (!Debuger.EnableLog) return;

        Debuger.Log(GetLogTag(obj), message);    }

    public static void LogError(this object obj, string message)    {        Debuger.LogError(GetLogTag(obj), message);    }

    public static void LogWarning(this object obj, string message)    {        Debuger.LogWarning(GetLogTag(obj), message);    }

    [Conditional("EnableLog")]    public static void Log(this object obj, string message,params object[] args)    {        if (!Debuger.EnableLog) return;

        Debuger.Log(GetLogTag(obj), string.Format(message,args));    }

    public static void LogError(this object obj, string message, params object[] args)    {        Debuger.LogError(GetLogTag(obj), string.Format(message, args));    }

    public static void LogWarning(this object obj, string message, params object[] args)    {        Debuger.LogWarning(GetLogTag(obj), string.Format(message, args));    }

    public static string GetLogTag(object obj)    {        FieldInfo finfo = obj.GetType().GetField("LOG");        if (finfo != null)            return (string)finfo.GetValue(obj);

        return obj.GetType().Name;    }}

可以做成dll用,不过我每次都是放进代码库里在用(这样好像有点蠢,但我感觉其实区别不大来着),other setting里加个EnableLog就可以用了。

使用的时候把几个静态变量设置成true,直接this.Log打印要打印的东西,会自动打印出带时间和当前类的信息,而且会存到txt里,蛮方便的,不过我没加删除日志的功能。
写完感觉已经精疲力尽,删除什么的以后慢慢往里拓展功能,等我完善了再发一个上来。就酱奋斗

 

 

原创粉丝点击