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>
125 lines
3.9 KiB
C++
125 lines
3.9 KiB
C++
#pragma once
|
|
|
|
#include <Void/Log/Log.hpp>
|
|
#include <Void/Functional/Error.hpp>
|
|
#include <Void/Functional/Result.hpp>
|
|
|
|
#include <filesystem>
|
|
#include <format>
|
|
#include <system_error>
|
|
#include <vector>
|
|
|
|
namespace Void::FileSystem
|
|
{
|
|
|
|
/// Return true if path exists (file, directory, or symlink).
|
|
[[nodiscard]] inline bool exists(const std::filesystem::path& path) noexcept
|
|
{
|
|
std::error_code ec;
|
|
return std::filesystem::exists(path, ec);
|
|
}
|
|
|
|
/// Return the size of a regular file in bytes.
|
|
[[nodiscard]] inline Void::Result::Result<std::uintmax_t, Void::Result::Error>
|
|
file_size(const std::filesystem::path& path)
|
|
{
|
|
std::error_code ec;
|
|
auto sz = std::filesystem::file_size(path, ec);
|
|
if (ec) {
|
|
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()));
|
|
}
|
|
return Void::Result::ok(sz);
|
|
}
|
|
|
|
/// Return the last-write time of a path.
|
|
[[nodiscard]] inline Void::Result::
|
|
Result<std::filesystem::file_time_type, Void::Result::Error>
|
|
last_modified(const std::filesystem::path& path)
|
|
{
|
|
std::error_code ec;
|
|
auto t = std::filesystem::last_write_time(path, ec);
|
|
if (ec) {
|
|
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()));
|
|
}
|
|
return Void::Result::ok(t);
|
|
}
|
|
|
|
/// List all entries in a directory (non-recursive, filesystem order).
|
|
[[nodiscard]] inline Void::Result::
|
|
Result<std::vector<std::filesystem::path>, Void::Result::Error>
|
|
list_directory(const std::filesystem::path& path)
|
|
{
|
|
std::error_code ec;
|
|
std::vector<std::filesystem::path> entries;
|
|
for (const auto& entry :
|
|
std::filesystem::directory_iterator{path, ec}) {
|
|
if (ec) {
|
|
break;
|
|
}
|
|
entries.push_back(entry.path());
|
|
}
|
|
if (ec) {
|
|
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::debug("FileSystem", "list_directory: '{}' ({} entries)",
|
|
path.string(), entries.size());
|
|
return Void::Result::ok(std::move(entries));
|
|
}
|
|
|
|
/// Copy a single file. Set overwrite=true to allow replacing an existing dst.
|
|
[[nodiscard]] inline Void::Result::Result<void, Void::Result::Error>
|
|
copy_file(const std::filesystem::path& src,
|
|
const std::filesystem::path& dst,
|
|
bool overwrite = false)
|
|
{
|
|
std::error_code ec;
|
|
auto opts = overwrite ? std::filesystem::copy_options::overwrite_existing
|
|
: std::filesystem::copy_options::none;
|
|
std::filesystem::copy_file(src, dst, opts, ec);
|
|
if (ec) {
|
|
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::debug("FileSystem", "copy_file: '{}' -> '{}'", src.string(),
|
|
dst.string());
|
|
return {};
|
|
}
|
|
|
|
/// Recursively copy a directory tree from src to dst.
|
|
/// dst must not already exist; its parent must be accessible.
|
|
[[nodiscard]] inline Void::Result::Result<void, Void::Result::Error>
|
|
copy_dir(const std::filesystem::path& src,
|
|
const std::filesystem::path& dst)
|
|
{
|
|
std::error_code ec;
|
|
std::filesystem::copy(src, dst, std::filesystem::copy_options::recursive,
|
|
ec);
|
|
if (ec) {
|
|
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::debug("FileSystem", "copy_dir: '{}' -> '{}'", src.string(),
|
|
dst.string());
|
|
return {};
|
|
}
|
|
|
|
} // namespace Void::FileSystem
|