unity 日志级别_【Unity】通用的Debugger日志模块
usingSystem;usingSystem.IO;namespaceUnityEngine
{///
///系統日志模塊///
public classDebugger
{public static bool EnableLog; //是否啟用日志,僅可控制普通級別的日志的啟用與關閉,LogError和LogWarn都是始終啟用的。
public static bool EnableTime = true;public static bool EnableSave = false; //是否允許保存日志,即把日志寫入到文件中
public static bool EnableStack = false;public static string LogFileDir = Application.persistentDataPath + "/DebuggerLog/";public static string LogFileName = "";public static string Prefix = ">"; //用于與Unity默認的系統日志做區分。本日志系統輸出的日志頭部都會帶上這個標記。
public static StreamWriter LogFileWriter = null;public static boolUseUnityEngine;private static string GetLogText(string tag, stringmessage)
{string str = "";if(EnableTime)
{
str= DateTime.Now.ToString("HH:mm:ss.fff") + " ";
}return (str + tag + "::" +message);
}private static stringGetLogTime()
{string str = "";if(EnableTime)
{
str= DateTime.Now.ToString("HH:mm:ss.fff") + " ";
}returnstr;
}public static void Log(objectmessage)
{if (!Debugger.EnableLog)return;string str = GetLogTime() +message;
Debug.Log(Prefix+ str, null);
LogToFile("[I]" + str, false);
}public static void Log(objectmessage, Object context)
{if (!Debugger.EnableLog)return;string str = GetLogTime() +message;
Debug.Log(Prefix+str, context);
LogToFile("[I]" + str, false);
}public static void Log(string tag, stringmessage)
{if (!Debugger.EnableLog)return;
message=GetLogText(tag, message);
Debug.Log(Prefix+ message, null);
LogToFile("[I]" + message, false);
}public static void Log(string tag, string format, params object[] args)
{if (!Debugger.EnableLog)return;string logText = GetLogText(tag, string.Format(format, args));
Debug.Log(Prefix+ logText, null);
LogToFile("[I]" + logText, false);
}public static void LogError(objectmessage)
{string str = GetLogTime() +message;
Debug.Log(Prefix+ str, null);
LogToFile("[E]" + str, true);
}public static void LogError(objectmessage, Object context)
{string str = GetLogTime() +message;
Debug.Log(Prefix+str, context);
LogToFile("[E]" + str, true);
}public static void LogError(string tag, stringmessage)
{
message=GetLogText(tag, message);
Debug.Log(Prefix+ message, null);
LogToFile("[E]" + message, true);
}public static void LogError(string tag, string format, params object[] args)
{string logText = GetLogText(tag, string.Format(format, args));
Debug.Log(Prefix+ logText, null);
LogToFile("[E]" + logText, true);
}///
///將日志寫入到文件中///
///
///
private static void LogToFile(string message, bool EnableStack = false)
{if (!Debugger.EnableSave)return;if (LogFileWriter == null)
{
LogFileName= DateTime.Now.GetDateTimeFormats('s')[0].ToString();
LogFileName= LogFileName.Replace("-", "_");
LogFileName= LogFileName.Replace(":", "_");
LogFileName= LogFileName.Replace(" ", "");
LogFileName= LogFileName + ".log";if (string.IsNullOrEmpty(LogFileDir))
{try{if(UseUnityEngine)
{
LogFileDir= Application.persistentDataPath + "/DebuggerLog/";
}else{
LogFileDir= AppDomain.CurrentDomain.BaseDirectory + "/DebuggerLog/";
}
}catch(Exception exception)
{
Debug.Log(Prefix+ "獲取 Application.persistentDataPath 報錯!" + exception.Message, null);return;
}
}string path = LogFileDir +LogFileName;try{if (!Directory.Exists(LogFileDir))
{
Directory.CreateDirectory(LogFileDir);
}
LogFileWriter=File.AppendText(path);
LogFileWriter.AutoFlush= true;
}catch(Exception exception2)
{
LogFileWriter= null;
Debug.Log("LogToCache()" + exception2.Message + exception2.StackTrace, null);return;
}
}if (LogFileWriter != null)
{try{
LogFileWriter.WriteLine(message);if ((EnableStack || Debugger.EnableStack) &&UseUnityEngine)
{
LogFileWriter.WriteLine(StackTraceUtility.ExtractStackTrace());
}
}catch(Exception)
{
}
}
}public static void LogWarning(objectmessage)
{string str = GetLogTime() +message;
Debug.Log(Prefix+ str, null);
LogToFile("[W]" + str, false);
}public static void LogWarning(objectmessage, Object context)
{string str = GetLogTime() +message;
Debug.Log(Prefix+str, context);
LogToFile("[W]" + str, false);
}public static void LogWarning(string tag, stringmessage)
{
message=GetLogText(tag, message);
Debug.Log(Prefix+ message, null);
LogToFile("[W]" + message, false);
}public static void LogWarning(string tag, string format, params object[] args)
{string logText = GetLogText(tag, string.Format(format, args));
Debug.Log(Prefix+ logText, null);
LogToFile("[W]" + logText, false);
}
}
}
總結
以上是生活随笔為你收集整理的unity 日志级别_【Unity】通用的Debugger日志模块的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 初步了解java虚拟机
- 下一篇: hdu 5045 Contest(状态压