Logger System 0.1.3
High-performance C++20 thread-safe logging system with asynchronous capabilities
Loading...
Searching...
No Matches
composite_writer.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
22#include <memory>
27
28namespace kcenon::logger {
29
78public:
94 composite_writer(std::unique_ptr<log_formatter_interface> formatter,
95 std::unique_ptr<output_sink_interface> sink)
96 : formatter_(std::move(formatter))
97 , sink_(std::move(sink))
98 {
99 if (!formatter_) {
100 throw std::invalid_argument("Formatter cannot be null");
101 }
102 if (!sink_) {
103 throw std::invalid_argument("Sink cannot be null");
104 }
105 }
106
114 ~composite_writer() override {
115 flush(); // Ensure all data is written
116 }
117
131 common::VoidResult write(const log_entry& entry) override {
132 // Stage 1: Format the log entry
133 std::string formatted = formatter_->format(entry);
134
135 // Stage 2: Write to sink
136 return sink_->write_raw(formatted);
137 }
138
148 return sink_->flush();
149 }
150
159 std::string get_name() const override {
160 return formatter_->get_name() + "+" + sink_->get_name();
161 }
162
172 bool is_healthy() const override {
173 return sink_->is_healthy();
174 }
175
186 return formatter_.get();
187 }
188
199 return sink_.get();
200 }
201
202private:
203 std::unique_ptr<log_formatter_interface> formatter_;
204 std::unique_ptr<output_sink_interface> sink_;
205};
206
226inline std::unique_ptr<composite_writer> make_composite_writer(
227 std::unique_ptr<log_formatter_interface> formatter,
228 std::unique_ptr<output_sink_interface> sink)
229{
230 return std::make_unique<composite_writer>(
231 std::move(formatter),
232 std::move(sink)
233 );
234}
235
236} // namespace kcenon::logger
Coordinates formatting and output through a pipeline.
std::string get_name() const override
output_sink_interface * get_sink() const
Get the sink.
~composite_writer() override
Destructor.
std::unique_ptr< log_formatter_interface > formatter_
Formatting stage.
composite_writer(std::unique_ptr< log_formatter_interface > formatter, std::unique_ptr< output_sink_interface > sink)
Construct a composite writer.
std::unique_ptr< output_sink_interface > sink_
Output stage.
common::VoidResult flush() override
Flush the sink.
common::VoidResult write(const log_entry &entry) override
Write a log entry through the pipeline.
bool is_healthy() const override
Check if the writer is healthy.
log_formatter_interface * get_formatter() const
Get the formatter.
Abstract interface for log message formatters.
Base interface for all log writers and decorators.
Abstract interface for log output destinations (I/O only)
Interface for log message formatters (Strategy Pattern) kcenon.
Base interface for all log writers and decorators.
std::unique_ptr< composite_writer > make_composite_writer(std::unique_ptr< log_formatter_interface > formatter, std::unique_ptr< output_sink_interface > sink)
Interface for log output destinations (Single Responsibility Principle) kcenon.
Tag interface for composite writers.
Represents a single log entry with all associated metadata.
Definition log_entry.h:155
Writer category interfaces and type traits kcenon.