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,94 @@
|
||||
#pragma once
|
||||
|
||||
#include <chrono>
|
||||
#include <condition_variable>
|
||||
#include <cstdint>
|
||||
#include <filesystem>
|
||||
#include <functional>
|
||||
#include <map>
|
||||
#include <mutex>
|
||||
#include <thread>
|
||||
|
||||
namespace Void::FileSystem
|
||||
{
|
||||
/// Polling file watcher with debounce.
|
||||
///
|
||||
/// One background thread polls every watched path's (mtime, size) at
|
||||
/// `poll_interval`. After a change is observed, the callback fires only
|
||||
/// once the metadata has been stable for `debounce_polls` consecutive
|
||||
/// polls — this avoids reacting to half-written files (e.g. a .so or
|
||||
/// config file still being copied).
|
||||
///
|
||||
/// Callbacks run on the watcher thread; keep them short or hand off.
|
||||
/// A path that disappears is reported once with `exists == false` and
|
||||
/// again when it reappears (after debounce).
|
||||
struct FileWatcherOptions {
|
||||
std::chrono::milliseconds poll_interval{500};
|
||||
int debounce_polls = 2;
|
||||
};
|
||||
|
||||
class FileWatcher
|
||||
{
|
||||
public:
|
||||
|
||||
using Options = FileWatcherOptions;
|
||||
|
||||
struct Event {
|
||||
std::filesystem::path path;
|
||||
bool exists; // false when the file vanished
|
||||
};
|
||||
|
||||
using Callback = std::function<void(const Event&)>;
|
||||
|
||||
explicit FileWatcher(Options options = {});
|
||||
~FileWatcher();
|
||||
|
||||
FileWatcher(const FileWatcher&) = delete;
|
||||
FileWatcher& operator=(const FileWatcher&) = delete;
|
||||
FileWatcher(FileWatcher&&) = delete;
|
||||
FileWatcher& operator=(FileWatcher&&) = delete;
|
||||
|
||||
/// Watch a path. Replaces any existing callback for it.
|
||||
/// Thread-safe.
|
||||
void watch(std::filesystem::path path, Callback callback);
|
||||
|
||||
/// Stop watching a path. Thread-safe.
|
||||
void unwatch(const std::filesystem::path& path);
|
||||
|
||||
[[nodiscard]] std::size_t watch_count() const;
|
||||
|
||||
/// Run one poll round immediately (test hook; also usable to
|
||||
/// force a check without waiting out the interval).
|
||||
void poll_now();
|
||||
|
||||
private:
|
||||
|
||||
struct Signature {
|
||||
bool exists = false;
|
||||
std::filesystem::file_time_type mtime{};
|
||||
std::uintmax_t size = 0;
|
||||
|
||||
bool operator==(const Signature&) const = default;
|
||||
};
|
||||
|
||||
struct Entry {
|
||||
Callback callback;
|
||||
Signature accepted; // last state we reported (or initial)
|
||||
Signature pending; // candidate new state being debounced
|
||||
int stable_polls = 0;
|
||||
bool dirty = false; // pending differs from accepted
|
||||
};
|
||||
|
||||
static Signature stat(const std::filesystem::path& path);
|
||||
void poll_round();
|
||||
void run();
|
||||
|
||||
Options m_options;
|
||||
mutable std::mutex m_mutex;
|
||||
std::condition_variable m_cv;
|
||||
std::map<std::filesystem::path, Entry> m_watches;
|
||||
bool m_stop = false;
|
||||
std::thread m_thread;
|
||||
};
|
||||
|
||||
} // namespace Void::FileSystem
|
||||
@@ -27,25 +27,21 @@ namespace Void::FileSystem
|
||||
{
|
||||
std::ifstream f{path, std::ios::binary};
|
||||
if (!f.is_open()) {
|
||||
Void::Log::log(
|
||||
Void::Log::Level::Warning, "FileSystem",
|
||||
std::format("read_file: cannot open '{}'", path.string()));
|
||||
Void::Log::warn("FileSystem", "read_file: cannot open '{}'", path.string());
|
||||
return Void::Result::fail(std::format(
|
||||
"FileSystem::read_file: cannot open '{}'", path.string()));
|
||||
}
|
||||
std::string content{std::istreambuf_iterator<char>(f),
|
||||
std::istreambuf_iterator<char>()};
|
||||
if (f.bad()) {
|
||||
Void::Log::log(Void::Log::Level::Warning, "FileSystem",
|
||||
std::format("read_file: I/O error reading '{}'",
|
||||
path.string()));
|
||||
Void::Log::warn("FileSystem", "read_file: I/O error reading '{}'",
|
||||
path.string());
|
||||
return Void::Result::fail(
|
||||
std::format("FileSystem::read_file: I/O error reading '{}'",
|
||||
path.string()));
|
||||
}
|
||||
Void::Log::log(Void::Log::Level::Debug, "FileSystem",
|
||||
std::format("read_file: '{}' ({} bytes)", path.string(),
|
||||
content.size()));
|
||||
Void::Log::debug("FileSystem", "read_file: '{}' ({} bytes)", path.string(),
|
||||
content.size());
|
||||
return Void::Result::ok(std::move(content));
|
||||
}
|
||||
|
||||
@@ -58,18 +54,15 @@ namespace Void::FileSystem
|
||||
{
|
||||
std::ofstream f{path, std::ios::binary | std::ios::trunc};
|
||||
if (!f.is_open()) {
|
||||
Void::Log::log(
|
||||
Void::Log::Level::Warning, "FileSystem",
|
||||
std::format("write_file: cannot open '{}'", path.string()));
|
||||
Void::Log::warn("FileSystem", "write_file: cannot open '{}'", path.string());
|
||||
return Void::Result::fail(std::format(
|
||||
"FileSystem::write_file: cannot open '{}'", path.string()));
|
||||
}
|
||||
f.write(content.data(), static_cast<std::streamsize>(content.size()));
|
||||
f.flush();
|
||||
if (!f.good()) {
|
||||
Void::Log::log(Void::Log::Level::Warning, "FileSystem",
|
||||
std::format("write_file: I/O error writing '{}'",
|
||||
path.string()));
|
||||
Void::Log::warn("FileSystem", "write_file: I/O error writing '{}'",
|
||||
path.string());
|
||||
return Void::Result::fail(
|
||||
std::format("FileSystem::write_file: I/O error writing '{}'",
|
||||
path.string()));
|
||||
@@ -99,17 +92,14 @@ namespace Void::FileSystem
|
||||
if (ec) {
|
||||
std::error_code ignore;
|
||||
std::filesystem::remove(staging, ignore);
|
||||
Void::Log::log(
|
||||
Void::Log::Level::Warning, "FileSystem",
|
||||
std::format("atomic_write: rename failed for '{}': {}",
|
||||
path.string(), ec.message()));
|
||||
Void::Log::warn("FileSystem", "atomic_write: rename failed for '{}': {}",
|
||||
path.string(), ec.message());
|
||||
return Void::Result::fail(std::format(
|
||||
"FileSystem::atomic_write: rename failed for '{}': {}",
|
||||
path.string(), ec.message()));
|
||||
}
|
||||
Void::Log::log(Void::Log::Level::Debug, "FileSystem",
|
||||
std::format("atomic_write: '{}' committed ({} bytes)",
|
||||
path.string(), content.size()));
|
||||
Void::Log::debug("FileSystem", "atomic_write: '{}' committed ({} bytes)",
|
||||
path.string(), content.size());
|
||||
return {};
|
||||
}
|
||||
|
||||
@@ -124,17 +114,13 @@ namespace Void::FileSystem
|
||||
}
|
||||
std::filesystem::create_directories(path, ec);
|
||||
if (ec) {
|
||||
Void::Log::log(
|
||||
Void::Log::Level::Warning, "FileSystem",
|
||||
std::format("ensure_directory: cannot create '{}': {}",
|
||||
path.string(), ec.message()));
|
||||
Void::Log::warn("FileSystem", "ensure_directory: cannot create '{}': {}",
|
||||
path.string(), ec.message());
|
||||
return Void::Result::fail(std::format(
|
||||
"FileSystem::ensure_directory: cannot create '{}': {}",
|
||||
path.string(), ec.message()));
|
||||
}
|
||||
Void::Log::log(
|
||||
Void::Log::Level::Debug, "FileSystem",
|
||||
std::format("ensure_directory: created '{}'", path.string()));
|
||||
Void::Log::debug("FileSystem", "ensure_directory: created '{}'", path.string());
|
||||
return {};
|
||||
}
|
||||
|
||||
@@ -146,9 +132,8 @@ namespace Void::FileSystem
|
||||
std::error_code ec;
|
||||
std::filesystem::remove_all(path, ec);
|
||||
if (ec) {
|
||||
Void::Log::log(Void::Log::Level::Warning, "FileSystem",
|
||||
std::format("remove_path: cannot remove '{}': {}",
|
||||
path.string(), ec.message()));
|
||||
Void::Log::warn("FileSystem", "remove_path: cannot remove '{}': {}",
|
||||
path.string(), ec.message());
|
||||
return Void::Result::fail(
|
||||
std::format("FileSystem::remove_path: cannot remove '{}': {}",
|
||||
path.string(), ec.message()));
|
||||
|
||||
@@ -26,10 +26,8 @@ namespace Void::FileSystem
|
||||
std::error_code ec;
|
||||
auto sz = std::filesystem::file_size(path, ec);
|
||||
if (ec) {
|
||||
Void::Log::log(
|
||||
Void::Log::Level::Warning, "FileSystem",
|
||||
std::format("file_size: cannot stat '{}': {}", path.string(),
|
||||
ec.message()));
|
||||
Void::Log::warn("FileSystem", "file_size: cannot stat '{}': {}", path.string(),
|
||||
ec.message());
|
||||
return Void::Result::fail(
|
||||
std::format("FileSystem::file_size: cannot stat '{}': {}",
|
||||
path.string(), ec.message()));
|
||||
@@ -45,10 +43,8 @@ namespace Void::FileSystem
|
||||
std::error_code ec;
|
||||
auto t = std::filesystem::last_write_time(path, ec);
|
||||
if (ec) {
|
||||
Void::Log::log(
|
||||
Void::Log::Level::Warning, "FileSystem",
|
||||
std::format("last_modified: cannot stat '{}': {}", path.string(),
|
||||
ec.message()));
|
||||
Void::Log::warn("FileSystem", "last_modified: cannot stat '{}': {}", path.string(),
|
||||
ec.message());
|
||||
return Void::Result::fail(
|
||||
std::format("FileSystem::last_modified: cannot stat '{}': {}",
|
||||
path.string(), ec.message()));
|
||||
@@ -71,17 +67,14 @@ namespace Void::FileSystem
|
||||
entries.push_back(entry.path());
|
||||
}
|
||||
if (ec) {
|
||||
Void::Log::log(
|
||||
Void::Log::Level::Warning, "FileSystem",
|
||||
std::format("list_directory: cannot iterate '{}': {}",
|
||||
path.string(), ec.message()));
|
||||
Void::Log::warn("FileSystem", "list_directory: cannot iterate '{}': {}",
|
||||
path.string(), ec.message());
|
||||
return Void::Result::fail(
|
||||
std::format("FileSystem::list_directory: cannot iterate '{}': {}",
|
||||
path.string(), ec.message()));
|
||||
}
|
||||
Void::Log::log(Void::Log::Level::Debug, "FileSystem",
|
||||
std::format("list_directory: '{}' ({} entries)",
|
||||
path.string(), entries.size()));
|
||||
Void::Log::debug("FileSystem", "list_directory: '{}' ({} entries)",
|
||||
path.string(), entries.size());
|
||||
return Void::Result::ok(std::move(entries));
|
||||
}
|
||||
|
||||
@@ -96,17 +89,14 @@ namespace Void::FileSystem
|
||||
: std::filesystem::copy_options::none;
|
||||
std::filesystem::copy_file(src, dst, opts, ec);
|
||||
if (ec) {
|
||||
Void::Log::log(
|
||||
Void::Log::Level::Warning, "FileSystem",
|
||||
std::format("copy_file: '{}' -> '{}': {}", src.string(),
|
||||
dst.string(), ec.message()));
|
||||
Void::Log::warn("FileSystem", "copy_file: '{}' -> '{}': {}", src.string(),
|
||||
dst.string(), ec.message());
|
||||
return Void::Result::fail(
|
||||
std::format("FileSystem::copy_file: '{}' -> '{}': {}",
|
||||
src.string(), dst.string(), ec.message()));
|
||||
}
|
||||
Void::Log::log(Void::Log::Level::Debug, "FileSystem",
|
||||
std::format("copy_file: '{}' -> '{}'", src.string(),
|
||||
dst.string()));
|
||||
Void::Log::debug("FileSystem", "copy_file: '{}' -> '{}'", src.string(),
|
||||
dst.string());
|
||||
return {};
|
||||
}
|
||||
|
||||
@@ -120,17 +110,14 @@ namespace Void::FileSystem
|
||||
std::filesystem::copy(src, dst, std::filesystem::copy_options::recursive,
|
||||
ec);
|
||||
if (ec) {
|
||||
Void::Log::log(
|
||||
Void::Log::Level::Warning, "FileSystem",
|
||||
std::format("copy_dir: '{}' -> '{}': {}", src.string(),
|
||||
dst.string(), ec.message()));
|
||||
Void::Log::warn("FileSystem", "copy_dir: '{}' -> '{}': {}", src.string(),
|
||||
dst.string(), ec.message());
|
||||
return Void::Result::fail(
|
||||
std::format("FileSystem::copy_dir: '{}' -> '{}': {}",
|
||||
src.string(), dst.string(), ec.message()));
|
||||
}
|
||||
Void::Log::log(Void::Log::Level::Debug, "FileSystem",
|
||||
std::format("copy_dir: '{}' -> '{}'", src.string(),
|
||||
dst.string()));
|
||||
Void::Log::debug("FileSystem", "copy_dir: '{}' -> '{}'", src.string(),
|
||||
dst.string());
|
||||
return {};
|
||||
}
|
||||
|
||||
|
||||
@@ -37,9 +37,7 @@ namespace Void::FileSystem
|
||||
~TempDir() noexcept
|
||||
{
|
||||
if (!m_path.empty()) {
|
||||
Void::Log::log(
|
||||
Void::Log::Level::Debug, "FileSystem",
|
||||
std::format("TempDir: removed '{}'", m_path.string()));
|
||||
Void::Log::debug("FileSystem", "TempDir: removed '{}'", m_path.string());
|
||||
std::error_code ignore;
|
||||
std::filesystem::remove_all(m_path, ignore);
|
||||
}
|
||||
@@ -113,9 +111,8 @@ namespace Void::FileSystem
|
||||
base / std::format("{}{:016x}", prefix, seed);
|
||||
std::error_code ec;
|
||||
if (std::filesystem::create_directory(candidate, ec)) {
|
||||
Void::Log::log(Void::Log::Level::Debug, "FileSystem",
|
||||
std::format("TempDir: created '{}'",
|
||||
candidate.string()));
|
||||
Void::Log::debug("FileSystem", "TempDir: created '{}'",
|
||||
candidate.string());
|
||||
return candidate;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user