Skip to content

fix: Map 'info' log type to Unity's 'Log' type for console log retrieval #40

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion Editor/Services/ConsoleLogsService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@ namespace McpUnity.Services
/// </summary>
public class ConsoleLogsService : IConsoleLogsService
{
// Static mapping for MCP log types to Unity log types
// Some MCP types map to multiple Unity types (e.g., "error" includes Error, Exception and Assert)
private static readonly Dictionary<string, HashSet<string>> LogTypeMapping = new Dictionary<string, HashSet<string>>(StringComparer.OrdinalIgnoreCase)
{
{ "info", new HashSet<string>(StringComparer.OrdinalIgnoreCase) { "Log" } },
{ "error", new HashSet<string>(StringComparer.OrdinalIgnoreCase) { "Error", "Exception", "Assert" } }
};

// Structure to store log information
private class LogEntry
{
Expand Down Expand Up @@ -76,11 +84,26 @@ public JArray GetAllLogsAsJson(string logType = "")
JArray logsArray = new JArray();
bool filter = !string.IsNullOrEmpty(logType);

// Map MCP log types to Unity log types outside the loop for better performance
HashSet<string> unityLogTypes = null;
if (filter)
{
if (LogTypeMapping.TryGetValue(logType, out var mapped))
{
unityLogTypes = mapped;
}
else
{
// If no mapping exists, create a set with the original type for case-insensitive comparison
unityLogTypes = new HashSet<string>(StringComparer.OrdinalIgnoreCase) { logType };
}
}

lock (_logEntries)
{
foreach (var entry in _logEntries)
{
if (filter && !entry.Type.ToString().Equals(logType, System.StringComparison.OrdinalIgnoreCase))
if (filter && !unityLogTypes.Contains(entry.Type.ToString()))
continue;
logsArray.Add(new JObject
{
Expand Down