Logger System 0.1.3
High-performance C++20 thread-safe logging system with asynchronous capabilities
Loading...
Searching...
No Matches
writer_builder_example.cpp File Reference

Demonstrates writer_builder usage patterns. More...

#include <kcenon/logger/builders/writer_builder.h>
#include <kcenon/logger/core/logger.h>
#include <kcenon/logger/interfaces/log_filter_interface.h>
#include <kcenon/logger/writers/async_writer.h>
#include <iostream>
#include <memory>
Include dependency graph for writer_builder_example.cpp:

Go to the source code of this file.

Classes

class  level_filter
 Custom filter that only allows specific log levels. More...
 

Functions

void example_basic_file_writer ()
 Example 1: Basic file writer.
 
void example_async_buffered_file ()
 Example 2: Async + Buffered File Writer.
 
void example_filtered_console ()
 Example 3: Console Writer with Filtering.
 
void example_production_setup ()
 Example 5: Production Setup with Multiple Writers.
 
void example_builder_vs_manual ()
 Example 6: Comparing Builder vs Manual Composition.
 
int main ()
 

Detailed Description

Demonstrates writer_builder usage patterns.

Author
kcenon
Since
4.1.0

Definition in file writer_builder_example.cpp.

Function Documentation

◆ example_async_buffered_file()

void example_async_buffered_file ( )

Example 2: Async + Buffered File Writer.

High-performance setup combining buffering and async writing.

  • Buffered: Batches up to 500 entries before writing
  • Async: Processes writes in background thread with 20000-entry queue
Examples
writer_builder_example.cpp.

Definition at line 77 of file writer_builder_example.cpp.

77 {
78 std::cout << "\n=== Example 2: Async + Buffered File Writer ===\n";
79
80 auto writer = writer_builder()
81 .file("app.log")
82 .buffered(500) // Buffer up to 500 entries
83 .async(20000) // Large queue for high throughput
84 .build();
85
86 std::cout << "Created async+buffered writer: " << writer->get_name() << "\n";
87 std::cout << " - Buffering reduces I/O operations\n";
88 std::cout << " - Async processing enables high throughput\n";
89
90 // Note: async_writer needs to be started
91 if (auto* async_w = dynamic_cast<async_writer*>(writer.get())) {
92 async_w->start();
93 std::cout << " - Async writer started\n";
94 async_w->stop();
95 }
96}
Asynchronous wrapper for log writers.

Referenced by main().

Here is the caller graph for this function:

◆ example_basic_file_writer()

void example_basic_file_writer ( )

Example 1: Basic file writer.

The simplest usage - just a file writer with no decorators.

Examples
writer_builder_example.cpp.

Definition at line 60 of file writer_builder_example.cpp.

60 {
61 std::cout << "\n=== Example 1: Basic File Writer ===\n";
62
63 auto writer = writer_builder()
64 .file("basic.log")
65 .build();
66
67 std::cout << "Created basic file writer: " << writer->get_name() << "\n";
68}

Referenced by main().

Here is the caller graph for this function:

◆ example_builder_vs_manual()

void example_builder_vs_manual ( )

Example 6: Comparing Builder vs Manual Composition.

Shows the difference between using the builder and manual nesting.

Examples
writer_builder_example.cpp.

Definition at line 218 of file writer_builder_example.cpp.

218 {
219 std::cout << "\n=== Example 6: Builder vs Manual Composition ===\n";
220
221 std::cout << "\nManual composition (verbose):\n";
222 std::cout << " auto writer = std::make_unique<async_writer>(\n";
223 std::cout << " std::make_unique<buffered_writer>(\n";
224 std::cout << " std::make_unique<file_writer>(\"app.log\"),\n";
225 std::cout << " 500\n";
226 std::cout << " ),\n";
227 std::cout << " 20000\n";
228 std::cout << " );\n";
229
230 std::cout << "\nBuilder pattern (readable):\n";
231 std::cout << " auto writer = writer_builder()\n";
232 std::cout << " .file(\"app.log\")\n";
233 std::cout << " .buffered(500)\n";
234 std::cout << " .async(20000)\n";
235 std::cout << " .build();\n";
236
237 auto writer = writer_builder()
238 .file("app.log")
239 .buffered(500)
240 .async(20000)
241 .build();
242
243 std::cout << "\nResult: " << writer->get_name() << "\n";
244 std::cout << "Benefits:\n";
245 std::cout << " ✓ More readable and self-documenting\n";
246 std::cout << " ✓ Less error-prone (no manual nesting)\n";
247 std::cout << " ✓ Easy to add/remove decorators\n";
248 std::cout << " ✓ Type-safe at compile time\n";
249
250 if (auto* async_w = dynamic_cast<async_writer*>(writer.get())) {
251 async_w->start();
252 async_w->stop();
253 }
254}

