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>
137 lines
4.1 KiB
C++
137 lines
4.1 KiB
C++
#include <Void/System/FileWatcher.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 <thread>
|
|
|
|
using Void::FileSystem::FileWatcher;
|
|
|
|
namespace
|
|
{
|
|
// Long poll interval so only explicit poll_now() rounds matter.
|
|
FileWatcher::Options manual_poll()
|
|
{
|
|
return {.poll_interval = std::chrono::milliseconds{60'000},
|
|
.debounce_polls = 2};
|
|
}
|
|
|
|
int write_counter = 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());
|
|
// Nudge mtime forward — coarse filesystem timestamps could
|
|
// otherwise hide consecutive writes from the watcher.
|
|
const auto now = std::filesystem::file_time_type::clock::now();
|
|
std::filesystem::last_write_time(
|
|
p, now + std::chrono::milliseconds{(++write_counter) * 10});
|
|
}
|
|
} // namespace
|
|
|
|
Void::Testing::Test watcher_fires_after_debounce()
|
|
{
|
|
Void::FileSystem::TempDir tmp{"void_watch"};
|
|
const auto file = tmp.path() / "config.txt";
|
|
write(file, "v1");
|
|
|
|
std::atomic<int> fired{0};
|
|
FileWatcher watcher{manual_poll()};
|
|
watcher.watch(file, [&](const FileWatcher::Event& e) {
|
|
Void::Testing::expect(e.exists);
|
|
++fired;
|
|
});
|
|
|
|
write(file, "v2-with-different-size");
|
|
watcher.poll_now(); // change observed, stability count = 1
|
|
Void::Testing::expect_eq(fired.load(), 0);
|
|
watcher.poll_now(); // stable for 2 polls -> fires
|
|
Void::Testing::expect_eq(fired.load(), 1);
|
|
watcher.poll_now(); // no further change -> no further events
|
|
Void::Testing::expect_eq(fired.load(), 1);
|
|
}
|
|
|
|
Void::Testing::Test watcher_debounce_restarts_while_changing()
|
|
{
|
|
Void::FileSystem::TempDir tmp{"void_watch"};
|
|
const auto file = tmp.path() / "config.txt";
|
|
write(file, "v1");
|
|
|
|
std::atomic<int> fired{0};
|
|
FileWatcher watcher{manual_poll()};
|
|
watcher.watch(file, [&](const FileWatcher::Event&) { ++fired; });
|
|
|
|
write(file, "second version");
|
|
watcher.poll_now();
|
|
write(file, "third version, longer again");
|
|
watcher.poll_now(); // still changing -> debounce restarts
|
|
Void::Testing::expect_eq(fired.load(), 0);
|
|
watcher.poll_now();
|
|
Void::Testing::expect_eq(fired.load(), 1);
|
|
}
|
|
|
|
Void::Testing::Test watcher_reports_deletion()
|
|
{
|
|
Void::FileSystem::TempDir tmp{"void_watch"};
|
|
const auto file = tmp.path() / "config.txt";
|
|
write(file, "v1");
|
|
|
|
std::atomic<int> gone{0};
|
|
FileWatcher watcher{manual_poll()};
|
|
watcher.watch(file, [&](const FileWatcher::Event& e) {
|
|
if (!e.exists) {
|
|
++gone;
|
|
}
|
|
});
|
|
|
|
std::filesystem::remove(file);
|
|
watcher.poll_now();
|
|
watcher.poll_now();
|
|
Void::Testing::expect_eq(gone.load(), 1);
|
|
}
|
|
|
|
Void::Testing::Test watcher_unwatch_stops_events()
|
|
{
|
|
Void::FileSystem::TempDir tmp{"void_watch"};
|
|
const auto file = tmp.path() / "config.txt";
|
|
write(file, "v1");
|
|
|
|
std::atomic<int> fired{0};
|
|
FileWatcher watcher{manual_poll()};
|
|
watcher.watch(file, [&](const FileWatcher::Event&) { ++fired; });
|
|
Void::Testing::expect_eq(watcher.watch_count(), std::size_t{1});
|
|
|
|
watcher.unwatch(file);
|
|
Void::Testing::expect_eq(watcher.watch_count(), std::size_t{0});
|
|
|
|
write(file, "changed");
|
|
watcher.poll_now();
|
|
watcher.poll_now();
|
|
Void::Testing::expect_eq(fired.load(), 0);
|
|
}
|
|
|
|
// @tag integration
|
|
Void::Testing::Test watcher_background_thread_fires_on_its_own()
|
|
{
|
|
Void::FileSystem::TempDir tmp{"void_watch"};
|
|
const auto file = tmp.path() / "config.txt";
|
|
write(file, "v1");
|
|
|
|
std::atomic<int> fired{0};
|
|
FileWatcher watcher{
|
|
{.poll_interval = std::chrono::milliseconds{10},
|
|
.debounce_polls = 2}};
|
|
watcher.watch(file, [&](const FileWatcher::Event&) { ++fired; });
|
|
|
|
write(file, "v2 changed contents");
|
|
for (int i = 0; i < 200 && fired.load() == 0; ++i) {
|
|
std::this_thread::sleep_for(std::chrono::milliseconds{5});
|
|
}
|
|
Void::Testing::expect_eq(fired.load(), 1);
|
|
}
|