From 2d3f9dc319ed7034aef6ae7ced151a7986b34313 Mon Sep 17 00:00:00 2001 From: faumaray Date: Thu, 25 Jun 2026 02:35:42 +0300 Subject: [PATCH] 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 --- include/Void/Log/Log.hpp | 104 ++++++++++++++++++++-------- include/Void/Log/LoggingAdapter.hpp | 5 +- tests/internal/CrossModule.cpp | 3 +- tests/standalone/Log/FromConfig.cpp | 2 +- tests/standalone/Log/Log.cpp | 32 +++++---- 5 files changed, 97 insertions(+), 49 deletions(-) diff --git a/include/Void/Log/Log.hpp b/include/Void/Log/Log.hpp index 505c498..c721f5c 100644 --- a/include/Void/Log/Log.hpp +++ b/include/Void/Log/Log.hpp @@ -1,11 +1,14 @@ #pragma once #include +#include #include #include #include #include +#include #include #include +#include #include #include @@ -40,15 +43,36 @@ namespace Void::Log Disabled }; - /// Signature for any log consumer. Three arguments: - /// level — severity of the event - /// module — short library-internal label (e.g. "System.Process") - /// message — human-readable description - using Sink = std::function; + /// Signature for any log consumer. Four arguments: + /// level — severity of the event + /// module — short library-internal label (e.g. "System.Process") + /// message — human-readable description + /// location — the call site that emitted the event (file/line/function) + using Sink = + std::function; namespace Detail { + // std::source_location cannot follow a variadic pack in a parameter + // list, so this wrapper captures the format string together with the + // caller's source location in its (consteval) constructor. + // std::type_identity_t... keeps Args non-deduced here, so they + // are deduced solely from the trailing argument pack. + template struct FmtLoc { + std::format_string fmt; + std::source_location location; + + template T> + consteval FmtLoc(T&& f, + std::source_location loc = + std::source_location::current()) + : fmt(std::forward(f)), location(loc) + { + } + }; + struct State { // Fast-path gates (checked before any locking or formatting). std::atomic has_sink{false}; @@ -133,69 +157,89 @@ namespace Void::Log /// Emit one pre-formatted log event through the global sink. /// No-op (zero allocation) when disabled. - inline void log(Level level, std::string_view module, - std::string_view message) + namespace Detail { - auto& s = Detail::state(); - if (!enabled(level, module)) { - return; - } - std::shared_lock lock{s.mutex}; - if (s.sink) { - s.sink(level, module, message); + // Single, non-overloaded dispatch to the sink. The formatted paths + // must forward through this rather than through log() itself: a + // log(level, module, std::string, source_location) call would also + // match the variadic log() overload (deducing source_location as a + // format argument) and fail in FmtLoc's consteval constructor. + inline void emit(Level level, std::string_view module, + std::string_view message, + const std::source_location& location) + { + auto& s = state(); + if (!::Void::Log::enabled(level, module)) { + return; + } + std::shared_lock lock{s.mutex}; + if (s.sink) { + s.sink(level, module, message, location); + } } + } // namespace Detail + + inline void log(Level level, std::string_view module, + std::string_view message, + const std::source_location& location = + std::source_location::current()) + { + Detail::emit(level, module, message, location); } /// Leveled, lazily-formatted emission: arguments are formatted only - /// after the level/sink gate passes. + /// after the level/sink gate passes. The call-site source location is + /// captured transparently via FmtLoc. template void log(Level level, std::string_view module, - std::format_string fmt, Args&&... args) + Detail::FmtLoc...> fmt, Args&&... args) { if (!enabled(level, module)) { return; } - log(level, module, std::format(fmt, std::forward(args)...)); + Detail::emit(level, module, + std::format(fmt.fmt, std::forward(args)...), + fmt.location); } template - void trace(std::string_view module, std::format_string fmt, - Args&&... args) + void trace(std::string_view module, + Detail::FmtLoc...> fmt, Args&&... args) { log(Level::Trace, module, fmt, std::forward(args)...); } template - void debug(std::string_view module, std::format_string fmt, - Args&&... args) + void debug(std::string_view module, + Detail::FmtLoc...> fmt, Args&&... args) { log(Level::Debug, module, fmt, std::forward(args)...); } template - void info(std::string_view module, std::format_string fmt, - Args&&... args) + void info(std::string_view module, + Detail::FmtLoc...> fmt, Args&&... args) { log(Level::Info, module, fmt, std::forward(args)...); } template - void warn(std::string_view module, std::format_string fmt, - Args&&... args) + void warn(std::string_view module, + Detail::FmtLoc...> fmt, Args&&... args) { log(Level::Warning, module, fmt, std::forward(args)...); } template - void error(std::string_view module, std::format_string fmt, - Args&&... args) + void error(std::string_view module, + Detail::FmtLoc...> fmt, Args&&... args) { log(Level::Error, module, fmt, std::forward(args)...); } template - void fatal(std::string_view module, std::format_string fmt, - Args&&... args) + void fatal(std::string_view module, + Detail::FmtLoc...> fmt, Args&&... args) { log(Level::Fatal, module, fmt, std::forward(args)...); } diff --git a/include/Void/Log/LoggingAdapter.hpp b/include/Void/Log/LoggingAdapter.hpp index d84ae19..2612a8f 100644 --- a/include/Void/Log/LoggingAdapter.hpp +++ b/include/Void/Log/LoggingAdapter.hpp @@ -59,12 +59,13 @@ namespace Void::Log [[nodiscard]] inline Sink make_sink(Void::Logging::Logger& logger) { return [&logger](Level level, std::string_view module, - std::string_view message) { + std::string_view message, + const std::source_location& location) { auto full = std::string{"["}; full += module; full += "] "; full += message; - logger.log(to_logging_level(level), full); + logger.log(to_logging_level(level), full, location); }; } diff --git a/tests/internal/CrossModule.cpp b/tests/internal/CrossModule.cpp index 5ef5f67..442e1ae 100644 --- a/tests/internal/CrossModule.cpp +++ b/tests/internal/CrossModule.cpp @@ -147,7 +147,8 @@ Void::Testing::Test filesystem_ops_emit_log_events() int debug_count = 0; int warning_count = 0; Void::Log::set_sink( - [&](Void::Log::Level l, std::string_view, std::string_view) { + [&](Void::Log::Level l, std::string_view, std::string_view, + const std::source_location&) { if (l == Void::Log::Level::Debug) { ++debug_count; } diff --git a/tests/standalone/Log/FromConfig.cpp b/tests/standalone/Log/FromConfig.cpp index 83db69e..069c8e7 100644 --- a/tests/standalone/Log/FromConfig.cpp +++ b/tests/standalone/Log/FromConfig.cpp @@ -121,7 +121,7 @@ Void::Testing::Test apply_facade_config_sets_levels() cfg.set("log.module_levels.Fsm", "trace"); Void::Logging::apply_facade_config(cfg); - Void::Log::set_sink([](auto, auto, auto) {}); + Void::Log::set_sink([](auto, auto, auto, auto) {}); Void::Testing::expect( !Void::Log::enabled(Void::Log::Level::Info, "Other")); Void::Testing::expect( diff --git a/tests/standalone/Log/Log.cpp b/tests/standalone/Log/Log.cpp index 411a972..17494d1 100644 --- a/tests/standalone/Log/Log.cpp +++ b/tests/standalone/Log/Log.cpp @@ -35,7 +35,8 @@ Void::Testing::Test sink_receives_all_fields() std::string got_message; Void::Log::set_sink( - [&](Void::Log::Level l, std::string_view mod, std::string_view msg) { + [&](Void::Log::Level l, std::string_view mod, std::string_view msg, + const std::source_location&) { got_level = l; got_module = mod; got_message = msg; @@ -54,10 +55,10 @@ Void::Testing::Test set_sink_replaces_previous() int first_calls = 0; int second_calls = 0; - Void::Log::set_sink([&](auto, auto, auto) { ++first_calls; }); + Void::Log::set_sink([&](auto, auto, auto, auto) { ++first_calls; }); Void::Log::log(Void::Log::Level::Info, "m", "a"); - Void::Log::set_sink([&](auto, auto, auto) { ++second_calls; }); + Void::Log::set_sink([&](auto, auto, auto, auto) { ++second_calls; }); Void::Log::log(Void::Log::Level::Info, "m", "b"); Void::Testing::expect_eq(first_calls, 1); @@ -68,7 +69,7 @@ Void::Testing::Test clear_sink_silences_output() { SinkGuard guard; int calls = 0; - Void::Log::set_sink([&](auto, auto, auto) { ++calls; }); + Void::Log::set_sink([&](auto, auto, auto, auto) { ++calls; }); Void::Log::log(Void::Log::Level::Info, "m", "before clear"); Void::Log::set_sink({}); Void::Log::log(Void::Log::Level::Info, "m", "after clear"); @@ -79,7 +80,7 @@ Void::Testing::Test concurrent_log_calls_are_safe() { SinkGuard guard; std::atomic count{0}; - Void::Log::set_sink([&](auto, auto, auto) { ++count; }); + Void::Log::set_sink([&](auto, auto, auto, auto) { ++count; }); constexpr int N = 64; std::vector threads; @@ -100,7 +101,7 @@ Void::Testing::Test all_levels_are_forwarded() { SinkGuard guard; int calls = 0; - Void::Log::set_sink([&](auto, auto, auto) { ++calls; }); + Void::Log::set_sink([&](auto, auto, auto, auto) { ++calls; }); Void::Log::log(Void::Log::Level::Trace, "m", "t"); Void::Log::log(Void::Log::Level::Debug, "m", "d"); @@ -119,10 +120,11 @@ Void::Testing::Test leveled_helpers_format_arguments() SinkGuard guard; std::string got; Void::Log::Level got_level{}; - Void::Log::set_sink([&](Void::Log::Level l, auto, std::string_view msg) { - got_level = l; - got = msg; - }); + Void::Log::set_sink( + [&](Void::Log::Level l, auto, std::string_view msg, auto) { + got_level = l; + got = msg; + }); Void::Log::warn("m", "answer = {}", 42); Void::Testing::expect(got_level == Void::Log::Level::Warning); @@ -158,7 +160,7 @@ Void::Testing::Test disabled_calls_do_not_format() // Sink present but level below the global minimum: still nothing. int calls = 0; - Void::Log::set_sink([&](auto, auto, auto) { ++calls; }); + Void::Log::set_sink([&](auto, auto, auto, auto) { ++calls; }); Void::Log::set_min_level(Void::Log::Level::Error); Void::Log::debug("m", "{}", FormatProbe{&formatted}); Void::Testing::expect_eq(formatted, 0); @@ -176,7 +178,7 @@ Void::Testing::Test global_min_level_filters() { SinkGuard guard; int calls = 0; - Void::Log::set_sink([&](auto, auto, auto) { ++calls; }); + Void::Log::set_sink([&](auto, auto, auto, auto) { ++calls; }); Void::Log::set_min_level(Void::Log::Level::Warning); Void::Log::info("m", "dropped"); @@ -189,7 +191,7 @@ Void::Testing::Test disabled_level_silences_everything() { SinkGuard guard; int calls = 0; - Void::Log::set_sink([&](auto, auto, auto) { ++calls; }); + Void::Log::set_sink([&](auto, auto, auto, auto) { ++calls; }); Void::Log::set_min_level(Void::Log::Level::Disabled); Void::Log::fatal("m", "still dropped"); @@ -200,7 +202,7 @@ Void::Testing::Test module_override_lowers_and_raises() { SinkGuard guard; int calls = 0; - Void::Log::set_sink([&](auto, auto, auto) { ++calls; }); + Void::Log::set_sink([&](auto, auto, auto, auto) { ++calls; }); Void::Log::set_min_level(Void::Log::Level::Warning); // Lower the threshold for one module only. @@ -222,7 +224,7 @@ Void::Testing::Test enabled_reflects_configuration() Void::Testing::expect( !Void::Log::enabled(Void::Log::Level::Fatal, "m")); // no sink - Void::Log::set_sink([](auto, auto, auto) {}); + Void::Log::set_sink([](auto, auto, auto, auto) {}); Void::Log::set_min_level(Void::Log::Level::Info); Void::Testing::expect(Void::Log::enabled(Void::Log::Level::Info, "m")); Void::Testing::expect(!Void::Log::enabled(Void::Log::Level::Debug, "m"));