Overhaul Logging and Configuration; integrate the log facade library-wide
Void::Log facade (header-only, macro-free):
- Leveled function templates trace/debug/info/warn/error/fatal(module,
fmt, args...) with lazy formatting - a disabled call is one relaxed
atomic load, no allocation, no formatting.
- Global min level + per-module overrides (can raise or lower); empty
sink/Disabled level = logging fully off; set_sink remains the splice
point for external logging libraries.
Void::Logging::Logger - async by default, one class for both modes:
- Mode::Async (default): lock-free MPSC queue + logger-owned worker
delivering batches via on_flush outside any lock; Immediate/Buffered
give synchronous behavior via constructor arg or set_mode at runtime.
- Bounded queue with OverflowPolicy{Block, DropNewest, DropOldest};
flush() (now virtual) blocks until everything submitted is delivered;
base destructor only stops/joins the worker, never touches virtuals.
- Fixed a wakeup race: stale park token + fresh release overflowed the
binary semaphore; wakeup is a counting semaphore (spurious wakes are
absorbed by the worker loop).
New sinks and formatters:
- RotatingFileLogger (size-based rotation, max-files cap), NullLogger,
CallbackLogger, MultiLogger (tee via new Logger::relay), SyslogLogger.
- Formatter interface: PatternFormatter ({time}/{level}/{scopes}/...),
JsonLinesFormatter, LogfmtFormatter; installable on Console/File/
Rotating sinks. Console/File sinks now default to Async.
- Logging::from_config builds the whole sink tree from Config keys;
apply_facade_config drives facade levels.
Configuration:
- Typed get_as<T>/get_or_as<T> (numbers, bool spellings, enums by name,
chrono durations), dotted keys, subtree(), sorted keys().
- New sources: from_env_prefix (APP_LOG__LEVEL -> log.level), from_ini
(sections as prefixes), from_json (objects/arrays flattened),
from_args (--key=value / --key value / --flag).
- Bind.hpp: reflection bind<T>(cfg, prefix) fills aggregates; defaults
in the struct are the schema; nested aggregates, optional, vector.
- Watch.hpp: ConfigWatch reloads on change with per-key diff; parse
failures keep the previous config.
System: new FileSystem::FileWatcher - debounced polling watcher (stable
mtime+size across N polls), callbacks off-thread, deletion events;
shared groundwork for plugin hot-reload.
Sweep: all internal Void::Log::log call sites migrated to the leveled
API ("Umbrella.Feature" labels); Plugin loader now logs load/reload.
918 tests green (28 new), stable across 30 consecutive runs, ASan+UBSan
clean.
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
1 parent
8a6065539a
commit
c74a02a5a5
43 files changed
+3889
-236
No files matched your search
@@ -0,0 +1,168 @@
|
||||
#pragma once
|
||||
|
||||
#include <Void/Configuration/Config.hpp>
|
||||
#include <Void/Functional/Error.hpp>
|
||||
#include <Void/Functional/Result.hpp>
|
||||
|
||||
#include <chrono>
|
||||
#include <format>
|
||||
#include <optional>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <type_traits>
|
||||
#include <vector>
|
||||
|
||||
#if __has_include(<meta>)
|
||||
#include <meta>
|
||||
#define VOID_HAS_CONFIG_REFLECTION 1
|
||||
#endif
|
||||
|
||||
// ── Reflection config binding
|
||||
// ───────────────────────────────────────────────── Fill an aggregate struct
|
||||
// from a Config using P2996 reflection: each data member binds to the dotted
|
||||
// key matching its name (under an optional prefix); nested aggregates extend
|
||||
// the prefix. The default-initialized member values double as the schema's
|
||||
// defaults — a missing key keeps the default, a present key that fails
|
||||
// conversion is an error.
|
||||
//
|
||||
// struct LogSettings {
|
||||
// std::string level = "info";
|
||||
// bool async = true;
|
||||
// std::chrono::milliseconds flush_every{250};
|
||||
// };
|
||||
// auto s = Void::Configuration::bind<LogSettings>(cfg, "log");
|
||||
//
|
||||
// Supported member types: everything Config::get_as<T> accepts (string,
|
||||
// bool, arithmetic, enums, chrono durations), std::optional<leaf>,
|
||||
// std::vector<leaf> (keys "name.0", "name.1", ...), nested aggregates.
|
||||
|
||||
namespace Void::Configuration
|
||||
{
|
||||
|
||||
#ifdef VOID_HAS_CONFIG_REFLECTION
|
||||
|
||||
namespace BindDetail
|
||||
{
|
||||
template <typename T> struct IsOptional : std::false_type {
|
||||
};
|
||||
template <typename U>
|
||||
struct IsOptional<std::optional<U>> : std::true_type {
|
||||
};
|
||||
|
||||
template <typename T> struct IsVector : std::false_type {
|
||||
};
|
||||
template <typename U> struct IsVector<std::vector<U>> : std::true_type {
|
||||
};
|
||||
|
||||
template <typename T> struct IsDuration : std::false_type {
|
||||
};
|
||||
template <typename Rep, typename Period>
|
||||
struct IsDuration<std::chrono::duration<Rep, Period>> : std::true_type {
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
concept Leaf = std::is_same_v<T, std::string> ||
|
||||
std::is_same_v<T, bool> || std::is_arithmetic_v<T> ||
|
||||
std::is_enum_v<T> || IsDuration<T>::value;
|
||||
|
||||
template <typename T>
|
||||
concept BindableAggregate =
|
||||
std::is_aggregate_v<T> && !std::is_array_v<T> &&
|
||||
std::is_default_constructible_v<T>;
|
||||
|
||||
[[nodiscard]] inline std::string join(std::string_view prefix,
|
||||
std::string_view name)
|
||||
{
|
||||
if (prefix.empty()) {
|
||||
return std::string{name};
|
||||
}
|
||||
return std::string{prefix} + "." + std::string{name};
|
||||
}
|
||||
|
||||
template <BindableAggregate T>
|
||||
Void::Result::Result<T, Void::Result::Error>
|
||||
bind_into(const Config& cfg, std::string_view prefix)
|
||||
{
|
||||
T out{};
|
||||
std::optional<Void::Result::Error> failure;
|
||||
|
||||
static constexpr auto members =
|
||||
std::define_static_array(std::meta::nonstatic_data_members_of(
|
||||
^^T, std::meta::access_context::current()));
|
||||
|
||||
template for (constexpr auto m : members)
|
||||
{
|
||||
if (!failure) {
|
||||
constexpr auto name = std::meta::identifier_of(m);
|
||||
using MT = [:std::meta::type_of(m):];
|
||||
const auto key = join(prefix, name);
|
||||
|
||||
if constexpr (BindableAggregate<MT> && !Leaf<MT>) {
|
||||
auto nested = bind_into<MT>(cfg, key);
|
||||
if (!nested.has_value()) {
|
||||
failure = std::move(nested).error();
|
||||
} else {
|
||||
out.[:m:] = std::move(*nested);
|
||||
}
|
||||
} else if constexpr (IsOptional<MT>::value) {
|
||||
using U = typename MT::value_type;
|
||||
if (cfg.contains(key)) {
|
||||
auto v = cfg.get_as<U>(key);
|
||||
if (!v.has_value()) {
|
||||
failure = std::move(v).error();
|
||||
} else {
|
||||
out.[:m:] = std::move(*v);
|
||||
}
|
||||
}
|
||||
} else if constexpr (IsVector<MT>::value) {
|
||||
using U = typename MT::value_type;
|
||||
MT items;
|
||||
for (std::size_t i = 0;; ++i) {
|
||||
const auto item_key = key + "." + std::to_string(i);
|
||||
if (!cfg.contains(item_key)) {
|
||||
break;
|
||||
}
|
||||
auto v = cfg.get_as<U>(item_key);
|
||||
if (!v.has_value()) {
|
||||
failure = std::move(v).error();
|
||||
break;
|
||||
}
|
||||
items.push_back(std::move(*v));
|
||||
}
|
||||
if (!failure && !items.empty()) {
|
||||
out.[:m:] = std::move(items);
|
||||
}
|
||||
} else {
|
||||
static_assert(Leaf<MT>,
|
||||
"member type not bindable from Config");
|
||||
if (cfg.contains(key)) {
|
||||
auto v = cfg.get_as<MT>(key);
|
||||
if (!v.has_value()) {
|
||||
failure = std::move(v).error();
|
||||
} else {
|
||||
out.[:m:] = std::move(*v);
|
||||
}
|
||||
}
|
||||
// absent key: keep the default member value
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (failure) {
|
||||
return Void::Result::fail(std::move(*failure));
|
||||
}
|
||||
return Void::Result::ok(std::move(out));
|
||||
}
|
||||
} // namespace BindDetail
|
||||
|
||||
/// Bind cfg's keys under `prefix` into a default-constructed T.
|
||||
template <BindDetail::BindableAggregate T>
|
||||
[[nodiscard]] Void::Result::Result<T, Void::Result::Error>
|
||||
bind(const Config& cfg, std::string_view prefix = {})
|
||||
{
|
||||
return BindDetail::bind_into<T>(cfg, prefix);
|
||||
}
|
||||
|
||||
#endif // VOID_HAS_CONFIG_REFLECTION
|
||||
|
||||
} // namespace Void::Configuration
|
||||
Reference in new issue
Block a user