自己写得一个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里,蛮方便的,不过我没加删除日志的功能。
写完感觉已经精疲力尽,删除什么的以后慢慢往里拓展功能,等我完善了再发一个上来。就酱
相关文章推荐
- 3D 打印照相馆 尝试“克隆”一个自己
- 实现安卓程序退出后重进自己程序的一个小功能(android,unity)
- 【写一个自己的js库】 2.实现自己的调试日志
- GitHub的一个日志打印库LazyLogger
- 自己造一个日志轮子
- 【Unity 3D】学习笔记二十八:unity工具类
- 自己用反射写的一个javaBean工具类
- 【Unity】理解协程的原理1——实现一个自己的WaitForSeconds
- Thinking in java 为前一个练习中的类添加一个重载的构造器,令其接受一个字符串作为参数,并在构造器中把你自己的消息和接收到的参数打印出来
- 想给自己制作一个简单的相册吗?快来看看 怎样实现3D图片相册效果
- 编写一个程序,开启3个线程,这3个线程的ID分别为A、B、C,每个线程将自己的ID在屏幕上打印10遍,要求输出结果必须按ABC的顺序显示;如:ABCABC….依次递推
- 写一个自己的验证码工具类
- 实现一个函数,打印乘法口诀表,口诀表的行数和列数自己指定, 输入9,输出9*9口诀表,输出12,输出12*12的乘法口诀表。
- 一个打印Log日志,好用的工具logger
- 编写一个程序,开启3个线程,这3个线程的ID分别为A、B、C,每个线程将自己的ID在屏幕上打印10遍
- Semslin 3D打印固件 项目日志节选(三)—— Marlin架构解析
- 3D打印 真能打印一个“我”?
- 日志打印工具类LogUtils
- 编写一个程序,开启3个线程,这3个线程的ID分别为A、B、C,每个线程将自己的ID在屏幕上打印10遍,要求输出结果必须按ABC的顺序显示;如:ABCABC….依次递推。
- [Unity通信]一个基于socket的3DARPG网络游戏(一):建立连接和事件分发