Files
Library/include/Void/Log/Log.hpp
T
faumarayandClaude Opus 5.5 cd237207d4
Release / release (push) Failing after 5s
Log: one facade state per process, in a LogState library
Void::Log::Detail::state() was a static in an inline function, which is
one object per DLL on Windows: a sink set from one DLL never saw events
from another (a plugin's log lines, or any module but the one that set
it). The state now lives in Void::LogState, a small compiled module with
no dependencies, linked by every module that includes the facade
(Async, System, Stacktrace, Testing; the rest through them), so
INTERFACE modules can still log and no dependency cycle appears.

Tests: VoidTests 1242 pass, 3 skip; run_with_args_builds_correct_argv
and run_stdin_forwarded_to_cat fail on NixOS, which has no
/bin/echo or /bin/cat (exit 127).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-27 05:43:45 +03:00

247 lines
7.9 KiB
C++

#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>
// ── Void::Log — the emission facade
// ─────────────────────────────────────────── Header-only apart from its
// state, which Void::LogState (a small library with no dependencies) holds
// once per process, so every module (including INTERFACE libraries) can log by
// linking only that. The full sink framework lives in Void::Logging
// (Logger.hpp); bridge with Void/Log/LoggingAdapter.hpp.
//
// Integration contract:
// * No sink installed (the default) — every call is a single relaxed
// atomic load; format arguments are NOT formatted. This is the
// "no logging at all" path.
// * set_sink(...) routes everything to any callable — your own logging
// library plugs in here.
// * set_min_level(Level::Disabled) silences without removing the sink.
// * Per-module overrides: set_module_level("Fsm", Level::Trace).
//
// The API is deliberately macro-free: leveled function templates take a
// std::format_string and only format after the level/sink gate passes.
namespace Void::Log
{
enum class Level : unsigned char {
Trace = 0,
Debug,
Info,
Warning,
Error,
Fatal,
Disabled
};
/// 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};
std::atomic<Level> min_level{Level::Trace};
std::atomic<bool> has_module_levels{false};
// Slow path.
Sink sink;
std::unordered_map<std::string, Level> module_levels;
std::shared_mutex mutex;
};
// The process's one State, defined in Void::LogState (a static in an
// inline function would be one per DLL on Windows).
State& state();
} // namespace Detail
/// Install the global sink. Any previously installed sink is replaced.
/// Pass a default-constructed (empty) Sink to silence all output.
/// Thread-safe: the new sink is visible to all threads after this returns.
inline void set_sink(Sink sink)
{
auto& s = Detail::state();
std::unique_lock lock{s.mutex};
s.sink = std::move(sink);
s.has_sink.store(static_cast<bool>(s.sink), std::memory_order_release);
}
/// Global minimum level. Events below it are discarded before
/// formatting. Default: Trace (everything passes; the sink filters).
inline void set_min_level(Level level) noexcept
{
Detail::state().min_level.store(level, std::memory_order_relaxed);
}
[[nodiscard]] inline Level min_level() noexcept
{
return Detail::state().min_level.load(std::memory_order_relaxed);
}
/// Per-module minimum level override (exact label match). Overrides take
/// precedence over the global minimum — they can both raise and lower it.
inline void set_module_level(std::string_view module, Level level)
{
auto& s = Detail::state();
std::unique_lock lock{s.mutex};
s.module_levels.insert_or_assign(std::string{module}, level);
s.has_module_levels.store(true, std::memory_order_release);
}
/// Remove a per-module override.
inline void clear_module_level(std::string_view module)
{
auto& s = Detail::state();
std::unique_lock lock{s.mutex};
s.module_levels.erase(std::string{module});
s.has_module_levels.store(!s.module_levels.empty(),
std::memory_order_release);
}
/// Would an event at `level` from `module` currently be emitted?
[[nodiscard]] inline bool enabled(Level level, std::string_view module)
{
auto& s = Detail::state();
if (!s.has_sink.load(std::memory_order_relaxed)) {
return false;
}
if (level == Level::Disabled) {
return false;
}
if (s.has_module_levels.load(std::memory_order_acquire)) {
std::shared_lock lock{s.mutex};
if (auto it = s.module_levels.find(std::string{module});
it != s.module_levels.end()) {
return level >= it->second;
}
}
return level >= s.min_level.load(std::memory_order_relaxed);
}
/// Emit one pre-formatted log event through the global sink.
/// No-op (zero allocation) when disabled.
namespace Detail
{
// 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. The call-site source location is
/// captured transparently via FmtLoc.
template <typename... Args>
void log(Level level, std::string_view module,
Detail::FmtLoc<std::type_identity_t<Args>...> fmt, Args&&... args)
{
if (!enabled(level, module)) {
return;
}
Detail::emit(level, module,
std::format(fmt.fmt, std::forward<Args>(args)...),
fmt.location);
}
template <typename... 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,
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,
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,
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,
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,
Detail::FmtLoc<std::type_identity_t<Args>...> fmt, Args&&... args)
{
log(Level::Fatal, module, fmt, std::forward<Args>(args)...);
}
} // namespace Void::Log