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>
This commit is contained in:
faumarayandClaude Opus 4.8 committed 2026-06-25 02:35:42 +03:00
1 parent e4ddb0c8c4
commit 2d3f9dc319
5 files changed
+97 -49

No files matched your search

+74 -30
View File
@@ -1,11 +1,14 @@
#pragma once
#include <atomic>
#include <concepts>
#include <format>
#include <functional>
#include <mutex>
#include <shared_mutex>
#include <source_location>
#include <string>
#include <string_view>
#include <type_traits>
#include <unordered_map>
#include <utility>
@@ -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<void(Level level, std::string_view module,
std::string_view message)>;
/// 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<void(Level level, std::string_view module,
std::string_view message,
const std::source_location& location)>;
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<Args>... keeps Args non-deduced here, so they
// are deduced solely from the trailing argument pack.
template <typename... Args> struct FmtLoc {
std::format_string<Args...> fmt;
std::source_location location;
template <std::convertible_to<std::string_view> T>
consteval FmtLoc(T&& f,
std::source_location loc =
std::source_location::current())
: fmt(std::forward<T>(f)), location(loc)
{
}
};
struct State {
// Fast-path gates (checked before any locking or formatting).
std::atomic<bool> 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 <typename... Args>
void log(Level level, std::string_view module,
std::format_string<Args...> fmt, Args&&... args)
Detail::FmtLoc<std::type_identity_t<Args>...> fmt, Args&&... args)
{
if (!enabled(level, module)) {
return;
}
log(level, module, std::format(fmt, std::forward<Args>(args)...));
Detail::emit(level, module,
std::format(fmt.fmt, std::forward<Args>(args)...),
fmt.location);
}
template <typename... Args>
void trace(std::string_view module, std::format_string<Args...> fmt,
Args&&... args)
void trace(std::string_view module,
Detail::FmtLoc<std::type_identity_t<Args>...> fmt, Args&&... args)
{
log(Level::Trace, module, fmt, std::forward<Args>(args)...);
}
template <typename... Args>
void debug(std::string_view module, std::format_string<Args...> fmt,
Args&&... args)
void debug(std::string_view module,
Detail::FmtLoc<std::type_identity_t<Args>...> fmt, Args&&... args)
{
log(Level::Debug, module, fmt, std::forward<Args>(args)...);
}
template <typename... Args>
void info(std::string_view module, std::format_string<Args...> fmt,
Args&&... args)
void info(std::string_view module,
Detail::FmtLoc<std::type_identity_t<Args>...> fmt, Args&&... args)
{
log(Level::Info, module, fmt, std::forward<Args>(args)...);
}
template <typename... Args>
void warn(std::string_view module, std::format_string<Args...> fmt,
Args&&... args)
void warn(std::string_view module,
Detail::FmtLoc<std::type_identity_t<Args>...> fmt, Args&&... args)
{
log(Level::Warning, module, fmt, std::forward<Args>(args)...);
}
template <typename... Args>
void error(std::string_view module, std::format_string<Args...> fmt,
Args&&... args)
void error(std::string_view module,
Detail::FmtLoc<std::type_identity_t<Args>...> fmt, Args&&... args)
{
log(Level::Error, module, fmt, std::forward<Args>(args)...);
}
template <typename... Args>
void fatal(std::string_view module, std::format_string<Args...> fmt,
Args&&... args)
void fatal(std::string_view module,
Detail::FmtLoc<std::type_identity_t<Args>...> fmt, Args&&... args)
{
log(Level::Fatal, module, fmt, std::forward<Args>(args)...);
}
+3 -2
View File
@@ -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);
};
}
+2 -1
View File
@@ -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;
}
+1 -1
View File
@@ -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(
+17 -15
View File
@@ -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<int> 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<std::thread> 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"));