forked from IvanMurzak/Unity-MCP
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUnityLogCollector.cs
More file actions
129 lines (106 loc) · 4.41 KB
/
UnityLogCollector.cs
File metadata and controls
129 lines (106 loc) · 4.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/*
┌──────────────────────────────────────────────────────────────────┐
│ Author: Ivan Murzak (https://github.com/IvanMurzak) │
│ Repository: GitHub (https://github.com/IvanMurzak/Unity-MCP) │
│ Copyright (c) 2025 Ivan Murzak │
│ Licensed under the Apache License, Version 2.0. │
│ See the LICENSE file in the project root for more information. │
└──────────────────────────────────────────────────────────────────┘
*/
#nullable enable
using System;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using com.IvanMurzak.McpPlugin.Common;
using com.IvanMurzak.ReflectorNet;
using com.IvanMurzak.ReflectorNet.Utils;
using UnityEngine;
namespace com.IvanMurzak.Unity.MCP
{
/// <summary>
/// Collects Unity log messages and manages saving/loading them to/from a cache file.
/// </summary>
public class UnityLogCollector : IDisposable
{
private static readonly Regex RichTextRegex = new Regex(@"</?(b|i|size|color|material|quad|a)\b[^>]*>", RegexOptions.Compiled | RegexOptions.IgnoreCase);
readonly ILogStorage _logStorage;
readonly ThreadSafeBool _isDisposed = new(false);
public UnityLogCollector(ILogStorage logStorage)
{
if (!MainThread.Instance.IsMainThread)
throw new Exception($"{GetType().GetTypeShortName()} constructor must be initialized on the main thread.");
_logStorage = logStorage ?? throw new ArgumentNullException(nameof(logStorage));
Application.logMessageReceivedThreaded += OnLogMessageReceived;
}
public void Clear()
{
if (_isDisposed.Value)
return;
_logStorage.Clear();
}
/// <summary>
/// Synchronously saves all current log entries to the cache file.
/// </summary>
public void Save()
{
if (_isDisposed.Value)
return;
_logStorage.Flush();
}
/// <summary>
/// Asynchronously saves all current log entries to the cache file.
/// </summary>
/// <returns>A task that completes when the save operation is finished.</returns>
public Task SaveAsync()
{
if (_isDisposed.Value)
return Task.CompletedTask;
return _logStorage.FlushAsync();
}
public Task<LogEntry[]> QueryAsync(
int maxEntries = 100,
LogType? logTypeFilter = null,
bool includeStackTrace = false,
int lastMinutes = 0)
{
return _logStorage.QueryAsync(maxEntries, logTypeFilter, includeStackTrace, lastMinutes);
}
public LogEntry[] Query(
int maxEntries = 100,
LogType? logTypeFilter = null,
bool includeStackTrace = false,
int lastMinutes = 0)
{
return _logStorage.Query(maxEntries, logTypeFilter, includeStackTrace, lastMinutes);
}
void OnLogMessageReceived(string message, string stackTrace, LogType type)
{
try
{
// Strip rich text tags
var cleanMessage = RichTextRegex.Replace(message, string.Empty);
// Filter out recursive logs from McpToolManager to prevent "insane slashes" and huge logs
// if (cleanMessage.Contains("[AI] McpToolManager Success Response to AI"))
// return;
var logEntry = new LogEntry(
message: cleanMessage,
stackTrace: stackTrace,
logType: type);
_logStorage.Append(logEntry);
}
catch
{
// Ignore logging errors to prevent recursive issues
}
}
public void Dispose()
{
if (!_isDisposed.TrySetTrue())
return; // already disposed
Application.logMessageReceivedThreaded -= OnLogMessageReceived;
_logStorage.Dispose();
GC.SuppressFinalize(this);
}
~UnityLogCollector() => Dispose();
}
}