#include #include #include #include #include #include #include #include #include #include #include using Void::Configuration::Config; Void::Testing::Test from_config_defaults_to_console() { Config cfg; // empty: console sink, trace level, async const auto logger = Void::Logging::from_config(cfg); Void::Testing::expect(logger.has_value()); Void::Testing::expect((*logger)->mode() == Void::Logging::Mode::Async); } Void::Testing::Test from_config_builds_multi_sink_tree() { Void::FileSystem::TempDir tmp{"void_fromcfg"}; Config cfg; cfg.set("log.sinks", "console, file"); cfg.set("log.file.path", (tmp.path() / "a.log").string()); cfg.set("log.level", "warning"); cfg.set("log.async", "false"); const auto logger = Void::Logging::from_config(cfg); Void::Testing::expect(logger.has_value()); Void::Testing::expect((*logger)->mode() == Void::Logging::Mode::Immediate); Void::Testing::expect((*logger)->level() == Void::Logging::Level::Warning); (*logger)->warning("to both sinks"); (*logger)->flush(); std::ifstream f(tmp.path() / "a.log"); std::string content((std::istreambuf_iterator(f)), {}); Void::Testing::expect(content.find("to both sinks") != std::string::npos); } Void::Testing::Test from_config_split_and_history_sinks() { Void::FileSystem::TempDir tmp{"void_fromcfg"}; Config cfg; cfg.set("log.sinks", "split, history"); cfg.set("log.split.dir", (tmp.path() / "split").string()); cfg.set("log.history.path", (tmp.path() / "history.jsonl").string()); cfg.set("log.async", "false"); const auto logger = Void::Logging::from_config(cfg); Void::Testing::expect(logger.has_value()); (*logger)->error("split me"); (*logger)->flush(); std::ifstream err(tmp.path() / "split" / "error.log"); std::string err_content((std::istreambuf_iterator(err)), {}); Void::Testing::expect(err_content.find("split me") != std::string::npos); std::ifstream hist(tmp.path() / "history.jsonl"); std::string hist_content((std::istreambuf_iterator(hist)), {}); Void::Testing::expect(hist_content.find("split me") != std::string::npos); } Void::Testing::Test from_config_json_format_applies() { Void::FileSystem::TempDir tmp{"void_fromcfg"}; Config cfg; cfg.set("log.sinks", "file"); cfg.set("log.file.path", (tmp.path() / "j.log").string()); cfg.set("log.format", "json"); cfg.set("log.async", "false"); const auto logger = Void::Logging::from_config(cfg); Void::Testing::expect(logger.has_value()); (*logger)->info("structured"); (*logger)->flush(); std::ifstream f(tmp.path() / "j.log"); std::string line; std::getline(f, line); Void::Testing::expect(line.find("\"message\":\"structured\"") != std::string::npos); } Void::Testing::Test from_config_missing_file_path_is_error() { Config cfg; cfg.set("log.sinks", "file"); Void::Testing::expect(!Void::Logging::from_config(cfg).has_value()); } Void::Testing::Test from_config_unknown_sink_is_error() { Config cfg; cfg.set("log.sinks", "carrier-pigeon"); Void::Testing::expect(!Void::Logging::from_config(cfg).has_value()); } Void::Testing::Test apply_facade_config_sets_levels() { struct Reset { ~Reset() { Void::Log::set_min_level(Void::Log::Level::Trace); Void::Log::clear_module_level("Fsm"); } } reset; Config cfg; cfg.set("log.level", "error"); cfg.set("log.module_levels.Fsm", "trace"); Void::Logging::apply_facade_config(cfg); Void::Log::set_sink([](auto, auto, auto) {}); Void::Testing::expect( !Void::Log::enabled(Void::Log::Level::Info, "Other")); Void::Testing::expect( Void::Log::enabled(Void::Log::Level::Trace, "Fsm")); Void::Log::set_sink({}); }