From e4ddb0c8c45097613b33b93d22f0c72ba172fcf2 Mon Sep 17 00:00:00 2001 From: faumaray Date: Thu, 25 Jun 2026 02:16:11 +0300 Subject: [PATCH] update Signed-off-by: faumaray --- include/Void/Log/Logger.hpp | 42 ++++++++++++++++----- src/Log/Logger.cpp | 75 ++++++++++--------------------------- 2 files changed, 53 insertions(+), 64 deletions(-) diff --git a/include/Void/Log/Logger.hpp b/include/Void/Log/Logger.hpp index ddfe1bf..136a981 100644 --- a/include/Void/Log/Logger.hpp +++ b/include/Void/Log/Logger.hpp @@ -1,5 +1,6 @@ #pragma once +#include "Void/Log/Field.hpp" #include #include #include @@ -277,10 +278,10 @@ namespace Void::Logging /// logger's lifetime). Buffered: auto-flush /// threshold (adjustable). /// @param policy Async overflow behaviour at capacity. - explicit Logger(Mode mode = Mode::Async, - std::size_t capacity = 512, - OverflowPolicy policy = - OverflowPolicy::Block) noexcept; + explicit Logger( + Mode mode = Mode::Async, + std::size_t capacity = 512, + OverflowPolicy policy = OverflowPolicy::Block) noexcept; /// Stops and joins the worker thread (Async). Does NOT flush /// remaining entries — see class-level note. @@ -376,11 +377,34 @@ namespace Void::Logging // ========================================================================= // Core (non-template) logging — canonical entry points // ========================================================================= + inline void log( + Level level, std::string_view message, + std::source_location location = std::source_location::current()) + { + // Early level check outside the lock to avoid allocations on + // the hot path. + { + std::lock_guard lock(m_mutex); + if (level < m_level) { + return; + } + } - void log(Level level, - std::string_view message, - std::source_location location = - std::source_location::current()); + const auto fields = current_fields(); + 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 + .fields = {fields.begin(), fields.end()}, + }; + + submit(std::move(entry)); + } /// Route a fully-built Entry through this logger's level filter /// and dispatch (queue / buffer / inline write). Meant for @@ -524,7 +548,7 @@ namespace Void::Logging OverflowPolicy m_policy; mutable std::mutex m_mutex; - std::vector m_buffer; // Buffered mode only + std::vector m_buffer; // Buffered mode only std::size_t m_buffer_capacity; // ── Async machinery ────────────────────────────────────────── diff --git a/src/Log/Logger.cpp b/src/Log/Logger.cpp index 5097143..d6ba3a3 100644 --- a/src/Log/Logger.cpp +++ b/src/Log/Logger.cpp @@ -1,5 +1,5 @@ -#include #include +#include #include // std::exchange @@ -68,10 +68,8 @@ namespace Void::Logging 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_mode(mode), m_async_capacity(capacity > 0 ? capacity : 1), + m_policy(policy), m_buffer_capacity(capacity > 0 ? capacity : 1), m_space(static_cast(capacity > 0 ? capacity : 1)) { if (mode == Mode::Buffered) { @@ -116,8 +114,7 @@ namespace Void::Logging // entry is stranded in a queue/buffer nobody drains anymore. if (old == Mode::Async) { drain_async(); - } - else if (old == Mode::Buffered) { + } else if (old == Mode::Buffered) { std::vector to_flush; { std::lock_guard lock(m_mutex); @@ -153,10 +150,7 @@ namespace Void::Logging return m_buffer_capacity; } - OverflowPolicy Logger::overflow_policy() const noexcept - { - return m_policy; - } + OverflowPolicy Logger::overflow_policy() const noexcept { return m_policy; } // ============================================================================= // Scoped context @@ -198,37 +192,7 @@ namespace Void::Logging // 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; - } - } - - const auto fields = current_fields(); - 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 - .fields = {fields.begin(), fields.end()}, - }; - - submit(std::move(entry)); - } - - void Logger::relay(Entry entry) - { - submit(std::move(entry)); - } + void Logger::relay(Entry entry) { submit(std::move(entry)); } void Logger::trace(std::string_view msg, std::source_location loc) { @@ -337,22 +301,23 @@ namespace Void::Logging write(entry); return; - case Mode::Buffered: { - std::vector batch; + case Mode::Buffered: { - 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); + std::vector 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; } - if (!batch.empty()) { - on_flush(batch); - } - return; - } } }