Initial commit: VoidLibrary C++23 modular library
Adds Logging module with thread-safe Logger base class, ScopedContext RAII guards, Preset helpers, and a FileLogger standalone test/example. Includes CMake module system for auto-discovery and CTest integration. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
commit
4fc129fb7e
17 files changed
+1910
No files matched your search
Whitespace-only changes.
@@ -0,0 +1,10 @@
|
||||
#include <Void/Logging/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);
|
||||
};
|
||||
@@ -0,0 +1,317 @@
|
||||
#include <Void/Logging/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
|
||||
Reference in new issue
Block a user