Merged
fix(cpp-sdk): Include log level name in MCP_LOG output prefix. #241
yechenzhenAdd on Jun 17
fix(cpp-sdk): Include log level name in MCP_LOG output prefix. #241
Merged
Total: 4 files changed+45-2
| @@ -37,6 +37,16 @@ MCP_LOG(MCP_LOG_LEVEL_INFO, std::string("server started on port ") + std::to_str | |||
| 37 | 37 | ||
| 38 | `SetLogLevel` 设置全局阈值;**低于阈值的日志在 `MCP_LOG` 入口即被过滤**,自定义 callback 也会收到已过滤后的消息。 | 38 | `SetLogLevel` 设置全局阈值;**低于阈值的日志在 `MCP_LOG` 入口即被过滤**,自定义 callback 也会收到已过滤后的消息。 |
| 39 | 39 | ||
| 40 | +### 输出格式 | ||
| 41 | + | ||
| 42 | +通过 `MCP_LOG` 输出的每行前缀格式为: | ||
| 43 | + | ||
| 44 | +```text | ||
| 45 | +[YYYY-MM-DD HH:MM:SS.mmm] [tid] [LEVEL] file.cpp::Function:[line] message | ||
| 46 | +``` | ||
| 47 | + | ||
| 48 | +其中 `LEVEL` 为 `DEBUG` / `INFO` / `WARN` / `ERROR` / `FATAL` 之一。级别名由 SDK 自动写入;`McpLogCallback` 的第一个参数仍为枚举值 `MCP_LOG_LEVEL`。 | ||
| 49 | + | ||
| 40 | ### 默认输出:stdout | 50 | ### 默认输出:stdout |
| 41 | 51 | ||
| 42 | 默认实现 `McpPrintfImpl` 写入 **stdout**(`printf`)。 | 52 | 默认实现 `McpPrintfImpl` 写入 **stdout**(`printf`)。 |
| @@ -39,6 +39,8 @@ int32_t SetLogCallback(McpLogCallback logCallback); | |||
| 39 | int32_t SetLogLevel(MCP_LOG_LEVEL logLevel); | 39 | int32_t SetLogLevel(MCP_LOG_LEVEL logLevel); |
| 40 | MCP_LOG_LEVEL GetLogLevel(void); | 40 | MCP_LOG_LEVEL GetLogLevel(void); |
| 41 | 41 | ||
| 42 | +const char* GetLogLevelName(MCP_LOG_LEVEL level); | ||
| 43 | + | ||
| 42 | // Helper function to get current timestamp | 44 | // Helper function to get current timestamp |
| 43 | static inline void GetCurrentTimeStamp(std::string& timestamp) | 45 | static inline void GetCurrentTimeStamp(std::string& timestamp) |
| 44 | { | 46 | { |
| @@ -79,8 +81,9 @@ inline void LogInternal(MCP_LOG_LEVEL level, const char* file, const char* func, | |||
| 79 | const char* filename = strrchr(file, '/'); | 81 | const char* filename = strrchr(file, '/'); |
| 80 | filename = filename ? filename + 1 : file; | 82 | filename = filename ? filename + 1 : file; |
| 81 | 83 | ||
| 82 | - std::string prefix = "[" + timestamp + "] [" + std::to_string(syscall(SYS_gettid)) + "] " + | 84 | + std::string prefix = "[" + timestamp + "] [" + std::to_string(syscall(SYS_gettid)) + "] [" + |
| 83 | - std::string(filename) + "::" + std::string(func) + ":[" + std::to_string(line) + "] "; | 85 | + std::string(GetLogLevelName(level)) + "] " + std::string(filename) + "::" + |
| 86 | + std::string(func) + ":[" + std::to_string(line) + "] "; | ||
| 84 | 87 | ||
| 85 | static_assert(sizeof...(Args) == 0, | 88 | static_assert(sizeof...(Args) == 0, |
| 86 | "MCP_LOG expects C++-style string composition"); | 89 | "MCP_LOG expects C++-style string composition"); |
| @@ -27,6 +27,24 @@ MCP_LOG_LEVEL GetLogLevel(void) | |||
| 27 | return g_logLevel; | 27 | return g_logLevel; |
| 28 | } | 28 | } |
| 29 | 29 | ||
| 30 | +const char* GetLogLevelName(MCP_LOG_LEVEL logLevel) | ||
| 31 | +{ | ||
| 32 | + switch (logLevel) { | ||
| 33 | + case MCP_LOG_LEVEL_DEBUG: | ||
| 34 | + return "DEBUG"; | ||
| 35 | + case MCP_LOG_LEVEL_INFO: | ||
| 36 | + return "INFO"; | ||
| 37 | + case MCP_LOG_LEVEL_WARN: | ||
| 38 | + return "WARN"; | ||
| 39 | + case MCP_LOG_LEVEL_ERROR: | ||
| 40 | + return "ERROR"; | ||
| 41 | + case MCP_LOG_LEVEL_FATAL: | ||
| 42 | + return "FATAL"; | ||
| 43 | + default: | ||
| 44 | + return "UNKNOWN"; | ||
| 45 | + } | ||
| 46 | +} | ||
| 47 | + | ||
| 30 | void McpPrintfImpl(MCP_LOG_LEVEL logLevel, std::string message) | 48 | void McpPrintfImpl(MCP_LOG_LEVEL logLevel, std::string message) |
| 31 | { | 49 | { |
| 32 | if (logLevel < GetLogLevel()) { | 50 | if (logLevel < GetLogLevel()) { |
| @@ -126,6 +126,17 @@ TEST_F(LogTestFixture, MacroLogOutput) { | |||
| 126 | std::string output = testing::internal::GetCapturedStdout(); | 126 | std::string output = testing::internal::GetCapturedStdout(); |
| 127 | 127 | ||
| 128 | EXPECT_TRUE(output.find("Test macro log with parameters") != std::string::npos); | 128 | EXPECT_TRUE(output.find("Test macro log with parameters") != std::string::npos); |
| 129 | + EXPECT_TRUE(output.find("[INFO]") != std::string::npos); | ||
| 130 | +} | ||
| 131 | + | ||
| 132 | +TEST_F(LogTestFixture, GetLogLevelNameMapsKnownLevels) | ||
| 133 | +{ | ||
| 134 | + EXPECT_STREQ("DEBUG", GetLogLevelName(MCP_LOG_LEVEL_DEBUG)); | ||
| 135 | + EXPECT_STREQ("INFO", GetLogLevelName(MCP_LOG_LEVEL_INFO)); | ||
| 136 | + EXPECT_STREQ("WARN", GetLogLevelName(MCP_LOG_LEVEL_WARN)); | ||
| 137 | + EXPECT_STREQ("ERROR", GetLogLevelName(MCP_LOG_LEVEL_ERROR)); | ||
| 138 | + EXPECT_STREQ("FATAL", GetLogLevelName(MCP_LOG_LEVEL_FATAL)); | ||
| 139 | + EXPECT_STREQ("UNKNOWN", GetLogLevelName(static_cast<MCP_LOG_LEVEL>(0))); | ||
| 129 | } | 140 | } |
| 130 | 141 | ||
| 131 | TEST_F(LogTestFixture, CallbackSwitch) { | 142 | TEST_F(LogTestFixture, CallbackSwitch) { |
| @@ -180,4 +191,5 @@ TEST_F(LogTestFixture, LogInternalFiltersBeforeCustomCallback) | |||
| 180 | MCP_LOG(MCP_LOG_LEVEL_ERROR, std::string("delivered to callback")); | 191 | MCP_LOG(MCP_LOG_LEVEL_ERROR, std::string("delivered to callback")); |
| 181 | EXPECT_TRUE(test_callback_invoked); | 192 | EXPECT_TRUE(test_callback_invoked); |
| 182 | EXPECT_TRUE(captured_message.find("delivered to callback") != std::string::npos); | 193 | EXPECT_TRUE(captured_message.find("delivered to callback") != std::string::npos); |
| 194 | + EXPECT_TRUE(captured_message.find("[ERROR]") != std::string::npos); | ||
| 183 | } | 195 | } |