您的位置:首页 > 移动开发 > Unity3D

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

2017-09-12 09:34 411 查看
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里,蛮方便的,不过我没加删除日志的功能。

写完感觉已经精疲力尽,删除什么的以后慢慢往里拓展功能,等我完善了再发一个上来。就酱


 

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