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>
73 lines
2.2 KiB
C++
73 lines
2.2 KiB
C++
#pragma once
|
|
#include <Void/Detail/Enum.hpp>
|
|
#include <Void/Log/Log.hpp>
|
|
#include <Void/Log/Logger.hpp>
|
|
|
|
// Bridge between Void::Log (the thin emission layer used by all modules) and
|
|
// Void::Logging::Logger (the full sink implementation).
|
|
//
|
|
// Include this header only in translation units that also link Void::Logging.
|
|
// All other code that merely calls Void::Log::log() needs only Log.hpp.
|
|
//
|
|
// Example:
|
|
// #include <Void/Log/LoggingAdapter.hpp>
|
|
// #include <Void/Log/ConsoleLogger.hpp>
|
|
//
|
|
// Void::Logging::ConsoleLogger logger;
|
|
// Void::Log::set_sink(Void::Log::make_sink(logger));
|
|
|
|
namespace Void::Log
|
|
{
|
|
|
|
/// Map a Void::Log::Level to the corresponding Void::Logging::Level.
|
|
/// Uses reflection to match by enumerator name; falls back to a
|
|
/// hand-written table for non-reflecting compilers.
|
|
[[nodiscard]] inline Void::Logging::Level
|
|
to_logging_level(Void::Log::Level level) noexcept
|
|
{
|
|
#ifdef VOID_HAS_ENUM_REFLECTION
|
|
return Void::Detail::enum_cast<Void::Logging::Level>(level);
|
|
#else
|
|
switch (level) {
|
|
case Level::Trace:
|
|
return Void::Logging::Level::Trace;
|
|
case Level::Debug:
|
|
return Void::Logging::Level::Debug;
|
|
case Level::Info:
|
|
return Void::Logging::Level::Info;
|
|
case Level::Warning:
|
|
return Void::Logging::Level::Warning;
|
|
case Level::Error:
|
|
return Void::Logging::Level::Error;
|
|
case Level::Fatal:
|
|
return Void::Logging::Level::Fatal;
|
|
case Level::Disabled:
|
|
return Void::Logging::Level::Disabled;
|
|
}
|
|
return Void::Logging::Level::Info;
|
|
#endif
|
|
}
|
|
|
|
/// Create a Sink that routes events to a Void::Logging::Logger.
|
|
///
|
|
/// The module name is prepended to the message as "[module] " so the
|
|
/// scope/tag information survives without requiring push_scope() calls on
|
|
/// the internal logger.
|
|
///
|
|
/// Lifetime: the returned Sink holds a reference to logger; logger must
|
|
/// outlive any call made through the Sink.
|
|
[[nodiscard]] inline Sink make_sink(Void::Logging::Logger& logger)
|
|
{
|
|
return [&logger](Level level, std::string_view module,
|
|
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, location);
|
|
};
|
|
}
|
|
|
|
} // namespace Void::Log
|