123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256 |
- using System;
- using System.IO;
- using System.Text;
- using UnityEngine;
- namespace XRTool.Util
- {
- /// <summary>
- /// 日志输出,请勿使用Debug或者Print等自带的日志
- /// 日志分级,普通日志,错误日志,异常日志
- /// debug模式可以输出普通日志,其他日志必输出
- /// 通过配置参数实现不同的日志等级
- /// 为了避免大量日志输出,采用优化方案写入日志
- /// </summary>
- public class UnityLog : Singleton<UnityLog>
- {
- private string logBuilder;
- private FileStream logStream;
- private FileStream errorStream;
- private FileStream exceptStream;
- public event Action<string> MessageReceived;
- /// <summary>
- /// 在编辑器中是否输出日志文件
- /// </summary>
- public bool isOutLogEditor = true;
- /// <summary>
- /// 日志等级,默认为1,正式发布版本为0
- /// 等级越高代表可输出的日志越多
- /// </summary>
- 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();
- }
- /// <summary>
- /// 接受系统级别的日志信息
- /// </summary>
- 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;
- }
- }
- /// <summary>
- /// 接受系统日志
- /// </summary>
- /// <param name="condition"></param>
- /// <param name="stackTrace"></param>
- /// <param name="type"></param>
- 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;
- }
- /// <summary>
- /// 释放资源
- /// </summary>
- 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
- }
- /// <summary>
- /// 正常的日志输出
- /// </summary>
- 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
- }
- /// <summary>
- /// 错误日志输出
- /// </summary>
- public void LogError(string log, int logLevel = 1)
- {
- ///错误日志任何模式都会输出
- Debug.LogError(log);
- }
- /// <summary>
- /// 错误日志输出
- /// </summary>
- public void LogException(Exception ex, int logLevel = 1)
- {
- ///异常日志由系统自动输出
- Debug.LogException(ex);
- }
- /// <summary>
- /// 日志输出,替换对应的日志文件
- /// </summary>
- 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);
- }
- }
- /// <summary>
- /// 释放资源
- /// </summary>
- public void ReleaseLog(string msg, FileStream stream)
- {
- if (!string.IsNullOrEmpty(msg))
- {
- WriteLogToFile(msg, stream);
- }
- }
- /// <summary>
- /// 将日志写入到文件中
- /// </summary>
- /// <param name="log"></param>
- /// <param name="logType"></param>
- 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);
- }
- }
- }
|