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>
127 lines
3.2 KiB
C++
127 lines
3.2 KiB
C++
#pragma once
|
|
|
|
#include <Void/System/Path.hpp>
|
|
#include <Void/Log/Log.hpp>
|
|
|
|
#include <atomic>
|
|
#include <chrono>
|
|
#include <cstdint>
|
|
#include <filesystem>
|
|
#include <format>
|
|
#include <string>
|
|
#include <string_view>
|
|
#include <system_error>
|
|
#include <thread>
|
|
#include <utility>
|
|
|
|
namespace Void::FileSystem
|
|
{
|
|
/// RAII scoped directory under `std::filesystem::temp_directory_path()`.
|
|
///
|
|
/// The directory is created with a unique name on construction and
|
|
/// recursively removed on destruction. Useful in tests that need a
|
|
/// scratch area without manual cleanup.
|
|
///
|
|
/// Construction throws `std::system_error` if the directory cannot
|
|
/// be created. Use `TempDir::open(prefix)` for a non-throwing
|
|
/// `Result`-returning factory.
|
|
class TempDir
|
|
{
|
|
public:
|
|
|
|
explicit TempDir(std::string_view prefix = "void_test_")
|
|
: m_path(make_unique_dir(prefix))
|
|
{
|
|
}
|
|
|
|
~TempDir() noexcept
|
|
{
|
|
if (!m_path.empty()) {
|
|
Void::Log::debug("FileSystem", "TempDir: removed '{}'", m_path.string());
|
|
std::error_code ignore;
|
|
std::filesystem::remove_all(m_path, ignore);
|
|
}
|
|
}
|
|
|
|
TempDir(const TempDir&) = delete;
|
|
TempDir& operator=(const TempDir&) = delete;
|
|
|
|
TempDir(TempDir&& other) noexcept
|
|
: m_path(std::exchange(other.m_path, {}))
|
|
{
|
|
}
|
|
|
|
TempDir& operator=(TempDir&& other) noexcept
|
|
{
|
|
if (this != &other) {
|
|
if (!m_path.empty()) {
|
|
std::error_code ignore;
|
|
std::filesystem::remove_all(m_path, ignore);
|
|
}
|
|
m_path = std::exchange(other.m_path, {});
|
|
}
|
|
return *this;
|
|
}
|
|
|
|
[[nodiscard]] const std::filesystem::path& path() const noexcept
|
|
{
|
|
return m_path;
|
|
}
|
|
|
|
/// Build a path inside the temp dir.
|
|
/// Example: `auto log_path = tmp / "test.log";`
|
|
[[nodiscard]] std::filesystem::path
|
|
operator/(std::string_view relative) const
|
|
{
|
|
return m_path / relative;
|
|
}
|
|
|
|
/// Non-throwing factory.
|
|
[[nodiscard]] static Void::Result::Result<TempDir,
|
|
Void::Result::Error>
|
|
open(std::string_view prefix = "void_test_")
|
|
{
|
|
try {
|
|
return Void::Result::ok(TempDir{prefix});
|
|
} catch (const std::exception& e) {
|
|
return Void::Result::fail(std::string{e.what()});
|
|
}
|
|
}
|
|
|
|
private:
|
|
|
|
std::filesystem::path m_path;
|
|
|
|
static std::filesystem::path
|
|
make_unique_dir(std::string_view prefix)
|
|
{
|
|
static std::atomic<std::uint64_t> counter{0};
|
|
const auto base = std::filesystem::temp_directory_path();
|
|
for (int attempt = 0; attempt < 16; ++attempt) {
|
|
const auto seed =
|
|
static_cast<std::uint64_t>(
|
|
std::chrono::steady_clock::now()
|
|
.time_since_epoch()
|
|
.count()) ^
|
|
(counter.fetch_add(1, std::memory_order_relaxed)
|
|
<< 16) ^
|
|
static_cast<std::uint64_t>(std::hash<std::thread::id>{}(
|
|
std::this_thread::get_id()));
|
|
auto candidate =
|
|
base / std::format("{}{:016x}", prefix, seed);
|
|
std::error_code ec;
|
|
if (std::filesystem::create_directory(candidate, ec)) {
|
|
Void::Log::debug("FileSystem", "TempDir: created '{}'",
|
|
candidate.string());
|
|
return candidate;
|
|
}
|
|
}
|
|
throw std::system_error(
|
|
std::make_error_code(std::errc::file_exists),
|
|
"TempDir: failed to create unique directory after 16 "
|
|
"attempts");
|
|
}
|
|
};
|
|
|
|
} // namespace Void::FileSystem
|