62 lines
1.8 KiB
C#
62 lines
1.8 KiB
C#
using System;
|
|
using UnityEngine;
|
|
using AibisDream.Utility;
|
|
|
|
namespace AibisDream.Kit
|
|
{
|
|
internal static class LogKit
|
|
{
|
|
/// <summary>
|
|
/// 文件名称格式
|
|
/// 请注意,不要删除时间格式,否则会造成保存不成功
|
|
/// </summary>
|
|
public static string LogFileName => "Log{0:_yyyy_MM_dd}.txt";
|
|
|
|
/// <summary>
|
|
/// 日志保存最近几天的内容
|
|
/// </summary>
|
|
public const int SaveDays = 30;
|
|
|
|
/// <summary>
|
|
/// 每一行的打印内容
|
|
/// </summary>
|
|
private static string LogContent => Time + ": {0}\n{1}";
|
|
|
|
private static FileLogger _fileLogger;
|
|
private static bool _isInitialized;
|
|
|
|
private static string Time => DateTime.Now.ToString("[HH:mm:ss.fffd]");
|
|
|
|
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
|
|
private static void Initialize()
|
|
{
|
|
#if !UNITY_EDITOR
|
|
if (_isInitialized)
|
|
return;
|
|
|
|
_fileLogger = new FileLogger(LogFileName, ConstRef.LogFilePath, SaveDays);
|
|
LogMessage($"Ver.{Application.version}", "", LogType.Log);
|
|
Application.logMessageReceivedThreaded += LogMessage;
|
|
Application.quitting += Shutdown;
|
|
_isInitialized = true;
|
|
#endif
|
|
}
|
|
|
|
private static void LogMessage(string condition, string stackTrace, LogType type)
|
|
{
|
|
_fileLogger?.Write(string.Format(LogContent, condition, stackTrace));
|
|
}
|
|
|
|
private static void Shutdown()
|
|
{
|
|
if (!_isInitialized)
|
|
return;
|
|
|
|
Application.logMessageReceivedThreaded -= LogMessage;
|
|
Application.quitting -= Shutdown;
|
|
_fileLogger?.OnDestroy();
|
|
_fileLogger = null;
|
|
_isInitialized = false;
|
|
}
|
|
}
|
|
} |