-
Notifications
You must be signed in to change notification settings - Fork 116
feat: Add search_console_logs tool for Unity log searching #50
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
Changes from all commits
65084f3
30d64e9
90ca612
9c2d0c1
92b8387
2d59af9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -121,3 +121,6 @@ log.txt | |
log.txt.meta | ||
Server~/build/* | ||
Server~/node_modules/* | ||
|
||
# Claude settings | ||
.claude/settings.local.json |
Large diffs are not rendered by default.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
namespace McpUnity.Services | ||
{ | ||
/// <summary> | ||
/// Unity LogEntry mode flags constants | ||
/// Based on Unity's internal LogMessageFlags from UnityCsReference | ||
/// https://github.com/Unity-Technologies/UnityCsReference/blob/master/Editor/Mono/LogEntries.bindings.cs | ||
/// </summary> | ||
public static class LogEntryModeFlags | ||
{ | ||
// Basic log type flags (bits 0-4) | ||
public const int kModeError = 1 << 0; // 1 (0x1) | ||
public const int kModeAssert = 1 << 1; // 2 (0x2) | ||
public const int kModeLog = 1 << 2; // 4 (0x4) | ||
public const int kModeWarning = 1 << 3; // 8 (0x8) | ||
public const int kModeException = 1 << 4; // 16 (0x10) | ||
|
||
// Scripting related flags (bits 8-12) | ||
public const int kScriptingError = 1 << 8; // 256 (0x100) | ||
public const int kScriptingWarning = 1 << 9; // 512 (0x200) | ||
public const int kScriptingLog = 1 << 10; // 1024 (0x400) | ||
public const int kScriptCompileError = 1 << 11; // 2048 (0x800) | ||
public const int kScriptCompileWarning = 1 << 12; // 4096 (0x1000) | ||
|
||
// Observed composite values from debugging | ||
// These include additional undocumented high bits | ||
public const int ObservedCompilerWarning = 266240; // 0x41000 (bits: 18, 10) | ||
public const int ObservedCompilerError = 272384; // 0x42800 (bits: 18, 14, 10) | ||
public const int ObservedShaderError = 262212; // 0x40044 (bits: 18, 6, 2) | ||
public const int ObservedRuntimeWarning = 8405504; // 0x804200 (bits: 23, 18, 9) | ||
public const int ObservedRuntimeError = 8405248; // 0x804100 (bits: 23, 18, 8) | ||
|
||
/// <summary> | ||
/// Determine log type from mode flags | ||
/// </summary> | ||
public static string GetLogTypeFromMode(int mode) | ||
{ | ||
// Check for observed compiler/shader message patterns first | ||
if (mode == ObservedCompilerError) return "Error"; | ||
if (mode == ObservedCompilerWarning) return "Warning"; | ||
if (mode == ObservedShaderError) return "Error"; | ||
|
||
// Check for script compile errors/warnings | ||
if ((mode & kScriptCompileError) != 0) return "Error"; | ||
if ((mode & kScriptCompileWarning) != 0) return "Warning"; | ||
|
||
// Check for scripting errors/warnings | ||
if ((mode & kScriptingError) != 0) return "Error"; | ||
if ((mode & kScriptingWarning) != 0) return "Warning"; | ||
|
||
// Then check standard flags | ||
if ((mode & kModeError) != 0) return "Error"; | ||
if ((mode & kModeAssert) != 0) return "Assert"; | ||
if ((mode & kModeException) != 0) return "Exception"; | ||
if ((mode & kModeWarning) != 0) return "Warning"; | ||
if ((mode & kModeLog) != 0) return "Log"; | ||
|
||
return "Log"; // Default to Log instead of Unknown | ||
} | ||
Comment on lines
+35
to
+58
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. π οΈ Refactor suggestion
Returning a string sacrifices compile-time safety and invites typos downstream. Unity already defines -public static string GetLogTypeFromMode(int mode)
+public static UnityEngine.LogType GetLogTypeFromMode(int mode) Adjust the
π€ Prompt for AI Agents
|
||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
# Unity 6 Internal API Reference | ||
|
||
## LogEntry Structure | ||
|
||
Unity's internal `LogEntry` structure contains the following fields: | ||
|
||
### Fields | ||
| Field Name | Type | Description | | ||
|------------|------|-------------| | ||
| message | String | Full message including stack trace | | ||
| file | String | Source file path | | ||
| line | Int32 | Line number | | ||
| column | Int32 | Column number (-1 if not available) | | ||
| mode | Int32 | Log type flags (see Mode Flags section) | | ||
| instanceID | Int32 | Instance ID of the object | | ||
| identifier | Int32 | Unique identifier | | ||
| globalLineIndex | Int32 | Global line index in console | | ||
| callstackTextStartUTF8 | Int32 | UTF-8 byte position where stack trace starts | | ||
| callstackTextStartUTF16 | Int32 | UTF-16 character position where stack trace starts | | ||
|
||
## Mode Flags | ||
|
||
### Standard Unity Log Flags (from UnityCsReference) | ||
```csharp | ||
// Basic log types | ||
const int kModeError = 1 << 0; // 1 (0x1) | ||
const int kModeAssert = 1 << 1; // 2 (0x2) | ||
const int kModeLog = 1 << 2; // 4 (0x4) | ||
const int kModeWarning = 1 << 3; // 8 (0x8) | ||
const int kModeException = 1 << 4; // 16 (0x10) | ||
|
||
// Scripting related flags | ||
const int kScriptingError = 1 << 8; // 256 (0x100) | ||
const int kScriptingWarning = 1 << 9; // 512 (0x200) | ||
const int kScriptingLog = 1 << 10; // 1024 (0x400) | ||
const int kScriptCompileError = 1 << 11; // 2048 (0x800) | ||
const int kScriptCompileWarning = 1 << 12; // 4096 (0x1000) | ||
``` | ||
|
||
### Observed Compiler/Shader Message Values (from debugging) | ||
```csharp | ||
// These are composite values with multiple flags set | ||
const int ObservedCompilerWarning = 266240; // 0x41000 (bits: 18, 10) | ||
const int ObservedCompilerError = 272384; // 0x42800 (bits: 18, 14, 10) | ||
const int ObservedShaderError = 262212; // 0x40044 (bits: 18, 6, 2) | ||
``` | ||
|
||
Note: The observed values include additional high bits (2, 6, 10, 14, 18) beyond the documented flags, suggesting Unity may be using undocumented internal flags. | ||
|
||
## Important Findings | ||
|
||
1. **Compiler messages use special mode values** | ||
- C# compilation errors (e.g., `error CS0103`) have mode = 272384 | ||
- C# compilation warnings (e.g., `warning CS0414`) have mode = 266240 | ||
- These do NOT use the standard error/warning flags! | ||
|
||
2. **Shader errors** | ||
- Shader compilation errors appear to use standard Log mode (4) | ||
- Need message content analysis to properly classify | ||
|
||
3. **Stack trace separation** | ||
- Use `callstackTextStartUTF16` for C# strings (preferred) | ||
- Fallback to `callstackTextStartUTF8` if needed | ||
- Unity provides exact position where stack trace begins | ||
|
||
## Usage Notes | ||
|
||
- Always check special compiler flags before standard flags | ||
- Message content analysis may still be needed for some error types | ||
- The mode field alone is not sufficient for all classification needs | ||
|
||
## Example Mode Analysis | ||
|
||
``` | ||
Warning CS0414: mode = 266240 = 0x41000 | ||
Binary: 1000001000000000000 | ||
Bits set: 18, 10 | ||
|
||
Error CS0103: mode = 272384 = 0x42800 | ||
Binary: 1000010100000000000 | ||
Bits set: 18, 14, 10 | ||
``` | ||
|
||
The high bits (10, 14, 18) appear to indicate compiler-related messages. |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -143,6 +143,30 @@ private void DrawServerTab() | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
settings.SaveSettings(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
EditorGUILayout.Space(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// Console log service selection | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
ConsoleLogServiceType newConsoleLogService = (ConsoleLogServiceType)EditorGUILayout.EnumPopup( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
new GUIContent("Console Log Service", "Select console log service implementation. EventBased is safe but may miss some logs. Unity6Enhanced uses internal APIs for better reliability but requires Unity 6+. Server restart required when changed."), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
settings.ConsoleLogService); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if (newConsoleLogService != settings.ConsoleLogService) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
settings.ConsoleLogService = newConsoleLogService; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
settings.SaveSettings(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// Show warning for Unity6Enhanced | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if (newConsoleLogService == ConsoleLogServiceType.Unity6Enhanced) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
#if UNITY_6000_0_OR_NEWER | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
EditorUtility.DisplayDialog("Console Log Service Changed", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
"Unity 6 enhanced console log service selected. This uses internal Unity APIs for better reliability but may break in future Unity versions.\n\nRestart the MCP server for changes to take effect.", "OK"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
#else | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
EditorUtility.DisplayDialog("Console Log Service Warning", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
$"Unity 6 enhanced console log service selected but current Unity version is {Application.unityVersion}. The service will fall back to event-based implementation.\n\nRestart the MCP server for changes to take effect.", "OK"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
#endif | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+146
to
+169
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. π οΈ Refactor suggestion Auto-restart the server after changing the log service for consistency. When the user switches the console-log service, the dialog warns that a restart is required, yet the code does not replicate the automatic restart behaviour you already implement for the port change (lines 101-105). Consider restarting the server immediately after persisting the new value: settings.ConsoleLogService = newConsoleLogService;
settings.SaveSettings();
+// Ensure the active server uses the new implementation
+if (mcpUnityServer.IsListening)
+{
+ mcpUnityServer.StopServer();
+ mcpUnityServer.StartServer();
+} π Committable suggestion
Suggested change
π€ Prompt for AI Agents
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
EditorGUILayout.Space(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// Server control buttons | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
π οΈ Refactor suggestion
Consider using a
[Flags]
enum instead of rawconst int
valuesUsing an enum provides type-safety, self-documentation, and built-in bitwise helpers while still compiling to the same constants. Example:
This small refactor eliminates magic numbers elsewhere and prevents accidental mixing with unrelated bit-fields.
π Committable suggestion
π€ Prompt for AI Agents