Logger System 0.1.3
High-performance C++20 thread-safe logging system with asynchronous capabilities
Loading...
Searching...
No Matches
formatted_writer.cpp
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
6
8
9#include <stdexcept>
10
11namespace kcenon::logger {
12
13formatted_writer::formatted_writer(std::unique_ptr<log_writer_interface> wrapped,
14 std::unique_ptr<log_formatter_interface> formatter)
15 : decorator_writer_base(std::move(wrapped), "formatted"), formatter_(std::move(formatter)) {
16 // Base class constructor handles nullptr check for wrapped writer
17}
18
20 // If no formatter, pass through all entries unchanged
21 if (!formatter_) {
22 return wrapped().write(entry);
23 }
24
25 // Apply formatter to get formatted message
26 std::string formatted_message = formatter_->format(entry);
27
28 // Create a new log entry with the formatted message
29 log_entry formatted_entry(entry.level, formatted_message, entry.timestamp);
30
31 // Copy optional fields from original entry
32 formatted_entry.location = entry.location;
33 formatted_entry.thread_id = entry.thread_id;
34 formatted_entry.category = entry.category;
35 formatted_entry.otel_ctx = entry.otel_ctx;
36 formatted_entry.fields = entry.fields;
37
38 // Delegate to wrapped writer
39 return wrapped().write(formatted_entry);
40}
41
42std::string formatted_writer::get_name() const {
43 if (formatter_) {
44 return "formatted(" + formatter_->get_name() + ")_" + wrapped().get_name();
45 }
46 // Use base class default format when no formatter
48}
49
53
54// Factory function
55std::unique_ptr<formatted_writer> make_formatted_writer(
56 std::unique_ptr<log_writer_interface> writer,
57 std::unique_ptr<log_formatter_interface> formatter) {
58 return std::make_unique<formatted_writer>(std::move(writer), std::move(formatter));
59}
60
61} // namespace kcenon::logger
Abstract base class for decorator pattern log writers.
std::string get_name() const override
Get the name of this writer.
log_writer_interface & wrapped() noexcept
Access the wrapped writer (non-const)
common::VoidResult write(const log_entry &entry) override
Write a log entry after applying the formatter.
std::string get_name() const override
Get the name of this writer.
formatted_writer(std::unique_ptr< log_writer_interface > wrapped, std::unique_ptr< log_formatter_interface > formatter)
Construct a formatted writer.
const log_formatter_interface * get_formatter() const
Get the current formatter.
std::unique_ptr< log_formatter_interface > formatter_
Abstract interface for log message formatters.
virtual std::string get_name() const =0
virtual common::VoidResult write(const log_entry &entry)=0
Write a log entry.
Decorator that applies formatting to wrapped log writers.
Data structures for representing log entries and source locations kcenon.
std::unique_ptr< formatted_writer > make_formatted_writer(std::unique_ptr< log_writer_interface > writer, std::unique_ptr< log_formatter_interface > formatter)
Factory function to create a formatted writer.
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
std::optional< log_fields > fields
Optional structured fields for key-value logging.
Definition log_entry.h:213
std::optional< small_string_64 > thread_id
Optional thread identifier.
Definition log_entry.h:190
std::optional< otlp::otel_context > otel_ctx
Optional OpenTelemetry context for trace correlation.
Definition log_entry.h:204
log_level level
Severity level of the log message.
Definition log_entry.h:162
std::optional< small_string_128 > category
Optional category for log filtering and routing.
Definition log_entry.h:197
std::chrono::system_clock::time_point timestamp
Timestamp when the log entry was created.
Definition log_entry.h:175