#include #include "vde/foundation/vde_log.h" #include "vde/foundation/utf8_path.h" #include #include #include #include #include using namespace vde::foundation; // ═══════════════════════════════════════════════════════════ // D10: vde_log — 结构化日志模块测试 // ═══════════════════════════════════════════════════════════ TEST(VdeLogTest, LevelLabels) { EXPECT_STREQ(log_level_label(LogLevel::Trace), "T"); EXPECT_STREQ(log_level_label(LogLevel::Info), "I"); EXPECT_STREQ(log_level_label(LogLevel::Error), "E"); EXPECT_STREQ(log_level_name(LogLevel::Critical), "CRITICAL"); } TEST(VdeLogTest, LevelFromString) { EXPECT_EQ(log_level_from_string("INFO"), LogLevel::Info); EXPECT_EQ(log_level_from_string("warning"), LogLevel::Warn); EXPECT_EQ(log_level_from_string("ERROR"), LogLevel::Error); EXPECT_EQ(log_level_from_string("nonsense"), LogLevel::Info); } TEST(VdeLogTest, LevelFiltering) { std::ostringstream oss; vde_log::set_output(&oss); vde_log::set_level(LogLevel::Warn); vde_log::info("test", "evt", "should be filtered"); EXPECT_TRUE(oss.str().empty()) << "Info 级别在 Warn 过滤下不应输出"; vde_log::warn("test", "evt", "visible warning"); EXPECT_FALSE(oss.str().empty()); // 恢复默认 vde_log::set_output(nullptr); vde_log::set_level(LogLevel::Info); } TEST(VdeLogTest, StructuredFields) { std::ostringstream oss; vde_log::set_output(&oss); vde_log::set_level(LogLevel::Trace); vde_log::info("brep", "boolean_union", "union completed", vde_log::field("faces_a", 12), vde_log::field("faces_b", 8), vde_log::field("time_ms", 3.5), vde_log::field("ok", true)); std::string out = oss.str(); EXPECT_NE(out.find("boolean_union"), std::string::npos); EXPECT_NE(out.find("faces_a=12"), std::string::npos); EXPECT_NE(out.find("faces_b=8"), std::string::npos); EXPECT_NE(out.find("ok=true"), std::string::npos); vde_log::set_output(nullptr); vde_log::set_level(LogLevel::Info); } TEST(VdeLogTest, SinkCallback) { int count = 0; std::string last_module; vde_log::set_sink([&](const LogMessage& m) { ++count; last_module = m.module; }); vde_log::error("step", "import", "failed", vde_log::field("code", 1301)); EXPECT_EQ(count, 1); EXPECT_EQ(last_module, "step"); vde_log::set_sink(nullptr); } TEST(VdeLogTest, MessageFormat) { LogMessage msg; msg.level = LogLevel::Error; msg.module = "curves"; msg.event = "eval"; msg.message = "boom"; msg.timestamp_us = 1000000; // 1970-01-01 00:00:01 UTC msg.fields = {{"t", "0.5"}}; std::string s = msg.to_string(); EXPECT_NE(s.find("E"), std::string::npos); EXPECT_NE(s.find("curves"), std::string::npos); EXPECT_NE(s.find("eval"), std::string::npos); EXPECT_NE(s.find("boom"), std::string::npos); EXPECT_NE(s.find("t=0.5"), std::string::npos); } TEST(VdeLogTest, LevelOffDisablesAll) { std::ostringstream oss; vde_log::set_output(&oss); vde_log::set_level(LogLevel::Off); vde_log::critical("x", "y", "should be silent"); EXPECT_TRUE(oss.str().empty()); vde_log::set_output(nullptr); vde_log::set_level(LogLevel::Info); } // ═══════════════════════════════════════════════════════════ // Wave21 A54:UTF-8(中文/非 ASCII)日志文件路径 // ═══════════════════════════════════════════════════════════ TEST(VdeLogUtf8Path, SetOutputFileUtf8Path) { const std::string path = std::string(VDE_TEST_TEMP_DIR) + "test_vde_log_日志_①.log"; std::error_code ec; std::filesystem::remove(path_from_utf8(path), ec); vde_log::set_output(nullptr); vde_log::set_level(LogLevel::Info); ASSERT_TRUE(vde_log::set_output_file(path)); EXPECT_TRUE(std::filesystem::exists(path_from_utf8(path))); // Error 级别会立即 flush(vde_log.cpp:155),随后即可从磁盘读回。 vde_log::error("utf8", "path", "UTF8_MARKER 中文日志"); { std::ifstream in = open_ifstream_utf8(path); ASSERT_TRUE(static_cast(in)); const std::string text((std::istreambuf_iterator(in)), std::istreambuf_iterator()); EXPECT_NE(text.find("UTF8_MARKER"), std::string::npos) << text; EXPECT_NE(text.find("中文日志"), std::string::npos) << text; } // 无公开 close API:用 NUL 设备替换输出以关闭旧句柄,再恢复默认。 vde_log::set_output_file("NUL"); vde_log::set_output(nullptr); vde_log::set_level(LogLevel::Info); std::filesystem::remove(path_from_utf8(path), ec); }