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>
160 lines
4.5 KiB
C++
160 lines
4.5 KiB
C++
#pragma once
|
|
|
|
#include <Void/Configuration/Config.hpp>
|
|
#include <Void/Functional/Error.hpp>
|
|
#include <Void/Functional/Result.hpp>
|
|
#include <Void/Log/Log.hpp>
|
|
#include <Void/System/FileWatcher.hpp>
|
|
|
|
#include <algorithm>
|
|
#include <filesystem>
|
|
#include <functional>
|
|
#include <mutex>
|
|
#include <string>
|
|
#include <utility>
|
|
#include <vector>
|
|
|
|
namespace Void::Configuration
|
|
{
|
|
/// Watches a configuration file and reloads it on change (debounced,
|
|
/// see FileSystem::FileWatcher). RAII: destroying the watch stops it.
|
|
///
|
|
/// The callback receives the freshly loaded Config plus the sorted
|
|
/// list of keys whose values changed (added, removed, or modified)
|
|
/// relative to the previous load. A load failure keeps the previous
|
|
/// configuration and logs a warning — the callback is not invoked.
|
|
///
|
|
/// Callbacks run on the watcher thread.
|
|
class ConfigWatch
|
|
{
|
|
public:
|
|
|
|
using Loader =
|
|
std::function<Void::Result::Result<Config, Void::Result::Error>(
|
|
const std::filesystem::path&)>;
|
|
using OnReload = std::function<void(
|
|
const Config&, const std::vector<std::string>& changed)>;
|
|
|
|
ConfigWatch(std::filesystem::path path, Loader loader,
|
|
OnReload on_reload,
|
|
FileSystem::FileWatcherOptions options = {})
|
|
: m_path(std::move(path)), m_loader(std::move(loader)),
|
|
m_on_reload(std::move(on_reload)), m_watcher(options)
|
|
{
|
|
if (auto initial = m_loader(m_path); initial.has_value()) {
|
|
m_current = std::move(*initial);
|
|
}
|
|
m_watcher.watch(
|
|
m_path, [this](const FileSystem::FileWatcher::Event& e) {
|
|
on_event(e);
|
|
});
|
|
}
|
|
|
|
/// Convenience factories for the built-in formats.
|
|
[[nodiscard]] static ConfigWatch
|
|
ini(std::filesystem::path path, OnReload on_reload,
|
|
FileSystem::FileWatcherOptions options = {})
|
|
{
|
|
return ConfigWatch{std::move(path),
|
|
[](const std::filesystem::path& p) {
|
|
return Config::from_ini(p);
|
|
},
|
|
std::move(on_reload), options};
|
|
}
|
|
|
|
[[nodiscard]] static ConfigWatch
|
|
json(std::filesystem::path path, OnReload on_reload,
|
|
FileSystem::FileWatcherOptions options = {})
|
|
{
|
|
return ConfigWatch{std::move(path),
|
|
[](const std::filesystem::path& p) {
|
|
return Config::from_json(p);
|
|
},
|
|
std::move(on_reload), options};
|
|
}
|
|
|
|
[[nodiscard]] static ConfigWatch
|
|
key_value(std::filesystem::path path, OnReload on_reload,
|
|
FileSystem::FileWatcherOptions options = {})
|
|
{
|
|
return ConfigWatch{std::move(path),
|
|
[](const std::filesystem::path& p) {
|
|
return Config::from_file(p);
|
|
},
|
|
std::move(on_reload), options};
|
|
}
|
|
|
|
/// The most recently loaded configuration (thread-safe copy).
|
|
[[nodiscard]] Config current() const
|
|
{
|
|
std::lock_guard lock(m_mutex);
|
|
return m_current;
|
|
}
|
|
|
|
/// Force a poll round (mainly for tests).
|
|
void poll_now() { m_watcher.poll_now(); }
|
|
|
|
private:
|
|
|
|
void on_event(const FileSystem::FileWatcher::Event& event)
|
|
{
|
|
if (!event.exists) {
|
|
Void::Log::warn("Configuration.Watch",
|
|
"'{}' disappeared; keeping last config",
|
|
m_path.string());
|
|
return;
|
|
}
|
|
auto loaded = m_loader(m_path);
|
|
if (!loaded.has_value()) {
|
|
Void::Log::warn(
|
|
"Configuration.Watch",
|
|
"reload of '{}' failed: {}; keeping last config",
|
|
m_path.string(), loaded.error().message());
|
|
return;
|
|
}
|
|
|
|
std::vector<std::string> changed;
|
|
Config snapshot;
|
|
{
|
|
std::lock_guard lock(m_mutex);
|
|
changed = diff(m_current, *loaded);
|
|
m_current = std::move(*loaded);
|
|
snapshot = m_current;
|
|
}
|
|
Void::Log::info("Configuration.Watch",
|
|
"reloaded '{}' ({} keys changed)",
|
|
m_path.string(), changed.size());
|
|
if (m_on_reload) {
|
|
m_on_reload(snapshot, changed);
|
|
}
|
|
}
|
|
|
|
[[nodiscard]] static std::vector<std::string>
|
|
diff(const Config& before, const Config& after)
|
|
{
|
|
std::vector<std::string> changed;
|
|
for (const auto& key : after.keys()) {
|
|
const auto old_val = before.get(key);
|
|
if (!old_val.has_value() || *old_val != after.get_or(key)) {
|
|
changed.push_back(key);
|
|
}
|
|
}
|
|
for (const auto& key : before.keys()) {
|
|
if (!after.contains(key)) {
|
|
changed.push_back(key);
|
|
}
|
|
}
|
|
std::sort(changed.begin(), changed.end());
|
|
return changed;
|
|
}
|
|
|
|
std::filesystem::path m_path;
|
|
Loader m_loader;
|
|
OnReload m_on_reload;
|
|
mutable std::mutex m_mutex;
|
|
Config m_current;
|
|
FileSystem::FileWatcher m_watcher; // last: stops first on dtor
|
|
};
|
|
|
|
} // namespace Void::Configuration
|