Referenced by main().

Here is the caller graph for this function:

◆ example_filtered_console()

void example_filtered_console ( )

Example 3: Console Writer with Filtering.

Console writer that only logs warnings and errors.

Examples
writer_builder_example.cpp.

Definition at line 103 of file writer_builder_example.cpp.

103 {
104 std::cout << "\n=== Example 3: Filtered Console Writer ===\n";
105
106 auto filter = std::make_unique<level_filter>(log_level::warning);
107
108 auto writer = writer_builder()
109 .console()
110 .filtered(std::move(filter))
111 .build();
112
113 std::cout << "Created filtered console writer: " << writer->get_name() << "\n";
114 std::cout << " - Only logs WARNING and above\n";
115}

Referenced by main().

Here is the caller graph for this function:

◆ example_production_setup()

void example_production_setup ( )

Example 5: Production Setup with Multiple Writers.

Realistic production scenario with multiple log destinations:

  • Main log: All messages with async+buffered
  • Error log: Only errors with separate file

Definition at line 162 of file writer_builder_example.cpp.

162 {
163 std::cout << "\n=== Example 5: Production Multi-Writer Setup ===\n";
164
165 logger log;
166
167 // Main log: async file with buffering
168 auto main_writer = writer_builder()
169 .file("app.log")
170 .buffered(500)
171 .async(20000)
172 .build();
173
174 std::cout << "Main writer: " << main_writer->get_name() << "\n";
175
176 // Start async writer before adding to logger
177 if (auto* async_main = dynamic_cast<async_writer*>(main_writer.get())) {
178 async_main->start();
179 }
180
181 log.add_writer("main", std::move(main_writer));
182
183 // Error log: separate file for errors only
184 auto error_filter = std::make_unique<level_filter>(log_level::error);
185
186 auto error_writer = writer_builder()
187 .file("errors.log")
188 .filtered(std::move(error_filter))
189 .async()
190 .build();
191
192 std::cout << "Error writer: " << error_writer->get_name() << "\n";
193
194 if (auto* async_error = dynamic_cast<async_writer*>(error_writer.get())) {
195 async_error->start();
196 }
197
198 log.add_writer("errors", std::move(error_writer));
199
200 // Log some messages
201 std::cout << "\nLogging messages:\n";
202 log.log(log_level::info, std::string("Application started"));
203 log.log(log_level::warning, std::string("Low disk space"));
204 log.log(log_level::error, std::string("Failed to connect to database"));
205
206 std::cout << " - INFO: Goes to app.log\n";
207 std::cout << " - WARNING: Goes to app.log\n";
208 std::cout << " - ERROR: Goes to both app.log and errors.log\n";
209
210 log.flush();
211}
common::VoidResult add_writer(log_writer_ptr writer)
Definition logger.cpp:265
common::VoidResult log(common::interfaces::log_level level, const std::string &message) override
Log a message with specified level (ILogger interface)
Definition logger.cpp:378
common::VoidResult flush() override
Flush any buffered log messages (ILogger interface)
Definition logger.cpp:574

References kcenon::logger::logger::add_writer(), kcenon::logger::logger::flush(), and kcenon::logger::logger::log().

Referenced by main().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ main()

int main ( )

Definition at line 256 of file writer_builder_example.cpp.

256 {
257 std::cout << "Writer Builder Examples\n";
258 std::cout << "=======================\n";
259 std::cout << "\nThese examples demonstrate how to use writer_builder\n";
260 std::cout << "to compose log writers using the Decorator pattern.\n";
261
262 try {
266
267#ifdef LOGGER_WITH_ENCRYPTION
268 example_encrypted_logging();
269#else
270 std::cout << "\n=== Example 4: Encrypted Logging ===\n";
271 std::cout << "Skipped (LOGGER_WITH_ENCRYPTION not defined)\n";
272#endif
273
276
277 std::cout << "\n=== All Examples Completed Successfully ===\n";
278 } catch (const std::exception& e) {
279 std::cerr << "\nError: " << e.what() << "\n";
280 return 1;
281 }
282
283 return 0;
284}
void example_async_buffered_file()
Example 2: Async + Buffered File Writer.
void example_basic_file_writer()
Example 1: Basic file writer.
void example_builder_vs_manual()
Example 6: Comparing Builder vs Manual Composition.
void example_filtered_console()
Example 3: Console Writer with Filtering.
void example_production_setup()
Example 5: Production Setup with Multiple Writers.

References example_async_buffered_file(), example_basic_file_writer(), example_builder_vs_manual(), example_filtered_console(), and example_production_setup().

Here is the call graph for this function: