123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285 |
- using Newtonsoft.Json;
- using System;
- using System.IO;
- using System.Text;
- using UnityEngine;
- namespace XRTool.Util
- {
-
-
-
-
-
-
-
- public static class UnityLog
- {
- private static string logBuilder;
- private static FileStream logStream;
- private static FileStream errorStream;
- private static FileStream exceptStream;
- public static event Action<string> MessageReceived;
-
-
-
- public static bool isOutLogEditor = true;
-
-
-
-
- public static int logLevel = 1;
- public const string NULL = "null";
- private static bool isFristLog = true;
- private static bool isFristLogError = true;
- private static bool isFristLogExcept = true;
- private static string logPath;
- public static FileStream LogStream
- {
- get
- {
- if (logStream == null)
- {
- logStream = new FileStream(Path.Combine(LogPath, "log.txt"), FileMode.Append);
- }
- return logStream;
- }
- }
- public static FileStream ErrorStream
- {
- get
- {
- if (errorStream == null)
- {
- errorStream = new FileStream(Path.Combine(LogPath, "error.txt"), FileMode.Append);
- }
- return errorStream;
- }
- }
- public static FileStream ExceptStream
- {
- get
- {
- if (exceptStream == null)
- {
- exceptStream = new FileStream(Path.Combine(LogPath, "except.txt"), FileMode.Append);
- }
- return exceptStream;
- }
- }
- public static string LogPath { get => logPath; set => logPath = value; }
- static UnityLog()
- {
- if (BuildConfigMgr.Instance.IsInit)
- {
- }
- LogPath = Path.Combine(BuildConfig.Instance.UserPath, "LogData");
- if (!Directory.Exists(LogPath))
- {
- Directory.CreateDirectory(LogPath);
- }
- Application.logMessageReceived += LogMessageReceived;
-
- }
-
-
-
-
-
-
- private static 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)
- {
-
- 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 + "_" + JsonConvert.SerializeObject(BuildConfig.Instance) + "\n",
- fileStream);
- }
- }
- ReleaseLog(logBuilder, fileStream);
- fileStream.Flush();
- }
- fileStream = null;
- logBuilder = null;
- }
-
-
-
- public static 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 static void Log(object log, int level = 1)
- {
-
-
- #if UNITY_EDITOR || !BuildType_RELEASE //Release
- bool isForce = false;
- #if UNITY_EDITOR
- isForce = logLevel >= 4;
- #endif
- if (isForce || level <= logLevel)
- {
- Debug.Log(log != null ? log.ToString() : NULL);
- }
- #endif
- }
-
-
-
- public static void Log(string log, int level = 1)
- {
-
- #if UNITY_EDITOR || !BuildType_RELEASE //Release
- bool isForce = false;
- #if UNITY_EDITOR
- isForce = logLevel >= 4;
- #endif
- if (isForce || level <= logLevel)
- {
- Debug.Log(log);
- }
- #endif
- }
-
-
-
- public static void LogError(string log, int logLevel = 1)
- {
-
- Debug.LogError(log);
- }
-
-
-
- public static void LogException(Exception ex, int level = 1)
- {
-
- Debug.LogException(ex);
- }
-
-
-
- public static 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 static void ReleaseLog(string msg, FileStream stream)
- {
- if (!string.IsNullOrEmpty(msg))
- {
- WriteLogToFile(msg, stream);
- }
- }
-
-
-
-
-
- public static void WriteLogToFile(string log, FileStream stream)
- {
- if (stream != null)
- {
-
- byte[] buttf = Encoding.Default.GetBytes(log);
- stream.Write(buttf, 0, log.Length);
- }
- }
- public static void ClearAllLog()
- {
- ReleaseLog();
- if (Directory.Exists(LogPath))
- {
- Directory.Delete(LogPath, true);
- }
- Directory.CreateDirectory(LogPath);
-
- }
- }
- }
|