feat(Log): harden LogState facade, bounded Logger delivery/tickets/quarantine, file sync/reopen, redaction, and separate LogConfig (M-LOG-01..05)
This commit is contained in:
1 parent
18a623aa62
commit
a74a402938
23 files changed
+2656
-1197
No files matched your search
+208
-53
@@ -1,8 +1,12 @@
|
||||
#pragma once
|
||||
#include <atomic>
|
||||
#include <concepts>
|
||||
#include <condition_variable>
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <format>
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
#include <shared_mutex>
|
||||
#include <source_location>
|
||||
@@ -11,6 +15,7 @@
|
||||
#include <type_traits>
|
||||
#include <unordered_map>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
// ── Void::Log — the emission facade
|
||||
// ─────────────────────────────────────────── Header-only apart from its
|
||||
@@ -18,18 +23,6 @@
|
||||
// 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
|
||||
{
|
||||
@@ -44,23 +37,24 @@ namespace Void::Log
|
||||
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)
|
||||
/// Signature for any log consumer.
|
||||
using Sink =
|
||||
std::function<void(Level level, std::string_view module,
|
||||
std::string_view message,
|
||||
const std::source_location& location)>;
|
||||
|
||||
using RegistrationId = std::uint64_t;
|
||||
|
||||
enum class DrainResult : std::uint8_t {
|
||||
drained = 0,
|
||||
not_found,
|
||||
would_deadlock
|
||||
};
|
||||
|
||||
using EmergencyHandler = void (*)(std::string_view message) noexcept;
|
||||
|
||||
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;
|
||||
@@ -74,36 +68,176 @@ namespace Void::Log
|
||||
}
|
||||
};
|
||||
|
||||
struct SinkRegistration {
|
||||
RegistrationId id{0};
|
||||
Sink sink{};
|
||||
std::shared_ptr<const void> owner{};
|
||||
std::atomic<bool> detached{false};
|
||||
std::atomic<std::size_t> active_calls{0};
|
||||
std::mutex drain_mutex;
|
||||
std::condition_variable drain_cv;
|
||||
};
|
||||
|
||||
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};
|
||||
std::atomic<std::uint64_t> reentrant_dropped{0};
|
||||
std::atomic<EmergencyHandler> emergency_handler{nullptr};
|
||||
|
||||
// Slow path.
|
||||
Sink sink;
|
||||
// Slow path (registration is immutable after creation;
|
||||
// invoked and destroyed strictly outside `mutex`).
|
||||
std::shared_ptr<SinkRegistration> registration{};
|
||||
RegistrationId next_id{0};
|
||||
std::unordered_map<RegistrationId, std::weak_ptr<SinkRegistration>> known;
|
||||
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).
|
||||
inline constexpr std::size_t kMaxNestedFacadeSinks = 8;
|
||||
|
||||
// Process-wide singleton accessors defined in Void::LogState.
|
||||
State& state();
|
||||
std::vector<const SinkRegistration*>& active_registrations_tls() noexcept;
|
||||
|
||||
struct ActiveRegistrationGuard {
|
||||
std::shared_ptr<SinkRegistration> reg;
|
||||
std::vector<const SinkRegistration*>& stack;
|
||||
|
||||
ActiveRegistrationGuard(std::shared_ptr<SinkRegistration> r,
|
||||
std::vector<const SinkRegistration*>& st)
|
||||
: reg(std::move(r)), stack(st)
|
||||
{
|
||||
stack.push_back(reg.get());
|
||||
}
|
||||
|
||||
~ActiveRegistrationGuard() noexcept
|
||||
{
|
||||
if (!stack.empty()) {
|
||||
stack.pop_back();
|
||||
}
|
||||
if (reg->active_calls.fetch_sub(
|
||||
1, std::memory_order_acq_rel) == 1) {
|
||||
std::lock_guard lock{reg->drain_mutex};
|
||||
reg->drain_cv.notify_all();
|
||||
}
|
||||
}
|
||||
|
||||
ActiveRegistrationGuard(const ActiveRegistrationGuard&) = delete;
|
||||
ActiveRegistrationGuard&
|
||||
operator=(const ActiveRegistrationGuard&) = delete;
|
||||
};
|
||||
} // 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)
|
||||
/// Install a sink alongside an optional lifetime-keepalive owner and return
|
||||
/// its registration ID for explicit detach_and_drain().
|
||||
inline RegistrationId
|
||||
register_sink(Sink sink, std::shared_ptr<const void> owner = {})
|
||||
{
|
||||
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);
|
||||
std::shared_ptr<Detail::SinkRegistration> prev;
|
||||
RegistrationId id = 0;
|
||||
|
||||
if (sink) {
|
||||
auto next = std::make_shared<Detail::SinkRegistration>();
|
||||
next->sink = std::move(sink);
|
||||
next->owner = std::move(owner);
|
||||
std::unique_lock lock{s.mutex};
|
||||
id = ++s.next_id;
|
||||
next->id = id;
|
||||
s.known.emplace(id, next);
|
||||
for (auto it = s.known.begin(); it != s.known.end();) {
|
||||
if (it->second.expired()) {
|
||||
it = s.known.erase(it);
|
||||
} else {
|
||||
++it;
|
||||
}
|
||||
}
|
||||
prev = std::exchange(s.registration, std::move(next));
|
||||
if (prev) {
|
||||
prev->detached.store(true, std::memory_order_release);
|
||||
}
|
||||
s.has_sink.store(true, std::memory_order_release);
|
||||
} else {
|
||||
std::unique_lock lock{s.mutex};
|
||||
prev = std::exchange(s.registration, nullptr);
|
||||
if (prev) {
|
||||
prev->detached.store(true, std::memory_order_release);
|
||||
}
|
||||
s.has_sink.store(false, std::memory_order_release);
|
||||
}
|
||||
|
||||
// Destroy previous callback/owner outside s.mutex.
|
||||
prev.reset();
|
||||
return id;
|
||||
}
|
||||
|
||||
/// Install the global sink. Any previously installed sink is replaced
|
||||
/// outside the facade lock (does not block waiting for in-flight calls).
|
||||
inline void set_sink(Sink sink)
|
||||
{
|
||||
(void)register_sink(std::move(sink), {});
|
||||
}
|
||||
|
||||
/// Detach a registered sink (or active sink when id == 0) and wait until
|
||||
/// all already-admitted invocations using that registration complete.
|
||||
/// Returns would_deadlock if called synchronously from that registration's
|
||||
/// own active callback.
|
||||
[[nodiscard]] inline DrainResult detach_and_drain(RegistrationId id = 0)
|
||||
{
|
||||
auto& s = Detail::state();
|
||||
std::shared_ptr<Detail::SinkRegistration> target;
|
||||
{
|
||||
std::unique_lock lock{s.mutex};
|
||||
if (id == 0) {
|
||||
target = s.registration;
|
||||
} else if (s.registration && s.registration->id == id) {
|
||||
target = s.registration;
|
||||
} else if (const auto it = s.known.find(id); it != s.known.end()) {
|
||||
target = it->second.lock();
|
||||
}
|
||||
|
||||
if (!target) {
|
||||
return DrainResult::not_found;
|
||||
}
|
||||
|
||||
const auto& active = Detail::active_registrations_tls();
|
||||
for (const auto* entry : active) {
|
||||
if (entry == target.get()) {
|
||||
return DrainResult::would_deadlock;
|
||||
}
|
||||
}
|
||||
|
||||
target->detached.store(true, std::memory_order_release);
|
||||
if (target == s.registration) {
|
||||
s.registration.reset();
|
||||
s.has_sink.store(false, std::memory_order_release);
|
||||
}
|
||||
s.known.erase(target->id);
|
||||
}
|
||||
|
||||
{
|
||||
std::unique_lock drain_lock{target->drain_mutex};
|
||||
target->drain_cv.wait(drain_lock, [&target] {
|
||||
return target->active_calls.load(std::memory_order_acquire) == 0;
|
||||
});
|
||||
}
|
||||
target.reset();
|
||||
return DrainResult::drained;
|
||||
}
|
||||
|
||||
[[nodiscard]] inline std::uint64_t reentrant_dropped() noexcept
|
||||
{
|
||||
return Detail::state().reentrant_dropped.load(std::memory_order_relaxed);
|
||||
}
|
||||
|
||||
inline void set_emergency_handler(EmergencyHandler handler) noexcept
|
||||
{
|
||||
Detail::state().emergency_handler.store(
|
||||
handler, 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);
|
||||
@@ -114,8 +248,6 @@ namespace Void::Log
|
||||
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();
|
||||
@@ -124,7 +256,6 @@ namespace Void::Log
|
||||
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();
|
||||
@@ -134,7 +265,6 @@ namespace Void::Log
|
||||
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();
|
||||
@@ -154,15 +284,8 @@ namespace Void::Log
|
||||
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)
|
||||
@@ -171,10 +294,45 @@ namespace Void::Log
|
||||
if (!::Void::Log::enabled(level, module)) {
|
||||
return;
|
||||
}
|
||||
std::shared_lock lock{s.mutex};
|
||||
if (s.sink) {
|
||||
s.sink(level, module, message, location);
|
||||
|
||||
std::shared_ptr<SinkRegistration> reg;
|
||||
bool reentrant = false;
|
||||
{
|
||||
std::shared_lock lock{s.mutex};
|
||||
reg = s.registration;
|
||||
if (!reg || reg->detached.load(std::memory_order_acquire) ||
|
||||
!reg->sink) {
|
||||
return;
|
||||
}
|
||||
const auto& active = active_registrations_tls();
|
||||
if (active.size() >= kMaxNestedFacadeSinks) {
|
||||
reentrant = true;
|
||||
} else {
|
||||
for (const auto* entry : active) {
|
||||
if (entry == reg.get()) {
|
||||
reentrant = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!reentrant) {
|
||||
reg->active_calls.fetch_add(1, std::memory_order_acq_rel);
|
||||
}
|
||||
}
|
||||
|
||||
if (reentrant) {
|
||||
s.reentrant_dropped.fetch_add(1, std::memory_order_relaxed);
|
||||
if (const auto eh =
|
||||
s.emergency_handler.load(std::memory_order_acquire);
|
||||
eh != nullptr) {
|
||||
eh(message);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
ActiveRegistrationGuard guard{
|
||||
std::move(reg), active_registrations_tls()};
|
||||
guard.reg->sink(level, module, message, location);
|
||||
}
|
||||
} // namespace Detail
|
||||
|
||||
@@ -186,9 +344,6 @@ namespace Void::Log
|
||||
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)
|
||||
@@ -243,4 +398,4 @@ namespace Void::Log
|
||||
log(Level::Fatal, module, fmt, std::forward<Args>(args)...);
|
||||
}
|
||||
|
||||
} // namespace Void::Log
|
||||
} // namespace Void::Log
|
||||
Reference in new issue
Block a user