1 parent
96b8b19970
commit
e4ddb0c8c4
2 files changed
+39
-50
No files matched your search
@@ -1,5 +1,6 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include "Void/Log/Field.hpp"
|
||||||
#include <Void/Async/MpscQueue.hpp>
|
#include <Void/Async/MpscQueue.hpp>
|
||||||
#include <Void/Log/Entry.hpp>
|
#include <Void/Log/Entry.hpp>
|
||||||
#include <Void/Log/Level.hpp>
|
#include <Void/Log/Level.hpp>
|
||||||
@@ -277,10 +278,10 @@ namespace Void::Logging
|
|||||||
/// logger's lifetime). Buffered: auto-flush
|
/// logger's lifetime). Buffered: auto-flush
|
||||||
/// threshold (adjustable).
|
/// threshold (adjustable).
|
||||||
/// @param policy Async overflow behaviour at capacity.
|
/// @param policy Async overflow behaviour at capacity.
|
||||||
explicit Logger(Mode mode = Mode::Async,
|
explicit Logger(
|
||||||
|
Mode mode = Mode::Async,
|
||||||
std::size_t capacity = 512,
|
std::size_t capacity = 512,
|
||||||
OverflowPolicy policy =
|
OverflowPolicy policy = OverflowPolicy::Block) noexcept;
|
||||||
OverflowPolicy::Block) noexcept;
|
|
||||||
|
|
||||||
/// Stops and joins the worker thread (Async). Does NOT flush
|
/// Stops and joins the worker thread (Async). Does NOT flush
|
||||||
/// remaining entries — see class-level note.
|
/// remaining entries — see class-level note.
|
||||||
@@ -376,11 +377,34 @@ namespace Void::Logging
|
|||||||
// =========================================================================
|
// =========================================================================
|
||||||
// Core (non-template) logging — canonical entry points
|
// 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,
|
const auto fields = current_fields();
|
||||||
std::string_view message,
|
Entry entry{
|
||||||
std::source_location location =
|
.kind = Kind::Message,
|
||||||
std::source_location::current());
|
.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
|
/// Route a fully-built Entry through this logger's level filter
|
||||||
/// and dispatch (queue / buffer / inline write). Meant for
|
/// and dispatch (queue / buffer / inline write). Meant for
|
||||||
|
|||||||
+8
-43
@@ -1,5 +1,5 @@
|
|||||||
#include <Void/Log/Field.hpp>
|
|
||||||
#include <Void/Log/Logger.hpp>
|
#include <Void/Log/Logger.hpp>
|
||||||
|
#include <Void/Log/Field.hpp>
|
||||||
|
|
||||||
#include <utility> // std::exchange
|
#include <utility> // std::exchange
|
||||||
|
|
||||||
@@ -68,10 +68,8 @@ namespace Void::Logging
|
|||||||
|
|
||||||
Logger::Logger(Mode mode, std::size_t capacity,
|
Logger::Logger(Mode mode, std::size_t capacity,
|
||||||
OverflowPolicy policy) noexcept
|
OverflowPolicy policy) noexcept
|
||||||
: m_mode(mode),
|
: m_mode(mode), m_async_capacity(capacity > 0 ? capacity : 1),
|
||||||
m_async_capacity(capacity > 0 ? capacity : 1),
|
m_policy(policy), m_buffer_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))
|
m_space(static_cast<std::ptrdiff_t>(capacity > 0 ? capacity : 1))
|
||||||
{
|
{
|
||||||
if (mode == Mode::Buffered) {
|
if (mode == Mode::Buffered) {
|
||||||
@@ -116,8 +114,7 @@ namespace Void::Logging
|
|||||||
// entry is stranded in a queue/buffer nobody drains anymore.
|
// entry is stranded in a queue/buffer nobody drains anymore.
|
||||||
if (old == Mode::Async) {
|
if (old == Mode::Async) {
|
||||||
drain_async();
|
drain_async();
|
||||||
}
|
} else if (old == Mode::Buffered) {
|
||||||
else if (old == Mode::Buffered) {
|
|
||||||
std::vector<Entry> to_flush;
|
std::vector<Entry> to_flush;
|
||||||
{
|
{
|
||||||
std::lock_guard lock(m_mutex);
|
std::lock_guard lock(m_mutex);
|
||||||
@@ -153,10 +150,7 @@ namespace Void::Logging
|
|||||||
return m_buffer_capacity;
|
return m_buffer_capacity;
|
||||||
}
|
}
|
||||||
|
|
||||||
OverflowPolicy Logger::overflow_policy() const noexcept
|
OverflowPolicy Logger::overflow_policy() const noexcept { return m_policy; }
|
||||||
{
|
|
||||||
return m_policy;
|
|
||||||
}
|
|
||||||
|
|
||||||
// =============================================================================
|
// =============================================================================
|
||||||
// Scoped context
|
// Scoped context
|
||||||
@@ -198,37 +192,7 @@ namespace Void::Logging
|
|||||||
// Core logging
|
// Core logging
|
||||||
// =============================================================================
|
// =============================================================================
|
||||||
|
|
||||||
void Logger::log(Level level, std::string_view message,
|
void Logger::relay(Entry entry) { submit(std::move(entry)); }
|
||||||
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::trace(std::string_view msg, std::source_location loc)
|
void Logger::trace(std::string_view msg, std::source_location loc)
|
||||||
{
|
{
|
||||||
@@ -337,7 +301,8 @@ namespace Void::Logging
|
|||||||
write(entry);
|
write(entry);
|
||||||
return;
|
return;
|
||||||
|
|
||||||
case Mode::Buffered: {
|
case Mode::Buffered:
|
||||||
|
{
|
||||||
std::vector<Entry> batch;
|
std::vector<Entry> batch;
|
||||||
{
|
{
|
||||||
std::lock_guard lock(m_mutex);
|
std::lock_guard lock(m_mutex);
|
||||||
|
|||||||
Reference in new issue
Block a user