442 lines
12 KiB
C++
442 lines
12 KiB
C++
#include <Void/Log/Logger.hpp>
|
|
#include <Void/Log/Field.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,
|
|
OverflowPolicy policy) noexcept
|
|
: m_mode(mode), m_async_capacity(capacity > 0 ? capacity : 1),
|
|
m_policy(policy), m_buffer_capacity(capacity > 0 ? capacity : 1),
|
|
m_space(static_cast<std::ptrdiff_t>(capacity > 0 ? capacity : 1))
|
|
{
|
|
if (mode == Mode::Buffered) {
|
|
m_buffer.reserve(m_buffer_capacity);
|
|
}
|
|
}
|
|
|
|
Logger::~Logger() noexcept
|
|
{
|
|
// Only stop and join the worker. Virtual extension points must not
|
|
// be touched here — the derived class is already destroyed. Pending
|
|
// entries are freed (silently dropped) by the queue's destructor.
|
|
m_stop.store(true, std::memory_order_release);
|
|
wake_worker();
|
|
if (m_worker.joinable()) {
|
|
m_worker.join();
|
|
}
|
|
}
|
|
|
|
// =============================================================================
|
|
// Configuration
|
|
// =============================================================================
|
|
|
|
void Logger::set_level(Level level) noexcept
|
|
{
|
|
m_level.store(level, std::memory_order_relaxed);
|
|
}
|
|
|
|
Level Logger::level() const noexcept
|
|
{
|
|
return m_level.load(std::memory_order_relaxed);
|
|
}
|
|
|
|
void Logger::set_mode(Mode mode) noexcept
|
|
{
|
|
const Mode old = m_mode.load(std::memory_order_acquire);
|
|
if (old == mode) {
|
|
return;
|
|
}
|
|
|
|
// Deliver everything pending under the old strategy first so no
|
|
// entry is stranded in a queue/buffer nobody drains anymore.
|
|
if (old == Mode::Async) {
|
|
drain_async();
|
|
} else if (old == Mode::Buffered) {
|
|
std::vector<Entry> to_flush;
|
|
{
|
|
std::lock_guard lock(m_mutex);
|
|
to_flush = std::move(m_buffer);
|
|
m_buffer = {};
|
|
}
|
|
if (!to_flush.empty()) {
|
|
on_flush(to_flush);
|
|
}
|
|
}
|
|
|
|
if (mode == Mode::Buffered) {
|
|
std::lock_guard lock(m_mutex);
|
|
m_buffer.reserve(m_buffer_capacity);
|
|
}
|
|
m_mode.store(mode, std::memory_order_release);
|
|
}
|
|
|
|
Mode Logger::mode() const noexcept
|
|
{
|
|
return m_mode.load(std::memory_order_acquire);
|
|
}
|
|
|
|
void Logger::set_buffer_capacity(std::size_t n) noexcept
|
|
{
|
|
std::lock_guard lock(m_mutex);
|
|
m_buffer_capacity = n > 0 ? n : 1;
|
|
}
|
|
|
|
std::size_t Logger::buffer_capacity() const noexcept
|
|
{
|
|
std::lock_guard lock(m_mutex);
|
|
return m_buffer_capacity;
|
|
}
|
|
|
|
OverflowPolicy Logger::overflow_policy() const noexcept { return m_policy; }
|
|
|
|
// =============================================================================
|
|
// 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::relay(Entry entry) { 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()
|
|
{
|
|
if (m_mode.load(std::memory_order_acquire) == Mode::Async) {
|
|
drain_async();
|
|
return;
|
|
}
|
|
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;
|
|
}
|
|
}
|
|
|
|
const auto fields = current_fields();
|
|
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
|
|
.fields = {fields.begin(), fields.end()},
|
|
};
|
|
|
|
submit(std::move(entry));
|
|
}
|
|
|
|
// =============================================================================
|
|
// Private — submit
|
|
// =============================================================================
|
|
|
|
void Logger::submit(Entry entry)
|
|
{
|
|
if (entry.level < m_level.load(std::memory_order_relaxed)) {
|
|
return;
|
|
}
|
|
|
|
switch (m_mode.load(std::memory_order_acquire)) {
|
|
case Mode::Async:
|
|
async_submit(std::move(entry));
|
|
return;
|
|
|
|
case Mode::Immediate:
|
|
// Virtual call outside any lock (see class documentation).
|
|
write(entry);
|
|
return;
|
|
|
|
case Mode::Buffered:
|
|
{
|
|
std::vector<Entry> batch;
|
|
{
|
|
std::lock_guard lock(m_mutex);
|
|
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);
|
|
}
|
|
}
|
|
if (!batch.empty()) {
|
|
on_flush(batch);
|
|
}
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
|
|
// =============================================================================
|
|
// Private — async machinery
|
|
// =============================================================================
|
|
|
|
void Logger::async_submit(Entry entry)
|
|
{
|
|
start_worker();
|
|
|
|
switch (m_policy) {
|
|
case OverflowPolicy::Block:
|
|
// Wait for queue space; the worker releases one slot per
|
|
// popped entry. Nothing is ever dropped.
|
|
m_space.acquire();
|
|
break;
|
|
|
|
case OverflowPolicy::DropNewest:
|
|
if (!m_space.try_acquire()) {
|
|
return; // full — discard the incoming entry
|
|
}
|
|
break;
|
|
|
|
case OverflowPolicy::DropOldest:
|
|
// Unbounded enqueue; the worker trims the oldest entries
|
|
// down to capacity when it falls behind.
|
|
break;
|
|
}
|
|
|
|
m_submitted.fetch_add(1, std::memory_order_release);
|
|
m_queue.push(std::move(entry));
|
|
wake_worker();
|
|
}
|
|
|
|
void Logger::start_worker()
|
|
{
|
|
if (m_worker_started.load(std::memory_order_acquire)) {
|
|
return;
|
|
}
|
|
std::call_once(m_worker_once, [this] {
|
|
m_worker = std::thread([this] { worker_loop(); });
|
|
m_worker_started.store(true, std::memory_order_release);
|
|
});
|
|
}
|
|
|
|
void Logger::wake_worker() noexcept
|
|
{
|
|
if (m_sleeping.exchange(false, std::memory_order_acq_rel)) {
|
|
m_wakeup.release();
|
|
}
|
|
}
|
|
|
|
void Logger::worker_loop()
|
|
{
|
|
std::vector<Entry> batch;
|
|
const bool counts_space = m_policy != OverflowPolicy::DropOldest;
|
|
|
|
for (;;) {
|
|
// Drain everything currently queued.
|
|
while (auto e = m_queue.try_pop()) {
|
|
batch.push_back(std::move(*e));
|
|
if (counts_space) {
|
|
m_space.release();
|
|
}
|
|
}
|
|
|
|
if (!batch.empty()) {
|
|
const bool stopping = m_stop.load(std::memory_order_acquire);
|
|
|
|
std::size_t dropped = 0;
|
|
if (m_policy == OverflowPolicy::DropOldest &&
|
|
batch.size() > m_async_capacity) {
|
|
dropped = batch.size() - m_async_capacity;
|
|
batch.erase(batch.begin(),
|
|
batch.begin() +
|
|
static_cast<std::ptrdiff_t>(dropped));
|
|
}
|
|
|
|
// After stop the derived class may already be destroyed —
|
|
// never touch the virtuals; just account and discard.
|
|
if (!stopping) {
|
|
on_flush(batch);
|
|
}
|
|
m_completed.fetch_add(batch.size() + dropped,
|
|
std::memory_order_release);
|
|
m_completed.notify_all();
|
|
batch.clear();
|
|
continue; // re-poll before considering a park
|
|
}
|
|
|
|
if (m_stop.load(std::memory_order_acquire)) {
|
|
return;
|
|
}
|
|
|
|
// Park. Producers re-check m_sleeping after pushing, so a wake
|
|
// between the store and acquire below cannot be missed.
|
|
m_sleeping.store(true, std::memory_order_release);
|
|
if (!m_queue.empty() || m_stop.load(std::memory_order_acquire)) {
|
|
m_sleeping.store(false, std::memory_order_release);
|
|
continue;
|
|
}
|
|
m_wakeup.acquire();
|
|
m_sleeping.store(false, std::memory_order_release);
|
|
}
|
|
}
|
|
|
|
void Logger::drain_async()
|
|
{
|
|
const auto target = m_submitted.load(std::memory_order_acquire);
|
|
for (;;) {
|
|
const auto done = m_completed.load(std::memory_order_acquire);
|
|
if (done >= target) {
|
|
return;
|
|
}
|
|
wake_worker();
|
|
m_completed.wait(done, std::memory_order_acquire);
|
|
}
|
|
}
|
|
|
|
} // namespace Void::Logging
|