Skip to content

Commit 1317f19

Browse files
committed
update console log file read with async logic
1 parent 01cdbfc commit 1317f19

File tree

3 files changed

+34
-21
lines changed

3 files changed

+34
-21
lines changed

Unity-MCP-Plugin/Assets/root/Runtime/Unity/Logs/LogCache.cs

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public static class LogCache
3434

3535
static string _cacheFileName = "editor-logs.txt";
3636
static string _cacheFile = $"{Path.Combine(_cacheFilePath, _cacheFileName)}";
37-
static readonly object _fileLock = new();
37+
static readonly SemaphoreSlim _fileLock = new(1, 1);
3838
static bool _initialized = false;
3939

4040
public static void Initialize()
@@ -51,39 +51,52 @@ public static void Initialize()
5151
_initialized = true;
5252
}
5353

54-
public static void HandleLogCache()
54+
public static async void HandleLogCache()
5555
{
5656
if (LogUtils.LogEntries > 0)
5757
{
5858
var logs = LogUtils.GetAllLogs();
59-
CacheLogEntries(logs);
59+
await CacheLogEntriesAsync(logs);
6060
}
6161
}
6262

63-
public static void CacheLogEntries(LogEntry[] entries)
63+
public static async Task CacheLogEntriesAsync(LogEntry[] entries)
6464
{
65-
lock (_fileLock)
65+
await _fileLock.WaitAsync();
66+
try
6667
{
6768
string data = JsonUtility.ToJson(new LogWrapper { entries = entries });
6869
Directory.CreateDirectory(_cacheFilePath);
6970
// Atomic File Write
70-
File.WriteAllText(_cacheFile + ".tmp", data);
71+
await File.WriteAllTextAsync(_cacheFile + ".tmp", data);
7172
if (File.Exists(_cacheFile))
7273
File.Delete(_cacheFile);
7374
File.Move(_cacheFile + ".tmp", _cacheFile);
7475
}
76+
finally
77+
{
78+
_fileLock.Release();
79+
}
7580
}
76-
public static ConcurrentQueue<LogEntry> GetCachedLogEntries()
81+
public static async Task<ConcurrentQueue<LogEntry>> GetCachedLogEntriesAsync()
7782
{
78-
lock (_fileLock)
83+
await _fileLock.WaitAsync();
84+
try
7985
{
8086
if (!File.Exists(_cacheFile))
8187
{
82-
return new();
88+
return new ConcurrentQueue<LogEntry>();
8389
}
84-
string json = File.ReadAllText(_cacheFile);
85-
LogWrapper wrapper = JsonUtility.FromJson<LogWrapper>(json);
86-
return new ConcurrentQueue<LogEntry>(wrapper.entries);
90+
string json = await File.ReadAllTextAsync(_cacheFile);
91+
return await Task.Run(() =>
92+
{
93+
LogWrapper wrapper = JsonUtility.FromJson<LogWrapper>(json);
94+
return new ConcurrentQueue<LogEntry>(wrapper.entries);
95+
});
96+
}
97+
finally
98+
{
99+
_fileLock.Release();
87100
}
88101
}
89102
}

Unity-MCP-Plugin/Assets/root/Runtime/Unity/Logs/LogUtils.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,18 +55,18 @@ static LogUtils()
5555

5656
public static void EnsureSubscribed()
5757
{
58-
MainThread.Instance.RunAsync(() =>
58+
MainThread.Instance.RunAsync(async () =>
5959
{
6060
lock (_lockObject)
6161
{
6262
if (!_isSubscribed)
6363
{
6464
Application.logMessageReceivedThreaded += OnLogMessageReceived;
6565
LogCache.Initialize();
66-
_logEntries = LogCache.GetCachedLogEntries();
6766
_isSubscribed = true;
6867
}
6968
}
69+
_logEntries = await LogCache.GetCachedLogEntriesAsync();
7070
});
7171
}
7272

Unity-MCP-Plugin/Assets/root/Tests/Editor/Tool/Console/TestToolConsoleIntegration.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -256,15 +256,9 @@ public IEnumerator GetLogs_Validate_LogCount()
256256
[UnityTest]
257257
public IEnumerator GetLogs_Validate_ConsoleLogRetention()
258258
{
259-
// Wait for log collection system to process (EditMode tests can only yield null)
260-
for (int i = 0; i < 30000; i++)
261-
{
262-
yield return null;
263-
}
264259
// This test verifies that logs are being stored and read from the log cache properly.
265260
int testCount = 15;
266261
int startCount = LogUtils.LogEntries;
267-
Assert.AreEqual(startCount, LogCache.GetCachedLogEntries().Count(), "Log entries and Log Cache count should match at the start of this test.");
268262
for (int i = 0; i < testCount; i++)
269263
{
270264
Debug.Log($"Test Log {i + 1}");
@@ -274,8 +268,14 @@ public IEnumerator GetLogs_Validate_ConsoleLogRetention()
274268
{
275269
yield return null;
276270
}
271+
LogUtils.ClearLogs();
272+
Assert.AreEqual(0, LogUtils.LogEntries, "Log entries and Log Cache count should be empty.");
273+
LogUtils.EnsureSubscribed();
274+
for (int i = 0; i < 10000; i++)
275+
{
276+
yield return null;
277+
}
277278
Assert.AreEqual(startCount + testCount, LogUtils.LogEntries, "LogUtils should have new logs in memory.");
278-
Assert.IsTrue(LogUtils.LogEntries == LogCache.GetCachedLogEntries().Count(), "Log entries and Log Cache count should match.");
279279
}
280280
}
281281
}

0 commit comments

Comments
 (0)