一克泥在线服务
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user