微调
This commit is contained in:
64
Assets/Scripts/Utilities/BuildHangTracer.cs
Normal file
64
Assets/Scripts/Utilities/BuildHangTracer.cs
Normal file
@@ -0,0 +1,64 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Ichni.Diagnostics
|
||||
{
|
||||
/// <summary>
|
||||
/// 专用于 Standalone Build 的实时落盘诊断日志工具。
|
||||
/// 绕过 Unity 默认日志缓冲区,直接将日志强行追加写入独立文本文件,
|
||||
/// 确保即使游戏卡死(未响应),卡死前最后一条日志也能 100% 写入磁盘。
|
||||
/// </summary>
|
||||
public static class BuildHangTracer
|
||||
{
|
||||
private static string _logFilePath;
|
||||
|
||||
public static string LogFilePath
|
||||
{
|
||||
get
|
||||
{
|
||||
if (string.IsNullOrEmpty(_logFilePath))
|
||||
{
|
||||
_logFilePath = Path.Combine(Application.persistentDataPath, "build_hang_trace.txt");
|
||||
}
|
||||
return _logFilePath;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 强行立即追加一行日志到磁盘独立文件
|
||||
/// </summary>
|
||||
public static void Log(string tag, string message)
|
||||
{
|
||||
try
|
||||
{
|
||||
string timestamp = DateTime.Now.ToString("HH:mm:ss.fff");
|
||||
string logLine = $"[{timestamp}] [{tag}] {message}{Environment.NewLine}";
|
||||
File.AppendAllText(LogFilePath, logLine);
|
||||
}
|
||||
catch
|
||||
{
|
||||
// 静默忽略日志写入本身的异常
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 清空之前的日志文件并记录初始化标志
|
||||
/// </summary>
|
||||
public static void ClearLog()
|
||||
{
|
||||
try
|
||||
{
|
||||
if (File.Exists(LogFilePath))
|
||||
{
|
||||
File.Delete(LogFilePath);
|
||||
}
|
||||
Log("INIT", $"--- 诊断日志已重置。日志存储路径:{LogFilePath} ---");
|
||||
}
|
||||
catch
|
||||
{
|
||||
// 静默忽略
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
2
Assets/Scripts/Utilities/BuildHangTracer.cs.meta
Normal file
2
Assets/Scripts/Utilities/BuildHangTracer.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: bebb1ee94395f2648b7cf5892a16c9fd
|
||||
Reference in New Issue
Block a user