一克泥在线服务
This commit is contained in:
130
Assets/Scripts/Online/Util/EditorFileLogger.cs
Normal file
130
Assets/Scripts/Online/Util/EditorFileLogger.cs
Normal file
@@ -0,0 +1,130 @@
|
||||
namespace IchniOnline.Online.Util
|
||||
{
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
using UnityEditor;
|
||||
using UnityEngine;
|
||||
|
||||
[InitializeOnLoad]
|
||||
public static class EditorFileLogger
|
||||
{
|
||||
private static readonly object LockObj = new();
|
||||
private static StreamWriter writer;
|
||||
|
||||
static EditorFileLogger()
|
||||
{
|
||||
try
|
||||
{
|
||||
string logDir = Path.Combine(
|
||||
Environment.GetFolderPath(Environment.SpecialFolder.Desktop),
|
||||
"UnityEditorLogs");
|
||||
|
||||
Directory.CreateDirectory(logDir);
|
||||
|
||||
string logFile = Path.Combine(
|
||||
logDir,
|
||||
$"Editor_{DateTime.Now:yyyyMMdd_HHmmss}.log");
|
||||
|
||||
writer = new StreamWriter(
|
||||
new FileStream(
|
||||
logFile,
|
||||
FileMode.Create,
|
||||
FileAccess.Write,
|
||||
FileShare.ReadWrite,
|
||||
4096,
|
||||
FileOptions.WriteThrough),
|
||||
Encoding.UTF8);
|
||||
|
||||
writer.AutoFlush = true;
|
||||
|
||||
Application.logMessageReceivedThreaded += OnLog;
|
||||
|
||||
EditorApplication.quitting += Shutdown;
|
||||
EditorApplication.playModeStateChanged += OnPlayModeStateChanged;
|
||||
|
||||
WriteLine("====================================");
|
||||
WriteLine($"Unity Version : {Application.unityVersion}");
|
||||
WriteLine($"Project : {Application.productName}");
|
||||
WriteLine($"Started : {DateTime.Now:yyyy-MM-dd HH:mm:ss.fff}");
|
||||
WriteLine("====================================");
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Debug.LogException(e);
|
||||
}
|
||||
}
|
||||
|
||||
private static void OnPlayModeStateChanged(PlayModeStateChange state)
|
||||
{
|
||||
switch (state)
|
||||
{
|
||||
case PlayModeStateChange.ExitingEditMode:
|
||||
WriteLine("[EDITOR] >>> Preparing To Enter Play Mode");
|
||||
break;
|
||||
|
||||
case PlayModeStateChange.EnteredPlayMode:
|
||||
WriteLine("[EDITOR] >>> Entered Play Mode");
|
||||
break;
|
||||
|
||||
case PlayModeStateChange.ExitingPlayMode:
|
||||
WriteLine("[EDITOR] <<< Exiting Play Mode");
|
||||
break;
|
||||
|
||||
case PlayModeStateChange.EnteredEditMode:
|
||||
WriteLine("[EDITOR] <<< Returned To Edit Mode");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private static void OnLog(
|
||||
string condition,
|
||||
string stackTrace,
|
||||
LogType type)
|
||||
{
|
||||
lock (LockObj)
|
||||
{
|
||||
if (writer == null)
|
||||
return;
|
||||
|
||||
writer.WriteLine(
|
||||
$"[{DateTime.Now:yyyy-MM-dd HH:mm:ss.fff}] [{type}] {condition}");
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(stackTrace))
|
||||
{
|
||||
writer.WriteLine(stackTrace);
|
||||
}
|
||||
|
||||
writer.Flush();
|
||||
}
|
||||
}
|
||||
|
||||
private static void WriteLine(string message)
|
||||
{
|
||||
lock (LockObj)
|
||||
{
|
||||
if (writer == null)
|
||||
return;
|
||||
|
||||
writer.WriteLine(
|
||||
$"[{DateTime.Now:yyyy-MM-dd HH:mm:ss.fff}] {message}");
|
||||
|
||||
writer.Flush();
|
||||
}
|
||||
}
|
||||
|
||||
private static void Shutdown()
|
||||
{
|
||||
EditorApplication.playModeStateChanged -= OnPlayModeStateChanged;
|
||||
EditorApplication.quitting -= Shutdown;
|
||||
Application.logMessageReceivedThreaded -= OnLog;
|
||||
|
||||
lock (LockObj)
|
||||
{
|
||||
writer?.Flush();
|
||||
writer?.Dispose();
|
||||
writer = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
3
Assets/Scripts/Online/Util/EditorFileLogger.cs.meta
Normal file
3
Assets/Scripts/Online/Util/EditorFileLogger.cs.meta
Normal file
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 2169063fd004462880557c15613be8f9
|
||||
timeCreated: 1781771972
|
||||
141
Assets/Scripts/Online/Util/PresisentFileLogger.cs
Normal file
141
Assets/Scripts/Online/Util/PresisentFileLogger.cs
Normal file
@@ -0,0 +1,141 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
using UnityEngine;
|
||||
namespace IchniOnline.Online.Util
|
||||
{
|
||||
public class PersistentFileLogger : MonoBehaviour
|
||||
{
|
||||
private static StreamWriter _writer;
|
||||
private static readonly object LockObj = new object();
|
||||
|
||||
private string _logFilePath;
|
||||
|
||||
[Header("日志文件夹名称")]
|
||||
public string logFolderName = "Logs";
|
||||
|
||||
[Header("启动时打印日志路径")]
|
||||
public bool printLogPath = true;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
DontDestroyOnLoad(gameObject);
|
||||
|
||||
InitializeLogger();
|
||||
}
|
||||
|
||||
private void InitializeLogger()
|
||||
{
|
||||
if (_writer != null)
|
||||
return;
|
||||
|
||||
string logDir = Path.Combine(
|
||||
Application.persistentDataPath,
|
||||
logFolderName);
|
||||
|
||||
Directory.CreateDirectory(logDir);
|
||||
|
||||
string fileName =
|
||||
$"log_{DateTime.Now:yyyyMMdd_HHmmss}.txt";
|
||||
|
||||
_logFilePath = Path.Combine(logDir, fileName);
|
||||
|
||||
_writer = new StreamWriter(
|
||||
new FileStream(
|
||||
_logFilePath,
|
||||
FileMode.Create,
|
||||
FileAccess.Write,
|
||||
FileShare.ReadWrite,
|
||||
4096,
|
||||
FileOptions.WriteThrough),
|
||||
Encoding.UTF8);
|
||||
|
||||
_writer.AutoFlush = true;
|
||||
|
||||
Application.logMessageReceivedThreaded += OnLogReceived;
|
||||
|
||||
WriteRaw("==================================================");
|
||||
WriteRaw($"Unity Version : {Application.unityVersion}");
|
||||
WriteRaw($"Platform : {Application.platform}");
|
||||
WriteRaw($"Start Time : {DateTime.Now:yyyy-MM-dd HH:mm:ss.fff}");
|
||||
WriteRaw($"Log File : {_logFilePath}");
|
||||
WriteRaw("==================================================");
|
||||
|
||||
if (printLogPath)
|
||||
{
|
||||
Debug.Log($"Log file: {_logFilePath}");
|
||||
}
|
||||
}
|
||||
|
||||
private void OnDestroy()
|
||||
{
|
||||
ShutdownLogger();
|
||||
}
|
||||
|
||||
private void OnApplicationQuit()
|
||||
{
|
||||
ShutdownLogger();
|
||||
}
|
||||
|
||||
private void ShutdownLogger()
|
||||
{
|
||||
Application.logMessageReceivedThreaded -= OnLogReceived;
|
||||
|
||||
lock (LockObj)
|
||||
{
|
||||
if (_writer != null)
|
||||
{
|
||||
WriteRaw("Application Exit");
|
||||
|
||||
_writer.Flush();
|
||||
_writer.Close();
|
||||
_writer.Dispose();
|
||||
|
||||
_writer = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void OnLogReceived(
|
||||
string condition,
|
||||
string stackTrace,
|
||||
LogType type)
|
||||
{
|
||||
lock (LockObj)
|
||||
{
|
||||
if (_writer == null)
|
||||
return;
|
||||
|
||||
string timestamp =
|
||||
DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff");
|
||||
|
||||
_writer.WriteLine(
|
||||
$"[{timestamp}] [{type}] {condition}");
|
||||
|
||||
if (type == LogType.Error ||
|
||||
type == LogType.Exception ||
|
||||
type == LogType.Assert)
|
||||
{
|
||||
if (!string.IsNullOrWhiteSpace(stackTrace))
|
||||
{
|
||||
_writer.WriteLine(stackTrace);
|
||||
}
|
||||
}
|
||||
|
||||
_writer.Flush();
|
||||
}
|
||||
}
|
||||
|
||||
private static void WriteRaw(string message)
|
||||
{
|
||||
lock (LockObj)
|
||||
{
|
||||
if (_writer == null)
|
||||
return;
|
||||
|
||||
_writer.WriteLine(message);
|
||||
_writer.Flush();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
3
Assets/Scripts/Online/Util/PresisentFileLogger.cs.meta
Normal file
3
Assets/Scripts/Online/Util/PresisentFileLogger.cs.meta
Normal file
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 7e726f6c24c3464cbc0ccc66f372c55f
|
||||
timeCreated: 1781771683
|
||||
Reference in New Issue
Block a user