Logger System 0.1.3
High-performance C++20 thread-safe logging system with asynchronous capabilities
Loading...
Searching...
No Matches
queued_writer_base.h
Go to the documentation of this file.
1// BSD 3-Clause License
2// Copyright (c) 2025, 🍀☀🌕🌥 🌊
3// See the LICENSE file in the project root for full license information.
4
5#pragma once
6
18#include <mutex>
19#include <memory>
20#include <atomic>
21#include <queue>
22#include <vector>
23
24namespace kcenon::logger {
25
33inline log_entry copy_log_entry(const log_entry& entry) {
34 if (entry.location) {
35 return log_entry(entry.level,
36 entry.message.to_string(),
37 entry.location->file.to_string(),
38 entry.location->line,
39 entry.location->function.to_string(),
40 entry.timestamp);
41 }
42 return log_entry(entry.level,
43 entry.message.to_string(),
44 entry.timestamp);
45}
46
60template <typename Container>
62public:
70 explicit queued_writer_base(std::unique_ptr<log_writer_interface> wrapped_writer,
71 std::size_t max_queue_size,
72 std::string_view decorator_name)
73 : decorator_writer_base(std::move(wrapped_writer), decorator_name)
74 , max_queue_size_(max_queue_size)
75 , shutting_down_(false) {
76 }
77
81 ~queued_writer_base() override = default;
82
87 bool is_healthy() const override {
88 return !shutting_down_ && wrapped().is_healthy();
89 }
90
95 std::size_t get_queue_size() const {
96 std::lock_guard<std::mutex> lock(queue_mutex_);
98 }
99
104 std::size_t get_max_queue_size() const {
105 return max_queue_size_;
106 }
107
108protected:
115 std::lock_guard<std::mutex> lock(queue_mutex_);
116
118 return handle_overflow(entry);
119 }
120
121 enqueue_entry(queue_, entry);
123 return common::ok();
124 }
125
132 virtual common::VoidResult handle_overflow(const log_entry& /*entry*/) {
134 }
135
140 virtual void on_entry_enqueued() {}
141
145 template <typename T>
146 static std::size_t get_container_size(const std::queue<T>& container) {
147 return container.size();
148 }
149
153 template <typename T>
154 static std::size_t get_container_size(const std::vector<T>& container) {
155 return container.size();
156 }
157
161 template <typename T>
162 static void enqueue_entry(std::queue<T>& container, const log_entry& entry) {
163 container.push(copy_log_entry(entry));
164 }
165
169 template <typename T>
170 static void enqueue_entry(std::vector<T>& container, const log_entry& entry) {
171 container.push_back(copy_log_entry(entry));
172 }
173
174 std::size_t max_queue_size_;
175
176 mutable std::mutex queue_mutex_;
177 Container queue_;
178
179 std::atomic<bool> shutting_down_;
180};
181
182} // namespace kcenon::logger
Abstract base class for decorator pattern log writers.
const std::string & decorator_name() const noexcept
Get the decorator name.
log_writer_interface & wrapped() noexcept
Access the wrapped writer (non-const)
virtual bool is_healthy() const =0
Check if the writer is healthy.
Abstract base class for queue-based log writers.
virtual common::VoidResult handle_overflow(const log_entry &)
Handle queue overflow condition.
virtual void on_entry_enqueued()
Called after an entry is successfully enqueued.
std::size_t get_queue_size() const
Get the current queue size.
common::VoidResult try_enqueue(const log_entry &entry)
Try to enqueue an entry with overflow protection.
~queued_writer_base() override=default
Virtual destructor.
bool is_healthy() const override
Check if the writer is healthy.
static void enqueue_entry(std::vector< T > &container, const log_entry &entry)
Enqueue entry (specialization for std::vector)
static std::size_t get_container_size(const std::queue< T > &container)
Get container size (specialization for std::queue)
static void enqueue_entry(std::queue< T > &container, const log_entry &entry)
Enqueue entry (specialization for std::queue)
std::size_t get_max_queue_size() const
Get maximum queue size.
static std::size_t get_container_size(const std::vector< T > &container)
Get container size (specialization for std::vector)
queued_writer_base(std::unique_ptr< log_writer_interface > wrapped_writer, std::size_t max_queue_size, std::string_view decorator_name)
Constructor for queued writer base.
std::string to_string() const
Convert to std::string.
Base class for decorator pattern writers.
Data structures for representing log entries and source locations kcenon.
VoidResult ok()
common::VoidResult make_logger_void_result(logger_error_code code, const std::string &message="")
log_entry copy_log_entry(const log_entry &entry)
Creates a copy of a log_entry for queue storage.
Tag interface for asynchronous writers.
Represents a single log entry with all associated metadata.
Definition log_entry.h:155
std::optional< source_location > location
Optional source code location information.
Definition log_entry.h:183
log_level level
Severity level of the log message.
Definition log_entry.h:162
small_string_256 message
The actual log message.
Definition log_entry.h:169
std::chrono::system_clock::time_point timestamp
Timestamp when the log entry was created.
Definition log_entry.h:175
Writer category interfaces and type traits kcenon.