From 9cd1a832c35fc50f7f7401946fb801ac0192db46 Mon Sep 17 00:00:00 2001 From: Ding Yuntian <1491671119@qq.com> Date: Sat, 8 Mar 2025 22:57:59 +0800 Subject: [PATCH] =?UTF-8?q?=E5=BC=80=E5=8F=91=E8=80=85=E5=B7=A5=E5=85=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Prefabs/UI/Bubble Left Bottom.prefab | 2 +- Assets/Scenes/Persistence.unity | 45 +++++++++ Assets/Scripts/Framework/LogKit.meta | 8 ++ Assets/Scripts/Framework/LogKit/FileLogger.cs | 99 +++++++++++++++++++ .../Framework/LogKit/FileLogger.cs.meta | 3 + Assets/Scripts/Framework/LogKit/LogKit.cs | 57 +++++++++++ .../Scripts/Framework/LogKit/LogKit.cs.meta | 11 +++ 7 files changed, 224 insertions(+), 1 deletion(-) create mode 100644 Assets/Scripts/Framework/LogKit.meta create mode 100644 Assets/Scripts/Framework/LogKit/FileLogger.cs create mode 100644 Assets/Scripts/Framework/LogKit/FileLogger.cs.meta create mode 100644 Assets/Scripts/Framework/LogKit/LogKit.cs create mode 100644 Assets/Scripts/Framework/LogKit/LogKit.cs.meta diff --git a/Assets/Resources/Prefabs/UI/Bubble Left Bottom.prefab b/Assets/Resources/Prefabs/UI/Bubble Left Bottom.prefab index 2e6a985d8..613997fbc 100644 --- a/Assets/Resources/Prefabs/UI/Bubble Left Bottom.prefab +++ b/Assets/Resources/Prefabs/UI/Bubble Left Bottom.prefab @@ -94,7 +94,7 @@ MonoBehaviour: m_Padding: m_Left: 30 m_Right: 45 - m_Top: 50 + m_Top: 55 m_Bottom: 30 m_ChildAlignment: 4 m_Spacing: 0 diff --git a/Assets/Scenes/Persistence.unity b/Assets/Scenes/Persistence.unity index ce1440f54..b7fe3e8b6 100644 --- a/Assets/Scenes/Persistence.unity +++ b/Assets/Scenes/Persistence.unity @@ -12469,6 +12469,7 @@ Transform: - {fileID: 1317294298} - {fileID: 1620453322} - {fileID: 369430730} + - {fileID: 1068968715} m_Father: {fileID: 0} m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} --- !u!1 &1015707783 @@ -12564,6 +12565,50 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: ac0b09e7857660247b1477e93731de29, type: 3} m_Name: m_EditorClassIdentifier: +--- !u!1 &1068968714 +GameObject: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + serializedVersion: 6 + m_Component: + - component: {fileID: 1068968715} + - component: {fileID: 1068968716} + m_Layer: 0 + m_Name: Log Kit + m_TagString: Untagged + m_Icon: {fileID: 0} + m_NavMeshLayer: 0 + m_StaticEditorFlags: 0 + m_IsActive: 1 +--- !u!4 &1068968715 +Transform: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1068968714} + serializedVersion: 2 + m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} + m_LocalPosition: {x: 0, y: 0, z: 0} + m_LocalScale: {x: 1, y: 1, z: 1} + m_ConstrainProportionsScale: 0 + m_Children: [] + m_Father: {fileID: 1009826577} + m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} +--- !u!114 &1068968716 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1068968714} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 647335bd723dab94ea97b993d03af61e, type: 3} + m_Name: + m_EditorClassIdentifier: --- !u!1 &1076102355 GameObject: m_ObjectHideFlags: 0 diff --git a/Assets/Scripts/Framework/LogKit.meta b/Assets/Scripts/Framework/LogKit.meta new file mode 100644 index 000000000..2661da971 --- /dev/null +++ b/Assets/Scripts/Framework/LogKit.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: bfe8aa3a0b2c8c144af979bb5714ae85 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Framework/LogKit/FileLogger.cs b/Assets/Scripts/Framework/LogKit/FileLogger.cs new file mode 100644 index 000000000..373d8efc6 --- /dev/null +++ b/Assets/Scripts/Framework/LogKit/FileLogger.cs @@ -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; + + /// + /// 开始打印 + /// + 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"); + } + + /// + /// 新建文件 + /// + 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; + } + } +} \ No newline at end of file diff --git a/Assets/Scripts/Framework/LogKit/FileLogger.cs.meta b/Assets/Scripts/Framework/LogKit/FileLogger.cs.meta new file mode 100644 index 000000000..b2e55a25c --- /dev/null +++ b/Assets/Scripts/Framework/LogKit/FileLogger.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 7a2dc29290b545c1974b6473534df853 +timeCreated: 1741445068 \ No newline at end of file diff --git a/Assets/Scripts/Framework/LogKit/LogKit.cs b/Assets/Scripts/Framework/LogKit/LogKit.cs new file mode 100644 index 000000000..2fb63c542 --- /dev/null +++ b/Assets/Scripts/Framework/LogKit/LogKit.cs @@ -0,0 +1,57 @@ +using System; +using UnityEngine; + +namespace AibisDream.Kit +{ + internal class LogKit : MonoBehaviour + { + /// + /// 文件名称格式 + /// 请注意,不要删除时间格式,否则会造成保存不成功 + /// + public static string LogFileName => "Log{0:_yyyy_MM_dd}.txt"; + + /// + /// 日志文件路径 + /// + public static string LogPath => Application.streamingAssetsPath + "/LogFile"; + + /// + /// 日志保存最近几天的内容 + /// + public const int SaveDays = 30; + + /// + /// 每一行的打印内容 + /// + 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; + } + } +} \ No newline at end of file diff --git a/Assets/Scripts/Framework/LogKit/LogKit.cs.meta b/Assets/Scripts/Framework/LogKit/LogKit.cs.meta new file mode 100644 index 000000000..09cb677d3 --- /dev/null +++ b/Assets/Scripts/Framework/LogKit/LogKit.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 647335bd723dab94ea97b993d03af61e +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: