Files
Library/tests/standalone/Log/FromConfig.cpp
T
faumarayandClaude Opus 4.8 2d3f9dc319 Log: carry call-site source_location through to the sink
Every log line showed LoggingAdapter.hpp as its source because make_sink
called Void::Logging::Logger::log() with a defaulted source_location captured
inside the adapter, discarding the real call site.

Capture std::source_location at each Void::Log::{info,warn,...} call via an
FmtLoc wrapper (the same trick Logger uses internally), thread it through a
4-argument Sink, and forward it from make_sink to logger.log(). Sinks now
receive the originating file/line/function.

Formatted emission is routed through a single non-overloaded Detail::emit():
forwarding a (level, module, std::string, source_location) call to the public
log() would otherwise also match the variadic log() overload, deducing
source_location as a format argument and failing in FmtLoc's consteval ctor.

Sink is now a 4-arg callable; updated the bundled tests accordingly.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-25 02:35:42 +03:00

131 lines
4.2 KiB
C++

#include <Void/Configuration/Config.hpp>
#include <Void/Log/FromConfig.hpp>
#include <Void/Log/Log.hpp>
#include <Void/Log/RotatingFileLogger.hpp>
#include <Void/Log/Sinks.hpp>
#include <Void/System/TempDir.hpp>
#include <Void/Testing/Assert.hpp>
#include <Void/Testing/Test.hpp>
#include <filesystem>
#include <fstream>
#include <string>
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<char>(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<char>(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<char>(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, 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({});
}