52 lines
1.5 KiB
C#
52 lines
1.5 KiB
C#
using System;
|
|
using System.IO;
|
|
using UnityEngine;
|
|
using AibisDream.Utility;
|
|
|
|
namespace AibisDream.Kit
|
|
{
|
|
internal class LogKit : MonoBehaviour
|
|
{
|
|
/// <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 FileLogger _fileLogger;
|
|
|
|
private static string Time => DateTime.Now.ToString("[HH:mm:ss.fffd]");
|
|
|
|
private void Awake()
|
|
{
|
|
#if !UNITY_EDITOR
|
|
DontDestroyOnLoad(gameObject);
|
|
_fileLogger = new FileLogger(LogFileName, ConstRef.LogFilePath, SaveDays);
|
|
LogMessage($"Ver.{Application.version}", "", LogType.Log);
|
|
Application.logMessageReceivedThreaded += LogMessage;
|
|
#endif
|
|
}
|
|
|
|
private void LogMessage(string condition, string stackTrace, LogType type)
|
|
{
|
|
_fileLogger?.Write(string.Format(LogContent, condition, stackTrace));
|
|
}
|
|
|
|
private void OnDestroy()
|
|
{
|
|
_fileLogger?.OnDestroy();
|
|
_fileLogger = null;
|
|
Application.logMessageReceivedThreaded -= LogMessage;
|
|
}
|
|
}
|
|
} |