Restructure into umbrella modules (~55 -> 16 targets)
Headers move, CMake targets merge, C++ namespaces stay unchanged: - Core: Scope, Lazy, Counted, Flags, Hash, Bits, Uuid, TypeList, TypeMap, Variant - Functional: Pipeline, Either, Result, Retry - Containers: Ring, Pool, PriorityQueue, LRU, Trie, Graph, DisjointSet, Bitset, Interval - Math: +Matrix, Numeric, Random, Histogram - Text: Str, Format, Tokenizer, Diff, Encoding, Semver - Data: Json, Csv, Ini - Async: Coro, Concurrency, EventBus, Observable - Time: +Bench - Log: Log facade + Logging framework (facade now header-only, breaking the Stacktrace<->Log link cycle the merge would have created) - System: FileSystem + Process - Configuration: +Argparse - Unchanged: Stacktrace, Plugin, StateMachine, Testing, TestingMock, Detail Module system fixes: - OPTIONAL_DEPS resolution deferred to void_finalize_modules() so it no longer depends on the alphabetical discovery order of src/ subdirs (Testing -> Text would have been silently dropped). - Every header-only module now has a tracked module.cmake; previously the empty src/<Name> marker dirs were untracked, so a fresh clone would have silently skipped those modules. - VOID_HAS_LOGGING/CONCURRENCY/STR consumers renamed to LOG/ASYNC/TEXT. Tests move to tests/standalone/<Umbrella>/<Feature>.cpp (Result suite files prefixed: ResultBasic etc.). Test count verified identical (831) before and after. README/CLAUDE.md module tables updated. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
1 parent
da2e93c405
commit
28b94883fd
156 files changed
+278
-220
No files matched your search
@@ -0,0 +1,96 @@
|
||||
#include <Void/Detail/Enum.hpp>
|
||||
#include <Void/Log/ConsoleLogger.hpp>
|
||||
|
||||
#ifdef VOID_HAS_STACKTRACE
|
||||
#include <Void/Stacktrace/Color.hpp>
|
||||
#endif
|
||||
|
||||
#include <format>
|
||||
#include <iostream>
|
||||
#include <ostream>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
|
||||
namespace Void::Logging
|
||||
{
|
||||
|
||||
namespace
|
||||
{
|
||||
#ifdef VOID_HAS_STACKTRACE
|
||||
std::string_view color_for(Level level)
|
||||
{
|
||||
using namespace Void::Stacktrace::Color;
|
||||
#ifdef VOID_HAS_ENUM_REFLECTION
|
||||
static constexpr auto enumerators =
|
||||
std::define_static_array(std::meta::enumerators_of(^^Level));
|
||||
template for (constexpr auto e : enumerators) {
|
||||
if (level == [:e:]) {
|
||||
if constexpr (std::meta::identifier_of(e) == "Trace")
|
||||
return dim;
|
||||
if constexpr (std::meta::identifier_of(e) == "Debug")
|
||||
return cyan;
|
||||
if constexpr (std::meta::identifier_of(e) == "Info")
|
||||
return bold_cyan;
|
||||
if constexpr (std::meta::identifier_of(e) == "Warning")
|
||||
return bold_yellow;
|
||||
if constexpr (std::meta::identifier_of(e) == "Error")
|
||||
return bold_red;
|
||||
if constexpr (std::meta::identifier_of(e) == "Fatal")
|
||||
return bold_magenta;
|
||||
return reset;
|
||||
}
|
||||
}
|
||||
return reset;
|
||||
#else
|
||||
switch (level) {
|
||||
case Level::Trace:
|
||||
return dim;
|
||||
case Level::Debug:
|
||||
return cyan;
|
||||
case Level::Info:
|
||||
return bold_cyan;
|
||||
case Level::Warning:
|
||||
return bold_yellow;
|
||||
case Level::Error:
|
||||
return bold_red;
|
||||
case Level::Fatal:
|
||||
return bold_magenta;
|
||||
default:
|
||||
return reset;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
#endif
|
||||
|
||||
std::string format_entry(const Entry& entry)
|
||||
{
|
||||
auto level_str = std::string{to_string(entry.level)};
|
||||
#ifdef VOID_HAS_STACKTRACE
|
||||
using namespace Void::Stacktrace::Color;
|
||||
return std::format("[{}] {}\n",
|
||||
apply(color_for(entry.level), level_str),
|
||||
entry.message);
|
||||
#else
|
||||
return std::format("[{}] {}\n", level_str, entry.message);
|
||||
#endif
|
||||
}
|
||||
} // namespace
|
||||
|
||||
ConsoleLogger::ConsoleLogger(std::ostream& out,
|
||||
Mode mode,
|
||||
std::size_t capacity)
|
||||
: Logger(mode, capacity), m_out(out)
|
||||
{
|
||||
}
|
||||
|
||||
ConsoleLogger::ConsoleLogger() : ConsoleLogger(std::cerr) {}
|
||||
|
||||
ConsoleLogger::~ConsoleLogger() noexcept { flush(); }
|
||||
|
||||
void ConsoleLogger::write(const Entry& entry)
|
||||
{
|
||||
m_out << format_entry(entry);
|
||||
m_out.flush();
|
||||
}
|
||||
|
||||
} // namespace Void::Logging
|
||||
Whitespace-only changes.
@@ -0,0 +1,62 @@
|
||||
#include <Void/Log/FileLogger.hpp>
|
||||
#include <Void/Log/Log.hpp>
|
||||
|
||||
#include <format>
|
||||
#include <stdexcept>
|
||||
|
||||
namespace Void::Logging
|
||||
{
|
||||
|
||||
FileLogger::FileLogger(const std::filesystem::path& path,
|
||||
Mode mode,
|
||||
std::size_t capacity)
|
||||
: Logger(mode, capacity), m_path(path), m_file(path, std::ios::app)
|
||||
{
|
||||
if (!m_file.is_open()) {
|
||||
Void::Log::log(
|
||||
Void::Log::Level::Warning, "Logging",
|
||||
std::format("FileLogger: cannot open '{}'", path.string()));
|
||||
throw std::runtime_error(
|
||||
std::format("FileLogger: cannot open '{}'", path.string()));
|
||||
}
|
||||
Void::Log::log(Void::Log::Level::Debug, "Logging",
|
||||
std::format("FileLogger: opened '{}'", path.string()));
|
||||
}
|
||||
|
||||
Void::Result::Result<std::unique_ptr<FileLogger>, Void::Result::Error>
|
||||
FileLogger::open(const std::filesystem::path& path,
|
||||
Mode mode,
|
||||
std::size_t capacity)
|
||||
{
|
||||
try {
|
||||
return Void::Result::ok(
|
||||
std::make_unique<FileLogger>(path, mode, capacity));
|
||||
} catch (const std::exception& e) {
|
||||
return Void::Result::fail(std::string{e.what()});
|
||||
}
|
||||
}
|
||||
|
||||
FileLogger::~FileLogger() noexcept
|
||||
{
|
||||
flush();
|
||||
Void::Log::log(Void::Log::Level::Debug, "Logging",
|
||||
std::format("FileLogger: closed '{}'",
|
||||
m_path.string()));
|
||||
}
|
||||
|
||||
void FileLogger::write(const Entry& entry)
|
||||
{
|
||||
m_file << std::format(
|
||||
"[{}] {}\n", to_string(entry.level), entry.message);
|
||||
m_file.flush();
|
||||
}
|
||||
|
||||
void FileLogger::on_flush(std::span<const Entry> entries)
|
||||
{
|
||||
for (const auto& e : entries) {
|
||||
m_file << std::format("[{}] {}\n", to_string(e.level), e.message);
|
||||
}
|
||||
m_file.flush();
|
||||
}
|
||||
|
||||
} // namespace Void::Logging
|
||||
@@ -0,0 +1,10 @@
|
||||
#include <Void/Log/Level.hpp>
|
||||
#include <string_view>
|
||||
namespace Void::Logging
|
||||
{
|
||||
} // namespace Void::Logging
|
||||
|
||||
constexpr std::string_view to_string(Void::Logging::Level level)
|
||||
{
|
||||
return Void::Logging::to_string(level);
|
||||
};
|
||||
@@ -1,28 +0,0 @@
|
||||
#include <Void/Log/Log.hpp>
|
||||
#include <mutex>
|
||||
#include <shared_mutex>
|
||||
|
||||
namespace Void::Log
|
||||
{
|
||||
|
||||
namespace
|
||||
{
|
||||
Sink g_sink;
|
||||
std::shared_mutex g_mutex;
|
||||
} // namespace
|
||||
|
||||
void set_sink(Sink sink)
|
||||
{
|
||||
std::unique_lock lock{g_mutex};
|
||||
g_sink = std::move(sink);
|
||||
}
|
||||
|
||||
void log(Level level, std::string_view module, std::string_view message)
|
||||
{
|
||||
std::shared_lock lock{g_mutex};
|
||||
if (g_sink) {
|
||||
g_sink(level, module, message);
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace Void::Log
|
||||
@@ -0,0 +1,317 @@
|
||||
#include <Void/Log/Logger.hpp>
|
||||
|
||||
#include <utility> // std::exchange
|
||||
|
||||
namespace Void::Logging
|
||||
{
|
||||
|
||||
// =============================================================================
|
||||
// Thread-local definition
|
||||
// =============================================================================
|
||||
|
||||
thread_local std::vector<std::string> Logger::s_scope_stack;
|
||||
|
||||
// =============================================================================
|
||||
// ScopedContext
|
||||
// =============================================================================
|
||||
|
||||
Logger::ScopedContext::ScopedContext(Logger& owner,
|
||||
std::string name,
|
||||
Level level,
|
||||
std::source_location loc) noexcept
|
||||
: m_owner(&owner), m_name(std::move(name)), m_level(level)
|
||||
{
|
||||
// push_back can technically throw; if it does we mark inactive so
|
||||
// the destructor does not attempt a mismatched pop or spurious exit
|
||||
// entry.
|
||||
try {
|
||||
s_scope_stack.push_back(m_name);
|
||||
} catch (...) {
|
||||
m_active = false;
|
||||
return;
|
||||
}
|
||||
// Emit the ScopeEnter entry after the push so Entry::scopes already
|
||||
// contains the new scope (innermost = last).
|
||||
owner.emit_scope_boundary(Kind::ScopeEnter, m_name, m_level, loc);
|
||||
}
|
||||
|
||||
Logger::ScopedContext::~ScopedContext() noexcept
|
||||
{
|
||||
if (!m_active || m_owner == nullptr) {
|
||||
return;
|
||||
}
|
||||
// Emit the exit entry while the scope is still on the stack so that
|
||||
// Entry::scopes reflects the stack *including* this scope, matching the
|
||||
// ScopeEnter entry.
|
||||
m_owner->emit_scope_boundary(
|
||||
Kind::ScopeExit,
|
||||
m_name,
|
||||
m_level,
|
||||
std::source_location{}); // no meaningful caller site
|
||||
|
||||
if (!s_scope_stack.empty()) {
|
||||
s_scope_stack.pop_back();
|
||||
}
|
||||
}
|
||||
|
||||
Logger::ScopedContext::ScopedContext(ScopedContext&& other) noexcept
|
||||
: m_owner(std::exchange(other.m_owner, nullptr)),
|
||||
m_name(std::move(other.m_name)), m_level(other.m_level),
|
||||
m_active(std::exchange(other.m_active, false))
|
||||
{
|
||||
}
|
||||
|
||||
// =============================================================================
|
||||
// Logger — lifetime
|
||||
// =============================================================================
|
||||
|
||||
Logger::Logger(Mode mode, std::size_t capacity) noexcept
|
||||
: m_mode(mode), m_buffer_capacity(capacity)
|
||||
{
|
||||
if (m_mode == Mode::Buffered) {
|
||||
m_buffer.reserve(m_buffer_capacity);
|
||||
}
|
||||
}
|
||||
|
||||
// =============================================================================
|
||||
// Configuration
|
||||
// =============================================================================
|
||||
|
||||
void Logger::set_level(Level level) noexcept
|
||||
{
|
||||
std::lock_guard lock(m_mutex);
|
||||
m_level = level;
|
||||
}
|
||||
|
||||
Level Logger::level() const noexcept
|
||||
{
|
||||
std::lock_guard lock(m_mutex);
|
||||
return m_level;
|
||||
}
|
||||
|
||||
void Logger::set_mode(Mode mode) noexcept
|
||||
{
|
||||
std::vector<Entry> to_flush;
|
||||
{
|
||||
std::lock_guard lock(m_mutex);
|
||||
if (m_mode == mode) {
|
||||
return;
|
||||
}
|
||||
// Flush any buffered entries before switching modes.
|
||||
to_flush = std::move(m_buffer);
|
||||
m_buffer = {};
|
||||
m_mode = mode;
|
||||
|
||||
if (m_mode == Mode::Buffered) {
|
||||
m_buffer.reserve(m_buffer_capacity);
|
||||
}
|
||||
}
|
||||
if (!to_flush.empty()) {
|
||||
on_flush(to_flush); // called outside the lock
|
||||
}
|
||||
}
|
||||
|
||||
Mode Logger::mode() const noexcept
|
||||
{
|
||||
std::lock_guard lock(m_mutex);
|
||||
return m_mode;
|
||||
}
|
||||
|
||||
void Logger::set_buffer_capacity(std::size_t n) noexcept
|
||||
{
|
||||
std::lock_guard lock(m_mutex);
|
||||
m_buffer_capacity = n;
|
||||
}
|
||||
|
||||
std::size_t Logger::buffer_capacity() const noexcept
|
||||
{
|
||||
std::lock_guard lock(m_mutex);
|
||||
return m_buffer_capacity;
|
||||
}
|
||||
|
||||
// =============================================================================
|
||||
// Scoped context
|
||||
// =============================================================================
|
||||
|
||||
Logger::ScopedContext Logger::push_scope(std::string name,
|
||||
Level level,
|
||||
std::source_location loc) noexcept
|
||||
{
|
||||
return ScopedContext(*this, std::move(name), level, loc);
|
||||
}
|
||||
|
||||
void Logger::scope(std::string_view name,
|
||||
Level level,
|
||||
std::source_location loc)
|
||||
{
|
||||
// Emit a pair of scope boundary entries back-to-back.
|
||||
// The scope stack is NOT modified — this is the "lightweight" variant.
|
||||
emit_scope_boundary(Kind::ScopeEnter, name, level, loc);
|
||||
emit_scope_boundary(Kind::ScopeExit, name, level,
|
||||
std::source_location{});
|
||||
}
|
||||
|
||||
std::span<const std::string> Logger::current_scopes() noexcept
|
||||
{
|
||||
return s_scope_stack;
|
||||
}
|
||||
|
||||
// =============================================================================
|
||||
// Preset factory
|
||||
// =============================================================================
|
||||
|
||||
Logger::Preset Logger::make_preset(Level level, std::string prefix) noexcept
|
||||
{
|
||||
return Preset(*this, level, std::move(prefix));
|
||||
}
|
||||
|
||||
// =============================================================================
|
||||
// Core logging
|
||||
// =============================================================================
|
||||
|
||||
void Logger::log(Level level, std::string_view message,
|
||||
std::source_location location)
|
||||
{
|
||||
// Early level check outside the lock to avoid allocations on the hot
|
||||
// path.
|
||||
{
|
||||
std::lock_guard lock(m_mutex);
|
||||
if (level < m_level) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
Entry entry{
|
||||
.kind = Kind::Message,
|
||||
.level = level,
|
||||
.message = std::string(message),
|
||||
.timestamp = std::chrono::system_clock::now(),
|
||||
.location = location,
|
||||
.thread_id = std::this_thread::get_id(),
|
||||
.scopes = s_scope_stack, // snapshot — s_scope_stack is thread-local
|
||||
};
|
||||
|
||||
submit(std::move(entry));
|
||||
}
|
||||
|
||||
void Logger::trace(std::string_view msg, std::source_location loc)
|
||||
{
|
||||
log(Level::Trace, msg, loc);
|
||||
}
|
||||
void Logger::debug(std::string_view msg, std::source_location loc)
|
||||
{
|
||||
log(Level::Debug, msg, loc);
|
||||
}
|
||||
void Logger::info(std::string_view msg, std::source_location loc)
|
||||
{
|
||||
log(Level::Info, msg, loc);
|
||||
}
|
||||
void Logger::warning(std::string_view msg, std::source_location loc)
|
||||
{
|
||||
log(Level::Warning, msg, loc);
|
||||
}
|
||||
void Logger::error(std::string_view msg, std::source_location loc)
|
||||
{
|
||||
log(Level::Error, msg, loc);
|
||||
}
|
||||
void Logger::fatal(std::string_view msg, std::source_location loc)
|
||||
{
|
||||
log(Level::Fatal, msg, loc);
|
||||
}
|
||||
|
||||
// =============================================================================
|
||||
// Flush
|
||||
// =============================================================================
|
||||
|
||||
void Logger::flush()
|
||||
{
|
||||
std::vector<Entry> to_flush;
|
||||
{
|
||||
std::lock_guard lock(m_mutex);
|
||||
to_flush = std::move(m_buffer);
|
||||
m_buffer = {};
|
||||
}
|
||||
// Call the virtual method outside the lock so derived classes can
|
||||
// safely do blocking I/O, acquire their own mutexes, etc.
|
||||
if (!to_flush.empty()) {
|
||||
on_flush(to_flush);
|
||||
}
|
||||
}
|
||||
|
||||
void Logger::on_flush(std::span<const Entry> entries)
|
||||
{
|
||||
for (const auto& entry : entries) {
|
||||
write(entry);
|
||||
}
|
||||
}
|
||||
|
||||
// =============================================================================
|
||||
// Private — emit_scope_boundary
|
||||
// =============================================================================
|
||||
|
||||
void Logger::emit_scope_boundary(Kind kind,
|
||||
std::string_view name,
|
||||
Level level,
|
||||
std::source_location loc)
|
||||
{
|
||||
// Respect the configured minimum level for scope entries too.
|
||||
{
|
||||
std::lock_guard lock(m_mutex);
|
||||
if (level < m_level) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
Entry entry{
|
||||
.kind = kind,
|
||||
.level = level,
|
||||
.message = std::string(name),
|
||||
.timestamp = std::chrono::system_clock::now(),
|
||||
.location = loc,
|
||||
.thread_id = std::this_thread::get_id(),
|
||||
.scopes = s_scope_stack, // snapshot at boundary time
|
||||
};
|
||||
|
||||
submit(std::move(entry));
|
||||
}
|
||||
|
||||
// =============================================================================
|
||||
// Private — submit
|
||||
// =============================================================================
|
||||
|
||||
void Logger::submit(Entry entry)
|
||||
{
|
||||
bool write_now = false;
|
||||
std::vector<Entry> batch;
|
||||
|
||||
{
|
||||
std::lock_guard lock(m_mutex);
|
||||
|
||||
// Re-check level under the lock; set_level() may have raced.
|
||||
if (entry.level < m_level) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (m_mode == Mode::Immediate) {
|
||||
write_now = true;
|
||||
// entry intentionally NOT moved — used below outside the lock.
|
||||
} else {
|
||||
m_buffer.push_back(std::move(entry));
|
||||
|
||||
if (m_buffer.size() >= m_buffer_capacity) {
|
||||
batch = std::move(m_buffer);
|
||||
m_buffer = {};
|
||||
m_buffer.reserve(m_buffer_capacity);
|
||||
}
|
||||
}
|
||||
} // ← mutex released here
|
||||
|
||||
// Virtual calls happen outside the lock (see class documentation).
|
||||
if (write_now) {
|
||||
write(entry); // entry was not moved in this branch
|
||||
} else if (!batch.empty()) {
|
||||
on_flush(batch);
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace Void::Logging
|
||||
@@ -1 +1,4 @@
|
||||
void_add_module(Log)
|
||||
void_add_module(Log
|
||||
INTERNAL_DEPS Functional
|
||||
OPTIONAL_DEPS Stacktrace
|
||||
)
|
||||
Reference in new issue
Block a user