using System; using System.IO; using System.Text; using UnityEngine; namespace XRTool.Util { /// /// 日志输出,请勿使用Debug或者Print等自带的日志 /// 日志分级,普通日志,错误日志,异常日志 /// debug模式可以输出普通日志,其他日志必输出 /// 通过配置参数实现不同的日志等级 /// 为了避免大量日志输出,采用优化方案写入日志 /// public class UnityLog : Singleton { private string logBuilder; private FileStream logStream; private FileStream errorStream; private FileStream exceptStream; public event Action MessageReceived; /// /// 在编辑器中是否输出日志文件 /// public bool isOutLogEditor = true; /// /// 日志等级,默认为1,正式发布版本为0 /// 等级越高代表可输出的日志越多 /// public int logLevel = 1; public const string NULL = "null"; private bool isFristLog = true; private bool isFristLogError = true; private bool isFristLogExcept = true; public static string LogPath = "LogData"; public UnityLog() { InitLog(); } /// /// 接受系统级别的日志信息 /// public void InitLog(FileMode mode = FileMode.Append) { if (logStream == null) { string appPath = Application.persistentDataPath + "/" + LogPath; if (!Directory.Exists(appPath)) { Directory.CreateDirectory(appPath); } string logPath = Path.Combine(appPath, "log.txt"); string errorPath = Path.Combine(appPath, "error.txt"); string exceptPath = Path.Combine(appPath, "except.txt"); //Log(appPath); logStream = new FileStream(logPath, mode); errorStream = new FileStream(errorPath, mode); exceptStream = new FileStream(exceptPath, mode); Application.logMessageReceived += LogMessageReceived; } } /// /// 接受系统日志 /// /// /// /// private void LogMessageReceived(string condition, string stackTrace, LogType type) { if (!isOutLogEditor) { return; } logBuilder = null; FileStream fileStream = null; if (string.IsNullOrEmpty(condition) && string.IsNullOrEmpty(stackTrace)) { return; } bool isWriteFrist = false; #if UNITY_EDITOR || !BUILDTYPE_RELEASE //Release if (type == LogType.Log) { ///日志等级大于0时才会开启日志 if (logLevel >= 1) { fileStream = logStream; } if (isFristLog) { isWriteFrist = true; isFristLog = false; } } #endif if (type == LogType.Exception) { fileStream = exceptStream; if (isFristLogExcept) { isWriteFrist = true; isFristLogExcept = false; } } else if (type == LogType.Error) { fileStream = errorStream; if (isFristLogError) { isWriteFrist = true; isFristLogError = false; } } if (fileStream != null) { DateTime time = DateTime.Now; logBuilder = time + ": " + condition + stackTrace + "\n"; MessageReceived?.Invoke(logBuilder); if (isWriteFrist) { if (BuildConfigMgr.Instance.IsInit) { ReleaseLog("\n----------Version Log------------\n" + Application.productName + "_" + Application.version + "_" + BuildConfig.Instance.buildTime + "\n", fileStream); } } ReleaseLog(logBuilder, fileStream); fileStream.Flush(); } fileStream = null; logBuilder = null; } /// /// 释放资源 /// public void ReleaseLog() { Application.logMessageReceived -= LogMessageReceived; if (logStream != null) { logStream.Close(); } if (errorStream != null) { errorStream.Close(); } if (exceptStream != null) { exceptStream.Close(); } logStream = null; errorStream = null; exceptStream = null; } public void Log(object log, int logLevel = 1) { //Log(log != null ? log.ToString() : "null", logLevel); ///如果是编辑器模式或者非商用APP版本,可以打日志文件 #if UNITY_EDITOR || !BuildType_RELEASE //Release bool isForce = false; #if UNITY_EDITOR isForce = this.logLevel >= 4; #endif if (isForce || logLevel <= this.logLevel) { Debug.Log(log != null ? log.ToString() : NULL); } #endif } /// /// 正常的日志输出 /// public void Log(string log, int logLevel = 1) { ///如果是编辑器模式或者非商用APP版本,可以打日志文件 #if UNITY_EDITOR || !BuildType_RELEASE //Release bool isForce = false; #if UNITY_EDITOR isForce = this.logLevel >= 4; #endif if (isForce || logLevel <= this.logLevel) { Debug.Log(log); } #endif } /// /// 错误日志输出 /// public void LogError(string log, int logLevel = 1) { ///错误日志任何模式都会输出 Debug.LogError(log); } /// /// 错误日志输出 /// public void LogException(Exception ex, int logLevel = 1) { ///异常日志由系统自动输出 Debug.LogException(ex); } /// /// 日志输出,替换对应的日志文件 /// public void Log(string log, LogType logType, int logLevel = 1) { if (logType == LogType.Log) { Log(log, logLevel); } else if (logType == LogType.Error) { LogError(log, logLevel); } } /// /// 释放资源 /// public void ReleaseLog(string msg, FileStream stream) { if (!string.IsNullOrEmpty(msg)) { WriteLogToFile(msg, stream); } } /// /// 将日志写入到文件中 /// /// /// public void WriteLogToFile(string log, FileStream stream) { if (stream != null) { ///UTF8的编码格式转化成byte字节流 byte[] buttf = Encoding.Default.GetBytes(log); stream.Write(buttf, 0, log.Length); } } public void ClearAllLog() { ReleaseLog(); InitLog(FileMode.Create); } } }