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,45 @@
|
||||
#pragma once
|
||||
|
||||
#include <Void/Log/Logger.hpp>
|
||||
|
||||
#include <iosfwd>
|
||||
|
||||
namespace Void::Logging
|
||||
{
|
||||
/// Logger sink that writes formatted entries to a std::ostream.
|
||||
///
|
||||
/// Defaults to Mode::Immediate (each entry written as soon as it is
|
||||
/// submitted) and to std::cerr if no stream is supplied.
|
||||
///
|
||||
/// When the Stacktrace module is present (VOID_HAS_STACKTRACE), each
|
||||
/// level prefix is colorized via Void::Stacktrace::Color helpers and
|
||||
/// honors Void::Stacktrace::Color::g_color_enabled. Without Stacktrace
|
||||
/// the output is uncolored.
|
||||
class ConsoleLogger final : public Logger
|
||||
{
|
||||
public:
|
||||
|
||||
explicit ConsoleLogger(std::ostream& out,
|
||||
Mode mode = Mode::Immediate,
|
||||
std::size_t capacity = 0);
|
||||
|
||||
/// Convenience constructor — writes to std::cerr in Immediate mode.
|
||||
ConsoleLogger();
|
||||
|
||||
~ConsoleLogger() noexcept override;
|
||||
|
||||
ConsoleLogger(const ConsoleLogger&) = delete;
|
||||
ConsoleLogger& operator=(const ConsoleLogger&) = delete;
|
||||
ConsoleLogger(ConsoleLogger&&) = delete;
|
||||
ConsoleLogger& operator=(ConsoleLogger&&) = delete;
|
||||
|
||||
protected:
|
||||
|
||||
void write(const Entry& entry) override;
|
||||
|
||||
private:
|
||||
|
||||
std::ostream& m_out;
|
||||
};
|
||||
|
||||
} // namespace Void::Logging
|
||||
@@ -0,0 +1,53 @@
|
||||
#pragma once
|
||||
|
||||
#include <Void/Log/Level.hpp>
|
||||
|
||||
#include <chrono>
|
||||
#include <source_location>
|
||||
#include <string>
|
||||
#include <thread>
|
||||
#include <vector>
|
||||
|
||||
namespace Void::Logging
|
||||
{
|
||||
/// Distinguishes regular messages from scope boundary markers.
|
||||
///
|
||||
/// Sinks may use Kind to render scope entries differently —
|
||||
/// e.g. with indentation, brackets, or a dedicated colour.
|
||||
///
|
||||
/// Message — a regular log record
|
||||
/// ScopeEnter — emitted when a named scope opens (scope() call)
|
||||
/// ScopeExit — emitted when a named scope closes (RAII destructor)
|
||||
enum class Kind { Message, ScopeEnter, ScopeExit };
|
||||
|
||||
/// A single, immutable log record produced by Logger and handed to
|
||||
/// write() / on_flush(). All fields are populated by Logger before
|
||||
/// dispatch; derived sinks treat Entry as read-only.
|
||||
struct Entry {
|
||||
Kind kind{Kind::Message};
|
||||
Level level{Level::Debug};
|
||||
|
||||
/// Human-readable text. For ScopeEnter/ScopeExit entries this
|
||||
/// is the scope name.
|
||||
std::string message{};
|
||||
|
||||
/// Wall-clock time at the moment log() / scope() was called.
|
||||
std::chrono::system_clock::time_point timestamp{
|
||||
std::chrono::system_clock::now()};
|
||||
|
||||
/// Caller site captured automatically via
|
||||
/// std::source_location::current().
|
||||
std::source_location location{};
|
||||
|
||||
/// OS thread that produced this entry.
|
||||
std::thread::id thread_id{std::this_thread::get_id()};
|
||||
|
||||
/// Frozen snapshot of the thread-local ScopedContext stack at log
|
||||
/// time. Ordered outermost → innermost (index 0 is the root
|
||||
/// scope). For ScopeEnter this already includes the new scope as
|
||||
/// the last element; for ScopeExit it reflects the stack *before*
|
||||
/// the pop.
|
||||
std::vector<std::string> scopes{};
|
||||
};
|
||||
|
||||
} // namespace Void::Logging
|
||||
@@ -0,0 +1,62 @@
|
||||
#pragma once
|
||||
|
||||
#include <Void/Log/Logger.hpp>
|
||||
#include <Void/Functional/Error.hpp>
|
||||
#include <Void/Functional/Result.hpp>
|
||||
|
||||
#include <filesystem>
|
||||
#include <fstream>
|
||||
#include <memory>
|
||||
#include <span>
|
||||
|
||||
namespace Void::Logging
|
||||
{
|
||||
/// Logger sink that appends formatted entries to a file on disk.
|
||||
///
|
||||
/// Defaults to Mode::Buffered with capacity 64 — entries are batched
|
||||
/// via on_flush() and written using std::ofstream::flush() at the end
|
||||
/// of every batch. The destructor flushes any remaining entries.
|
||||
///
|
||||
/// Two construction paths:
|
||||
/// - `FileLogger::open(path)` returns
|
||||
/// `Result<unique_ptr<FileLogger>, Error>` and never throws —
|
||||
/// prefer this in code that already uses Void::Result for error
|
||||
/// plumbing.
|
||||
/// - The throwing constructor stays for callers that prefer
|
||||
/// exceptions; it throws std::runtime_error if the path cannot
|
||||
/// be opened for append.
|
||||
class FileLogger final : public Logger
|
||||
{
|
||||
public:
|
||||
|
||||
explicit FileLogger(const std::filesystem::path& path,
|
||||
Mode mode = Mode::Buffered,
|
||||
std::size_t capacity = 64);
|
||||
|
||||
~FileLogger() noexcept override;
|
||||
|
||||
FileLogger(const FileLogger&) = delete;
|
||||
FileLogger& operator=(const FileLogger&) = delete;
|
||||
FileLogger(FileLogger&&) = delete;
|
||||
FileLogger& operator=(FileLogger&&) = delete;
|
||||
|
||||
/// Non-throwing factory. On failure returns
|
||||
/// `Failure<Error>{Error{"FileLogger: cannot open '<path>'"}}`.
|
||||
[[nodiscard]] static Void::Result::
|
||||
Result<std::unique_ptr<FileLogger>, Void::Result::Error>
|
||||
open(const std::filesystem::path& path,
|
||||
Mode mode = Mode::Buffered,
|
||||
std::size_t capacity = 64);
|
||||
|
||||
protected:
|
||||
|
||||
void write(const Entry& entry) override;
|
||||
void on_flush(std::span<const Entry> entries) override;
|
||||
|
||||
private:
|
||||
|
||||
std::filesystem::path m_path;
|
||||
std::ofstream m_file;
|
||||
};
|
||||
|
||||
} // namespace Void::Logging
|
||||
@@ -0,0 +1,71 @@
|
||||
#pragma once
|
||||
|
||||
#include <Void/Detail/Enum.hpp>
|
||||
|
||||
#include <format>
|
||||
#include <string_view>
|
||||
#include <utility>
|
||||
|
||||
namespace Void::Logging
|
||||
{
|
||||
enum class Level { Disabled, Trace, Debug, Info, Warning, Error, Fatal };
|
||||
|
||||
[[nodiscard]] constexpr bool operator<(Level a, Level b) noexcept
|
||||
{
|
||||
return std::to_underlying(a) < std::to_underlying(b);
|
||||
}
|
||||
|
||||
[[nodiscard]] constexpr bool operator<=(Level a, Level b) noexcept
|
||||
{
|
||||
return std::to_underlying(a) <= std::to_underlying(b);
|
||||
}
|
||||
|
||||
[[nodiscard]] constexpr bool operator>(Level a, Level b) noexcept
|
||||
{
|
||||
return std::to_underlying(a) > std::to_underlying(b);
|
||||
}
|
||||
|
||||
[[nodiscard]] constexpr bool operator>=(Level a, Level b) noexcept
|
||||
{
|
||||
return std::to_underlying(a) >= std::to_underlying(b);
|
||||
}
|
||||
|
||||
/// Convert a Level to its identifier string via reflection when available,
|
||||
/// falling back to a hand-written table for non-reflecting compilers.
|
||||
[[nodiscard]] constexpr std::string_view to_string(Level level) noexcept
|
||||
{
|
||||
#ifdef VOID_HAS_ENUM_REFLECTION
|
||||
return Void::Detail::enum_name(level);
|
||||
#else
|
||||
switch (level) {
|
||||
case Level::Disabled:
|
||||
return "Disabled";
|
||||
case Level::Trace:
|
||||
return "Trace";
|
||||
case Level::Debug:
|
||||
return "Debug";
|
||||
case Level::Info:
|
||||
return "Info";
|
||||
case Level::Warning:
|
||||
return "Warning";
|
||||
case Level::Error:
|
||||
return "Error";
|
||||
case Level::Fatal:
|
||||
return "Fatal";
|
||||
default:
|
||||
return "?";
|
||||
}
|
||||
#endif
|
||||
}
|
||||
} // namespace Void::Logging
|
||||
|
||||
// ADL-visible free overload so std::format("{}", level) works.
|
||||
template <>
|
||||
struct std::formatter<Void::Logging::Level> : std::formatter<std::string_view> {
|
||||
template <class FormatContext>
|
||||
auto format(Void::Logging::Level level, FormatContext& ctx) const
|
||||
{
|
||||
return std::formatter<std::string_view>::format(
|
||||
Void::Logging::to_string(level), ctx);
|
||||
}
|
||||
};
|
||||
@@ -1,6 +1,9 @@
|
||||
#pragma once
|
||||
#include <functional>
|
||||
#include <mutex>
|
||||
#include <shared_mutex>
|
||||
#include <string_view>
|
||||
#include <utility>
|
||||
|
||||
namespace Void::Log
|
||||
{
|
||||
@@ -21,14 +24,41 @@ namespace Void::Log
|
||||
using Sink = std::function<void(
|
||||
Level level, std::string_view module, std::string_view message)>;
|
||||
|
||||
namespace Detail
|
||||
{
|
||||
struct SinkState {
|
||||
Sink sink;
|
||||
std::shared_mutex mutex;
|
||||
};
|
||||
|
||||
inline SinkState& sink_state()
|
||||
{
|
||||
static SinkState state;
|
||||
return state;
|
||||
}
|
||||
} // namespace Detail
|
||||
|
||||
/// Install the global sink. Any previously installed sink is replaced.
|
||||
/// Pass a default-constructed (empty) Sink to silence all output.
|
||||
/// Thread-safe: the new sink is visible to all threads after this returns.
|
||||
void set_sink(Sink sink);
|
||||
inline void set_sink(Sink sink)
|
||||
{
|
||||
auto& state = Detail::sink_state();
|
||||
std::unique_lock lock{state.mutex};
|
||||
state.sink = std::move(sink);
|
||||
}
|
||||
|
||||
/// Emit one log event through the global sink.
|
||||
/// No-op (zero allocation) if no sink is installed.
|
||||
/// Thread-safe with respect to set_sink() and concurrent log() calls.
|
||||
void log(Level level, std::string_view module, std::string_view message);
|
||||
inline void log(Level level, std::string_view module,
|
||||
std::string_view message)
|
||||
{
|
||||
auto& state = Detail::sink_state();
|
||||
std::shared_lock lock{state.mutex};
|
||||
if (state.sink) {
|
||||
state.sink(level, module, message);
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace Void::Log
|
||||
@@ -0,0 +1,467 @@
|
||||
#pragma once
|
||||
|
||||
#include <Void/Log/Entry.hpp>
|
||||
#include <Void/Log/Level.hpp>
|
||||
|
||||
#include <concepts>
|
||||
#include <cstddef>
|
||||
#include <format>
|
||||
#include <functional>
|
||||
#include <mutex>
|
||||
#include <source_location>
|
||||
#include <span>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <type_traits>
|
||||
#include <vector>
|
||||
|
||||
namespace Void::Logging
|
||||
{
|
||||
enum class Mode { Immediate, Buffered };
|
||||
|
||||
/// Abstract base class for all Void loggers.
|
||||
///
|
||||
/// ## Responsibilities
|
||||
/// - Level filtering (thread-safe)
|
||||
/// - Entry construction: timestamp, thread_id, scope snapshot
|
||||
/// - Immediate vs. Buffered dispatch (auto-flushes when buffer is full)
|
||||
/// - Thread-safe submission — the internal mutex protects only the
|
||||
/// buffer; virtual extension points are called *outside* the lock so
|
||||
/// derived classes may perform blocking I/O or acquire their own
|
||||
/// resources freely.
|
||||
/// - Scoped context via per-thread RAII guards (Logger::ScopedContext)
|
||||
/// - Named scope logging: scope() emits ScopeEnter/ScopeExit entries
|
||||
/// - Logger presets via make_preset() — bake in a level (and optional
|
||||
/// message prefix) and later call with only the message/format args.
|
||||
///
|
||||
/// ## What this class deliberately does NOT do
|
||||
/// - Spawn or manage threads / coroutines → use Void::Concurrency
|
||||
/// - Open, close, or rotate files → use Void::Filesystem
|
||||
///
|
||||
/// ## Deriving a logger
|
||||
/// 1. Override `write(const Entry&)` — called once per entry in
|
||||
/// Immediate mode and by the default `on_flush` implementation.
|
||||
/// 2. Optionally override `on_flush(std::span<const Entry>)` — useful
|
||||
/// for sinks that benefit from batch writes (e.g. file loggers).
|
||||
/// 3. **Call `flush()` in your own destructor** before the base
|
||||
/// destructor runs. Calling a pure virtual from `~Logger` is
|
||||
/// undefined behaviour, so the base destructor does *not* flush;
|
||||
/// any unflushed entries that remain after your destructor will be
|
||||
/// silently discarded.
|
||||
///
|
||||
/// ## Example — minimal derived logger
|
||||
/// @code
|
||||
/// class StdoutLogger final : public Void::Logging::Logger
|
||||
/// {
|
||||
/// public:
|
||||
/// ~StdoutLogger() noexcept override { flush(); }
|
||||
/// protected:
|
||||
/// void write(const Void::Logging::Entry& e) override
|
||||
/// {
|
||||
/// std::println("[{}] {}", e.level, e.message);
|
||||
/// }
|
||||
/// };
|
||||
/// @endcode
|
||||
class Logger
|
||||
{
|
||||
public:
|
||||
|
||||
// =========================================================================
|
||||
// ScopedContext — RAII, per-thread, named scope push/pop
|
||||
//
|
||||
// Pushing a scope has two effects:
|
||||
// 1. The scope name is appended to this thread's scope stack so
|
||||
// all subsequent entries carry it in Entry::scopes.
|
||||
// 2. A ScopeEnter entry is emitted at the level supplied to
|
||||
// push_scope() (or Trace by default).
|
||||
// 3. On destruction a ScopeExit entry is emitted at the same
|
||||
// level.
|
||||
//
|
||||
// Usage:
|
||||
// {
|
||||
// auto s = logger.push_scope("network"); // →
|
||||
// ScopeEnter logger.info("connecting …"); //
|
||||
// scopes={"network"}
|
||||
// {
|
||||
// auto t = logger.push_scope("tls"); // →
|
||||
// ScopeEnter logger.debug("handshake"); //
|
||||
// scopes={"network","tls"}
|
||||
// } // → ScopeExit
|
||||
// "tls"
|
||||
// } // → ScopeExit
|
||||
// "network"
|
||||
// =========================================================================
|
||||
class ScopedContext
|
||||
{
|
||||
public:
|
||||
|
||||
explicit ScopedContext(Logger& owner,
|
||||
std::string name,
|
||||
Level level,
|
||||
std::source_location loc) noexcept;
|
||||
~ScopedContext() noexcept;
|
||||
|
||||
// Non-copyable; move transfers ownership so the moved-from
|
||||
// guard does not emit a spurious ScopeExit on destruction.
|
||||
ScopedContext(const ScopedContext&) = delete;
|
||||
ScopedContext& operator=(const ScopedContext&) = delete;
|
||||
ScopedContext(ScopedContext&&) noexcept;
|
||||
ScopedContext& operator=(ScopedContext&&) = delete;
|
||||
|
||||
private:
|
||||
|
||||
Logger* m_owner{nullptr};
|
||||
std::string m_name;
|
||||
Level m_level{Level::Trace};
|
||||
bool m_active{true};
|
||||
};
|
||||
|
||||
// =========================================================================
|
||||
// Preset — a partially-applied logger
|
||||
//
|
||||
// A Preset captures a reference to the owning Logger together with
|
||||
// a fixed Level (and optional message prefix). Call it with just
|
||||
// the message / format args; the level is already baked in.
|
||||
//
|
||||
// Lifetime: a Preset must not outlive the Logger it was made from.
|
||||
//
|
||||
// Usage:
|
||||
// auto net_err = logger.make_preset(Level::Error, "[network] ");
|
||||
// net_err("connection refused"); // same as:
|
||||
// // logger.error("[network] connection refused");
|
||||
//
|
||||
// auto dbg = logger.make_preset(Level::Debug);
|
||||
// dbg("value = {}", 42);
|
||||
// =========================================================================
|
||||
class Preset
|
||||
{
|
||||
public:
|
||||
|
||||
Preset(Logger& owner, Level level,
|
||||
std::string prefix = {}) noexcept
|
||||
: m_owner(&owner), m_level(level),
|
||||
m_prefix(std::move(prefix))
|
||||
{
|
||||
}
|
||||
|
||||
// Non-owning; copyable so presets can be stored / passed
|
||||
// around.
|
||||
Preset(const Preset&) = default;
|
||||
Preset& operator=(const Preset&) = default;
|
||||
Preset(Preset&&) = default;
|
||||
Preset& operator=(Preset&&) = default;
|
||||
|
||||
// -----------------------------------------------------------------
|
||||
// Plain string overload
|
||||
// -----------------------------------------------------------------
|
||||
void operator()(std::string_view message,
|
||||
std::source_location loc =
|
||||
std::source_location::current()) const
|
||||
{
|
||||
if (m_prefix.empty()) {
|
||||
m_owner->log(m_level, message, loc);
|
||||
} else {
|
||||
m_owner->log(m_level,
|
||||
std::format("{}{}", m_prefix, message),
|
||||
loc);
|
||||
}
|
||||
}
|
||||
|
||||
// =========================================================================
|
||||
// FormatWithLocation helper
|
||||
//
|
||||
// std::source_location cannot share a parameter list with
|
||||
// a variadic pack, so this wrapper type captures both the
|
||||
// format string and the caller's source location in its
|
||||
// constructor.
|
||||
//
|
||||
// std::type_identity_t<Args>... prevents Args from being
|
||||
// deduced from the wrapper; they are deduced solely from
|
||||
// the trailing pack.
|
||||
//
|
||||
// Usage — source location is captured transparently:
|
||||
// logger.info("player {} scored {} points", name,
|
||||
// score);
|
||||
// =========================================================================
|
||||
template <typename... Args> struct FormatWithLocation {
|
||||
std::format_string<Args...> fmt;
|
||||
std::source_location location;
|
||||
|
||||
template <std::convertible_to<std::string_view> T>
|
||||
consteval FormatWithLocation(
|
||||
T&& f,
|
||||
std::source_location loc =
|
||||
std::source_location::current())
|
||||
: fmt(std::forward<T>(f)), location(loc)
|
||||
{
|
||||
}
|
||||
};
|
||||
|
||||
// -----------------------------------------------------------------
|
||||
// Formatted overload
|
||||
// Uses the same FormatWithLocation trick as Logger itself
|
||||
// so the call-site source location is captured
|
||||
// transparently.
|
||||
// -----------------------------------------------------------------
|
||||
template <typename... Args>
|
||||
void operator()(
|
||||
FormatWithLocation<std::type_identity_t<Args>...> fwl,
|
||||
Args&&... args) const
|
||||
{
|
||||
auto msg =
|
||||
m_prefix.empty()
|
||||
? std::format(fwl.fmt,
|
||||
std::forward<Args>(args)...)
|
||||
: std::format(
|
||||
"{}{}", m_prefix,
|
||||
std::format(fwl.fmt,
|
||||
std::forward<Args>(args)...));
|
||||
m_owner->log(m_level, msg, fwl.location);
|
||||
}
|
||||
|
||||
// Accessors
|
||||
[[nodiscard]] Level level() const noexcept
|
||||
{
|
||||
return m_level;
|
||||
}
|
||||
[[nodiscard]] const std::string& prefix() const noexcept
|
||||
{
|
||||
return m_prefix;
|
||||
}
|
||||
[[nodiscard]] Logger& logger() const noexcept
|
||||
{
|
||||
return *m_owner;
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
Logger* m_owner{nullptr};
|
||||
Level m_level{Level::Trace};
|
||||
std::string m_prefix;
|
||||
};
|
||||
|
||||
// =========================================================================
|
||||
// Lifetime
|
||||
// =========================================================================
|
||||
explicit Logger(Mode mode = Mode::Immediate,
|
||||
std::size_t capacity = 512) noexcept;
|
||||
|
||||
/// Does NOT flush remaining buffered entries — see class-level
|
||||
/// note.
|
||||
virtual ~Logger() noexcept = default;
|
||||
|
||||
Logger(const Logger&) = delete;
|
||||
Logger& operator=(const Logger&) = delete;
|
||||
Logger(Logger&&) = delete;
|
||||
Logger& operator=(Logger&&) = delete;
|
||||
|
||||
// =========================================================================
|
||||
// Configuration (all setters are thread-safe)
|
||||
// =========================================================================
|
||||
|
||||
/// Set the minimum level. Entries below this level are dropped.
|
||||
/// Level::Disabled suppresses all output.
|
||||
void set_level(Level level) noexcept;
|
||||
[[nodiscard]] Level level() const noexcept;
|
||||
|
||||
/// Switch between Immediate and Buffered mode.
|
||||
/// If the buffer is non-empty when switching, it is flushed first.
|
||||
void set_mode(Mode mode) noexcept;
|
||||
[[nodiscard]] Mode mode() const noexcept;
|
||||
|
||||
/// Maximum number of entries held before an automatic flush
|
||||
/// (Buffered mode only).
|
||||
void set_buffer_capacity(std::size_t n) noexcept;
|
||||
[[nodiscard]] std::size_t buffer_capacity() const noexcept;
|
||||
|
||||
// =========================================================================
|
||||
// Flush
|
||||
// =========================================================================
|
||||
|
||||
/// Drain the buffer and deliver all pending entries via on_flush().
|
||||
/// No-op in Immediate mode. Thread-safe.
|
||||
void flush();
|
||||
|
||||
// =========================================================================
|
||||
// Scoped context
|
||||
// =========================================================================
|
||||
|
||||
/// Push a named scope onto this thread's scope stack AND emit a
|
||||
/// ScopeEnter entry at the given level. The returned ScopedContext
|
||||
/// emits a ScopeExit entry and pops the stack when it goes out of
|
||||
/// scope.
|
||||
///
|
||||
/// @param name Human-readable scope label.
|
||||
/// @param level Level used for the ScopeEnter/ScopeExit entries.
|
||||
/// Defaults to Trace so scope boundaries are usually
|
||||
/// invisible in production builds.
|
||||
/// @return A [[nodiscard]] RAII guard — assign it to a named
|
||||
/// local; do NOT discard (it would immediately exit).
|
||||
[[nodiscard]] ScopedContext
|
||||
push_scope(std::string name,
|
||||
Level level = Level::Trace,
|
||||
std::source_location loc =
|
||||
std::source_location::current()) noexcept;
|
||||
|
||||
/// Emit a named scope open/close pair as explicit log entries
|
||||
/// WITHOUT modifying the thread-local scope stack. Useful when you
|
||||
/// want scope boundaries in the log but don't want child entries
|
||||
/// annotated.
|
||||
///
|
||||
/// Both ScopeEnter and ScopeExit are emitted immediately (Immediate
|
||||
/// mode) or buffered together (Buffered mode).
|
||||
void scope(
|
||||
std::string_view name,
|
||||
Level level = Level::Trace,
|
||||
std::source_location loc = std::source_location::current());
|
||||
|
||||
/// Read-only view of the current thread's scope stack.
|
||||
/// Ordered outermost → innermost.
|
||||
[[nodiscard]] static std::span<const std::string>
|
||||
current_scopes() noexcept;
|
||||
|
||||
// =========================================================================
|
||||
// Preset factory
|
||||
// =========================================================================
|
||||
|
||||
/// Create a Preset that fixes the level and an optional message
|
||||
/// prefix. The Preset holds a non-owning reference to *this — do
|
||||
/// not outlive the logger.
|
||||
[[nodiscard]] Preset make_preset(Level level,
|
||||
std::string prefix = {}) noexcept;
|
||||
|
||||
// =========================================================================
|
||||
// Core (non-template) logging — canonical entry points
|
||||
// =========================================================================
|
||||
|
||||
void log(Level level,
|
||||
std::string_view message,
|
||||
std::source_location location =
|
||||
std::source_location::current());
|
||||
|
||||
void trace(
|
||||
std::string_view msg,
|
||||
std::source_location loc = std::source_location::current());
|
||||
void debug(
|
||||
std::string_view msg,
|
||||
std::source_location loc = std::source_location::current());
|
||||
void info(
|
||||
std::string_view msg,
|
||||
std::source_location loc = std::source_location::current());
|
||||
void warning(
|
||||
std::string_view msg,
|
||||
std::source_location loc = std::source_location::current());
|
||||
void error(
|
||||
std::string_view msg,
|
||||
std::source_location loc = std::source_location::current());
|
||||
void fatal(
|
||||
std::string_view msg,
|
||||
std::source_location loc = std::source_location::current());
|
||||
|
||||
// =========================================================================
|
||||
// Formatted logging
|
||||
// =========================================================================
|
||||
|
||||
template <typename... Args>
|
||||
void log(Level level,
|
||||
Preset::FormatWithLocation<std::type_identity_t<Args>...>
|
||||
fwl,
|
||||
Args&&... args)
|
||||
{
|
||||
log(level,
|
||||
std::format(fwl.fmt, std::forward<Args>(args)...),
|
||||
fwl.location);
|
||||
}
|
||||
|
||||
template <typename... Args>
|
||||
void trace(
|
||||
Preset::FormatWithLocation<std::type_identity_t<Args>...> fwl,
|
||||
Args&&... args)
|
||||
{
|
||||
log(Level::Trace, fwl, std::forward<Args>(args)...);
|
||||
}
|
||||
|
||||
template <typename... Args>
|
||||
void debug(
|
||||
Preset::FormatWithLocation<std::type_identity_t<Args>...> fwl,
|
||||
Args&&... args)
|
||||
{
|
||||
log(Level::Debug, fwl, std::forward<Args>(args)...);
|
||||
}
|
||||
|
||||
template <typename... Args>
|
||||
void info(
|
||||
Preset::FormatWithLocation<std::type_identity_t<Args>...> fwl,
|
||||
Args&&... args)
|
||||
{
|
||||
log(Level::Info, fwl, std::forward<Args>(args)...);
|
||||
}
|
||||
|
||||
template <typename... Args>
|
||||
void warning(
|
||||
Preset::FormatWithLocation<std::type_identity_t<Args>...> fwl,
|
||||
Args&&... args)
|
||||
{
|
||||
log(Level::Warning, fwl, std::forward<Args>(args)...);
|
||||
}
|
||||
|
||||
template <typename... Args>
|
||||
void error(
|
||||
Preset::FormatWithLocation<std::type_identity_t<Args>...> fwl,
|
||||
Args&&... args)
|
||||
{
|
||||
log(Level::Error, fwl, std::forward<Args>(args)...);
|
||||
}
|
||||
|
||||
template <typename... Args>
|
||||
void fatal(
|
||||
Preset::FormatWithLocation<std::type_identity_t<Args>...> fwl,
|
||||
Args&&... args)
|
||||
{
|
||||
log(Level::Fatal, fwl, std::forward<Args>(args)...);
|
||||
}
|
||||
|
||||
protected:
|
||||
|
||||
// =========================================================================
|
||||
// Extension points
|
||||
//
|
||||
// Both methods are called OUTSIDE the internal mutex. Derived
|
||||
// classes may block, acquire their own locks, or call other Logger
|
||||
// methods without risk of deadlock.
|
||||
// =========================================================================
|
||||
|
||||
/// Deliver one entry to the sink.
|
||||
/// Called once per entry in Immediate mode, and by the default
|
||||
/// on_flush() for each entry in the batch.
|
||||
virtual void write(const Entry& entry) = 0;
|
||||
|
||||
/// Deliver a batch of buffered entries to the sink.
|
||||
/// The default implementation calls write() for each entry in
|
||||
/// order. Override to enable batched / vectored I/O.
|
||||
virtual void on_flush(std::span<const Entry> entries);
|
||||
|
||||
private:
|
||||
|
||||
/// Build an Entry and route it to write() or the buffer.
|
||||
void submit(Entry entry);
|
||||
|
||||
/// Emit a raw scope boundary entry; shared by push_scope and
|
||||
/// scope().
|
||||
void emit_scope_boundary(Kind kind,
|
||||
std::string_view name,
|
||||
Level level,
|
||||
std::source_location loc);
|
||||
|
||||
Level m_level{Level::Trace};
|
||||
Mode m_mode;
|
||||
std::size_t m_buffer_capacity;
|
||||
|
||||
mutable std::mutex m_mutex;
|
||||
std::vector<Entry> m_buffer;
|
||||
|
||||
/// Per-thread scope stack. ScopedContext pushes/pops into this.
|
||||
static thread_local std::vector<std::string> s_scope_stack;
|
||||
};
|
||||
|
||||
} // namespace Void::Logging
|
||||
@@ -1,7 +1,7 @@
|
||||
#pragma once
|
||||
#include <Void/Detail/Enum.hpp>
|
||||
#include <Void/Log/Log.hpp>
|
||||
#include <Void/Logging/Logger.hpp>
|
||||
#include <Void/Log/Logger.hpp>
|
||||
|
||||
// Bridge between Void::Log (the thin emission layer used by all modules) and
|
||||
// Void::Logging::Logger (the full sink implementation).
|
||||
@@ -11,7 +11,7 @@
|
||||
//
|
||||
// Example:
|
||||
// #include <Void/Log/LoggingAdapter.hpp>
|
||||
// #include <Void/Logging/ConsoleLogger.hpp>
|
||||
// #include <Void/Log/ConsoleLogger.hpp>
|
||||
//
|
||||
// Void::Logging::ConsoleLogger logger;
|
||||
// Void::Log::set_sink(Void::Log::make_sink(logger));
|
||||
|
||||
Reference in new issue
Block a user