Files
Library/tests/standalone/Configuration/Watch.cpp
T
faumarayandClaude Fable 5 c74a02a5a5 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>
2026-06-13 00:11:13 +03:00

117 lines
3.4 KiB
C++

#include <Void/Configuration/Watch.hpp>
#include <Void/System/Path.hpp>
#include <Void/System/TempDir.hpp>
#include <Void/Testing/Assert.hpp>
#include <Void/Testing/Test.hpp>
#include <atomic>
#include <chrono>
#include <filesystem>
#include <mutex>
#include <string>
#include <vector>
using Void::Configuration::Config;
using Void::Configuration::ConfigWatch;
namespace
{
Void::FileSystem::FileWatcherOptions manual_poll()
{
return {.poll_interval = std::chrono::milliseconds{60'000},
.debounce_polls = 2};
}
int bump = 0;
void write(const std::filesystem::path& p, std::string_view text)
{
auto r = Void::FileSystem::write_file(p, text);
Void::Testing::expect(r.has_value());
const auto now = std::filesystem::file_time_type::clock::now();
std::filesystem::last_write_time(
p, now + std::chrono::milliseconds{(++bump) * 10});
}
} // namespace
Void::Testing::Test config_watch_reloads_and_diffs()
{
Void::FileSystem::TempDir tmp{"void_cfgwatch"};
const auto file = tmp.path() / "app.ini";
write(file, "[log]\nlevel = info\nkeep = 1\n");
std::mutex mu;
std::vector<std::string> last_changed;
std::string last_level;
auto watch = ConfigWatch::ini(
file,
[&](const Config& cfg, const std::vector<std::string>& changed) {
std::lock_guard lock(mu);
last_changed = changed;
last_level = cfg.get_or("log.level");
},
manual_poll());
Void::Testing::expect(watch.current().get_or("log.level") ==
"info");
write(file, "[log]\nlevel = debug\nkeep = 1\nadded = x\n");
watch.poll_now();
watch.poll_now(); // debounce satisfied -> reload + callback
std::lock_guard lock(mu);
Void::Testing::expect_eq(last_level, std::string{"debug"});
Void::Testing::expect_eq(last_changed.size(), std::size_t{2});
Void::Testing::expect_eq(last_changed[0], std::string{"log.added"});
Void::Testing::expect_eq(last_changed[1], std::string{"log.level"});
Void::Testing::expect(watch.current().get_or("log.level") ==
"debug");
}
Void::Testing::Test config_watch_keeps_old_config_on_parse_error()
{
Void::FileSystem::TempDir tmp{"void_cfgwatch"};
const auto file = tmp.path() / "app.ini";
write(file, "[log]\nlevel = info\n");
std::atomic<int> reloads{0};
auto watch = ConfigWatch::ini(
file,
[&](const Config&, const std::vector<std::string>&) {
++reloads;
},
manual_poll());
write(file, "[broken\nlevel=zzz\n"); // malformed section
watch.poll_now();
watch.poll_now();
Void::Testing::expect_eq(reloads.load(), 0);
Void::Testing::expect(watch.current().get_or("log.level") ==
"info");
}
Void::Testing::Test config_watch_deletion_keeps_last_config()
{
Void::FileSystem::TempDir tmp{"void_cfgwatch"};
const auto file = tmp.path() / "app.ini";
write(file, "[log]\nlevel = info\n");
std::atomic<int> reloads{0};
auto watch = ConfigWatch::ini(
file,
[&](const Config&, const std::vector<std::string>&) {
++reloads;
},
manual_poll());
std::filesystem::remove(file);
watch.poll_now();
watch.poll_now();
Void::Testing::expect_eq(reloads.load(), 0);
Void::Testing::expect(watch.current().get_or("log.level") ==
"info");
}