基本信息
源码名称:unity Debuger写日志到文件(.cs类文件)
源码大小:1.17KB
文件格式:.zip
开发语言:C#
更新时间:2020-03-30
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):78630559

本次赞助数额为: 20 元 
   源码介绍



using UnityEngine;
using System.IO;
using System;

public class Debuger : MonoBehaviour
{
public static bool EnableLog = true;
public static bool EnableTime = true;
public static bool EnableSave = true;
public static bool EnableStack = true;
public static string Prefix = ">>";
public static string LogFilePath  =Application.persistentDataPath "/DebugerLog/";
public static string LogFileName = "";
public static StreamWriter LogFileWriter = null;

public static void SetDebugerInfo (bool enableLog = true, bool enableTime = true, bool enableSave = true, bool enableStack = true)
{
EnableLog = enableLog;
EnableTime = enableTime;
EnableSave = enableSave;
EnableStack = enableStack;
}

public static void SetDebugerLogFilePath (string logFilePath)
{
LogFilePath = logFilePath;
}

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 ("yyyy年mm月dd日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);
}

}
}
}