27 {
28 std::cout << "=== Composite Writer Example (SRP Architecture) ===" << std::endl;
29 std::cout << std::endl;
30
31
32 std::cout << "Example 1: Timestamp formatter + Console sink" << std::endl;
33 {
34 auto formatter = std::make_unique<timestamp_formatter>();
35 auto sink = std::make_unique<console_sink>();
37
39 entry.
level = log_level::info;
40 entry.
message =
"Application started successfully";
41 entry.
timestamp = std::chrono::system_clock::now();
42
43 writer->write(entry);
44 std::cout << "Writer name: " << writer->get_name() << std::endl;
45 std::cout << "Is healthy: " << (writer->is_healthy() ? "yes" : "no") << std::endl;
46 }
47 std::cout << std::endl;
48
49
50 std::cout << "Example 2: Timestamp formatter + File sink" << std::endl;
51 {
52 auto formatter = std::make_unique<timestamp_formatter>();
53 auto sink = std::make_unique<file_sink>("/tmp/app.log", true);
55
57 entry.
level = log_level::warning;
58 entry.
message =
"Configuration file not found, using defaults";
59 entry.
timestamp = std::chrono::system_clock::now();
60
61 writer->write(entry);
62 writer->flush();
63 std::cout << "Logged to file: " << writer->get_sink()->get_info() << std::endl;
64 }
65 std::cout << std::endl;
66
67
68 std::cout << "Example 3: Multiple writers (different configurations)" << std::endl;
69 {
70
71 auto console_formatter = std::make_unique<timestamp_formatter>();
72 auto console_sink = std::make_unique<console_sink>(
false,
true);
74 std::move(console_formatter),
76 );
77
78
79 auto error_formatter = std::make_unique<timestamp_formatter>();
80 auto error_sink = std::make_unique<console_sink>(true, true);
82 std::move(error_formatter),
83 std::move(error_sink)
84 );
85
87 info_entry.
level = log_level::info;
88 info_entry.
message =
"This goes to stdout";
89 info_entry.
timestamp = std::chrono::system_clock::now();
91
93 error_entry.
level = log_level::error;
94 error_entry.
message =
"This goes to stderr";
95 error_entry.
timestamp = std::chrono::system_clock::now();
96 error_writer->write(error_entry);
97 }
98 std::cout << std::endl;
99
100 std::cout << "=== Benefits of SRP Architecture ===" << std::endl;
101 std::cout << "1. Single Responsibility: Each component has one job" << std::endl;
102 std::cout << "2. Open/Closed: Add new formatters/sinks without modifying existing code" << std::endl;
103 std::cout << "3. Testability: Each component can be tested independently" << std::endl;
104 std::cout << "4. Flexibility: Mix and match any formatter with any sink" << std::endl;
105 std::cout << "5. Reusability: Formatters and sinks can be reused in different combinations" << std::endl;
106
107 return 0;
108}
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.
std::unique_ptr< composite_writer > make_composite_writer(std::unique_ptr< log_formatter_interface > formatter, std::unique_ptr< output_sink_interface > sink)
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.