#include <iostream>
#include <memory>
std::cout << "=== Composite Writer Example (SRP Architecture) ===" << std::endl;
std::cout << std::endl;
std::cout << "Example 1: Timestamp formatter + Console sink" << std::endl;
{
auto formatter = std::make_unique<timestamp_formatter>();
auto sink = std::make_unique<console_sink>();
auto writer = make_composite_writer(std::move(formatter), std::move(sink));
entry.
level = log_level::info;
entry.
message =
"Application started successfully";
entry.
timestamp = std::chrono::system_clock::now();
writer->write(entry);
std::cout << "Writer name: " << writer->get_name() << std::endl;
std::cout << "Is healthy: " << (writer->is_healthy() ? "yes" : "no") << std::endl;
}
std::cout << std::endl;
std::cout << "Example 2: Timestamp formatter + File sink" << std::endl;
{
auto formatter = std::make_unique<timestamp_formatter>();
auto sink = std::make_unique<file_sink>("/tmp/app.log", true);
auto writer = make_composite_writer(std::move(formatter), std::move(sink));
entry.
level = log_level::warning;
entry.
message =
"Configuration file not found, using defaults";
entry.
timestamp = std::chrono::system_clock::now();
writer->write(entry);
writer->flush();
std::cout << "Logged to file: " << writer->get_sink()->get_info() << std::endl;
}
std::cout << std::endl;
std::cout << "Example 3: Multiple writers (different configurations)" << std::endl;
{
auto console_formatter = std::make_unique<timestamp_formatter>();
auto console_sink = std::make_unique<console_sink>(
false,
true);
std::move(console_formatter),
);
auto error_formatter = std::make_unique<timestamp_formatter>();
auto error_sink = std::make_unique<console_sink>(true, true);
auto error_writer = make_composite_writer(
std::move(error_formatter),
std::move(error_sink)
);
info_entry.
level = log_level::info;
info_entry.
message =
"This goes to stdout";
info_entry.
timestamp = std::chrono::system_clock::now();
error_entry.
level = log_level::error;
error_entry.
message =
"This goes to stderr";
error_entry.
timestamp = std::chrono::system_clock::now();
error_writer->write(error_entry);
}
std::cout << std::endl;
std::cout << "=== Benefits of SRP Architecture ===" << std::endl;
std::cout << "1. Single Responsibility: Each component has one job" << std::endl;
std::cout << "2. Open/Closed: Add new formatters/sinks without modifying existing code" << std::endl;
std::cout << "3. Testability: Each component can be tested independently" << std::endl;
std::cout << "4. Flexibility: Mix and match any formatter with any sink" << std::endl;
std::cout << "5. Reusability: Formatters and sinks can be reused in different combinations" << std::endl;
return 0;
}
Outputs log messages to console (stdout or stderr)
Core console writer for logging to stdout/stderr.
common::VoidResult write(const log_entry &entry) override
Write a log entry to console.
Composite writer combining formatter and sink (Pipeline Pattern) kcenon.
Console output sink implementation (stdout/stderr) kcenon.
File output sink implementation.
Data structures for representing log entries and source locations kcenon.
Represents a single log entry with all associated metadata.
log_level level
Severity level of the log message.
small_string_256 message
The actual log message.
std::chrono::system_clock::time_point timestamp
Timestamp when the log entry was created.