65 lines
1.9 KiB
C#
65 lines
1.9 KiB
C#
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
|
|
{
|
|
// 静默忽略
|
|
}
|
|
}
|
|
}
|
|
}
|