开发者工具
This commit is contained in:
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: bfe8aa3a0b2c8c144af979bb5714ae85
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,99 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
|
||||
namespace AibisDream.Kit
|
||||
{
|
||||
internal class FileLogger
|
||||
{
|
||||
private StreamWriter _fileWriter;
|
||||
|
||||
private readonly string _filename;
|
||||
private readonly string _filePath;
|
||||
private readonly int _saveDays;
|
||||
|
||||
private static string CurrentDay => DateTime.Now.ToString("yyyyMMdd");
|
||||
private string _currentLogDay;
|
||||
private string _preSavePath;
|
||||
|
||||
/// <summary>
|
||||
/// 开始打印
|
||||
/// </summary>
|
||||
public FileLogger(string filename, string filePath, int saveDays)
|
||||
{
|
||||
_filename = filename;
|
||||
_filePath = filePath.TrimEnd('/') + "/";
|
||||
_saveDays = saveDays;
|
||||
StartNewFile();
|
||||
DeleteOldFile();
|
||||
_currentLogDay = CurrentDay;
|
||||
Write("*********************************************************\nSystem Start at " +
|
||||
DateTime.Now.ToString("HH:mm:ss") +
|
||||
"\n*********************************************************\n");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 新建文件
|
||||
/// </summary>
|
||||
private void StartNewFile()
|
||||
{
|
||||
if (_fileWriter != null)
|
||||
{
|
||||
_fileWriter.Close();
|
||||
_fileWriter.Dispose();
|
||||
}
|
||||
|
||||
_fileWriter = null;
|
||||
|
||||
if (!Directory.Exists(_filePath))
|
||||
{
|
||||
Directory.CreateDirectory(_filePath);
|
||||
}
|
||||
|
||||
_fileWriter = File.AppendText(_filePath + string.Format(_filename, DateTime.Now));
|
||||
}
|
||||
|
||||
public void Write(string content)
|
||||
{
|
||||
System.Globalization.CultureInfo.CurrentCulture.ClearCachedData();
|
||||
if (_currentLogDay != CurrentDay)
|
||||
{
|
||||
StartNewFile();
|
||||
DeleteOldFile();
|
||||
_currentLogDay = CurrentDay;
|
||||
}
|
||||
|
||||
if (_fileWriter is { BaseStream: { CanWrite: true } })
|
||||
_fileWriter.WriteLine(content);
|
||||
}
|
||||
|
||||
void DeleteOldFile()
|
||||
{
|
||||
var fileList = Directory.GetFiles(_filePath);
|
||||
if (fileList.Length > _saveDays)
|
||||
{
|
||||
_preSavePath = _filePath + string.Format(this._filename, DateTime.Now.AddDays(-_saveDays + 1));
|
||||
foreach (var t in fileList)
|
||||
{
|
||||
if (string.CompareOrdinal(_preSavePath, t) == 1)
|
||||
{
|
||||
File.Delete(t);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void OnDestroy()
|
||||
{
|
||||
if (_fileWriter == null)
|
||||
return;
|
||||
|
||||
Write("*********************************************************\nSystem Shutdown at " +
|
||||
DateTime.Now.ToString("HH:mm:ss") +
|
||||
"\n*********************************************************\n");
|
||||
|
||||
_fileWriter.Flush();
|
||||
_fileWriter.Close();
|
||||
_fileWriter = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 7a2dc29290b545c1974b6473534df853
|
||||
timeCreated: 1741445068
|
||||
@@ -0,0 +1,57 @@
|
||||
using System;
|
||||
using UnityEngine;
|
||||
|
||||
namespace AibisDream.Kit
|
||||
{
|
||||
internal class LogKit : MonoBehaviour
|
||||
{
|
||||
/// <summary>
|
||||
/// 文件名称格式
|
||||
/// 请注意,不要删除时间格式,否则会造成保存不成功
|
||||
/// </summary>
|
||||
public static string LogFileName => "Log{0:_yyyy_MM_dd}.txt";
|
||||
|
||||
/// <summary>
|
||||
/// 日志文件路径
|
||||
/// </summary>
|
||||
public static string LogPath => Application.streamingAssetsPath + "/LogFile";
|
||||
|
||||
/// <summary>
|
||||
/// 日志保存最近几天的内容
|
||||
/// </summary>
|
||||
public const int SaveDays = 30;
|
||||
|
||||
/// <summary>
|
||||
/// 每一行的打印内容
|
||||
/// </summary>
|
||||
private static string LogContent => "--------------------------" + Time + "--------------------------\n{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, LogPath, SaveDays);
|
||||
Application.logMessageReceivedThreaded += LogMessage;
|
||||
#endif
|
||||
}
|
||||
|
||||
private void LogMessage(string condition, string stackTrace, LogType type)
|
||||
{
|
||||
if (!type.Equals(LogType.Warning))
|
||||
{
|
||||
_fileLogger?.Write(string.Format(LogContent, condition, stackTrace));
|
||||
}
|
||||
}
|
||||
|
||||
private void OnDestroy()
|
||||
{
|
||||
_fileLogger?.OnDestroy();
|
||||
_fileLogger = null;
|
||||
Application.logMessageReceivedThreaded -= LogMessage;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 647335bd723dab94ea97b993d03af61e
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user