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:
1 parent
e4ddb0c8c4
commit
2d3f9dc319
5 files changed
+97
-49
No files matched your search
+74
-30
@@ -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)...);
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user