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>
131 lines
2.9 KiB
C++
131 lines
2.9 KiB
C++
#include <Void/System/FileWatcher.hpp>
|
|
#include <Void/Log/Log.hpp>
|
|
|
|
#include <system_error>
|
|
#include <utility>
|
|
#include <vector>
|
|
|
|
namespace Void::FileSystem
|
|
{
|
|
|
|
FileWatcher::FileWatcher(Options options) : m_options(options)
|
|
{
|
|
m_thread = std::thread([this] { run(); });
|
|
}
|
|
|
|
FileWatcher::~FileWatcher()
|
|
{
|
|
{
|
|
std::lock_guard lock(m_mutex);
|
|
m_stop = true;
|
|
}
|
|
m_cv.notify_all();
|
|
if (m_thread.joinable()) {
|
|
m_thread.join();
|
|
}
|
|
}
|
|
|
|
void FileWatcher::watch(std::filesystem::path path, Callback callback)
|
|
{
|
|
Entry entry;
|
|
entry.callback = std::move(callback);
|
|
entry.accepted = stat(path);
|
|
|
|
std::lock_guard lock(m_mutex);
|
|
Void::Log::debug("System.FileWatcher", "watching '{}'", path.string());
|
|
m_watches.insert_or_assign(std::move(path), std::move(entry));
|
|
}
|
|
|
|
void FileWatcher::unwatch(const std::filesystem::path& path)
|
|
{
|
|
std::lock_guard lock(m_mutex);
|
|
m_watches.erase(path);
|
|
}
|
|
|
|
std::size_t FileWatcher::watch_count() const
|
|
{
|
|
std::lock_guard lock(m_mutex);
|
|
return m_watches.size();
|
|
}
|
|
|
|
void FileWatcher::poll_now() { poll_round(); }
|
|
|
|
FileWatcher::Signature FileWatcher::stat(const std::filesystem::path& path)
|
|
{
|
|
Signature sig;
|
|
std::error_code ec;
|
|
sig.exists = std::filesystem::exists(path, ec) && !ec;
|
|
if (sig.exists) {
|
|
sig.mtime = std::filesystem::last_write_time(path, ec);
|
|
sig.size = std::filesystem::file_size(path, ec);
|
|
if (ec) {
|
|
sig = Signature{}; // treat stat failure as absent
|
|
}
|
|
}
|
|
return sig;
|
|
}
|
|
|
|
void FileWatcher::poll_round()
|
|
{
|
|
// Collect ripe callbacks under the lock, invoke them outside it so
|
|
// a callback may call watch()/unwatch() without deadlocking.
|
|
std::vector<std::pair<Callback, Event>> ripe;
|
|
|
|
{
|
|
std::lock_guard lock(m_mutex);
|
|
for (auto& [path, entry] : m_watches) {
|
|
const Signature now = stat(path);
|
|
|
|
if (!entry.dirty) {
|
|
if (now != entry.accepted) {
|
|
entry.dirty = true;
|
|
entry.pending = now;
|
|
entry.stable_polls = 1;
|
|
}
|
|
continue;
|
|
}
|
|
|
|
if (now != entry.pending) {
|
|
// Still changing — restart the stability count.
|
|
entry.pending = now;
|
|
entry.stable_polls = 1;
|
|
continue;
|
|
}
|
|
|
|
if (++entry.stable_polls >= m_options.debounce_polls) {
|
|
entry.accepted = now;
|
|
entry.dirty = false;
|
|
entry.stable_polls = 0;
|
|
ripe.emplace_back(entry.callback, Event{path, now.exists});
|
|
}
|
|
}
|
|
}
|
|
|
|
for (auto& [callback, event] : ripe) {
|
|
Void::Log::debug("System.FileWatcher", "change on '{}'",
|
|
event.path.string());
|
|
if (callback) {
|
|
callback(event);
|
|
}
|
|
}
|
|
}
|
|
|
|
void FileWatcher::run()
|
|
{
|
|
// Wait first, poll after: nothing is watched at startup, and an
|
|
// eager first round would race callers that add watches right
|
|
// after construction.
|
|
std::unique_lock lock(m_mutex);
|
|
while (!m_stop) {
|
|
if (m_cv.wait_for(lock, m_options.poll_interval,
|
|
[this] { return m_stop; })) {
|
|
return;
|
|
}
|
|
lock.unlock();
|
|
poll_round();
|
|
lock.lock();
|
|
}
|
|
}
|
|
|
|
} // namespace Void::FileSystem
|