Logger System 0.1.3
High-performance C++20 thread-safe logging system with asynchronous capabilities
Loading...
Searching...
No Matches
writer_factory.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
16#include <memory>
17#include <string>
18#include <unordered_map>
19#include <functional>
20#include <chrono>
21
28
29namespace kcenon::logger {
30
41public:
42 using creator_fn = std::function<log_writer_ptr()>;
43
44 //========================================================================
45 // Static Factory Methods
46 //========================================================================
47
55 bool use_stderr = false,
56 bool auto_detect_color = true
57 ) {
58 return std::make_unique<console_writer>(use_stderr, auto_detect_color);
59 }
60
69 const std::string& filename,
70 bool append = true,
71 std::size_t buffer_size = 8192
72 ) {
73 return std::make_unique<file_writer>(filename, append, buffer_size);
74 }
75
85 const std::string& filename,
86 std::size_t max_size,
87 std::size_t max_files,
88 std::size_t check_interval = 100
89 ) {
90 return std::make_unique<rotating_file_writer>(
91 filename, max_size, max_files, check_interval
92 );
93 }
94
104 const std::string& filename,
105 rotation_type type,
106 std::size_t max_files,
107 std::size_t check_interval = 100
108 ) {
109 return std::make_unique<rotating_file_writer>(
110 filename, type, max_files, check_interval
111 );
112 }
113
124 const std::string& host,
125 uint16_t port,
127 std::size_t buffer_size = 8192,
128 std::chrono::seconds reconnect_interval = std::chrono::seconds(5)
129 ) {
130 return std::make_unique<network_writer>(
131 host, port, protocol, buffer_size, reconnect_interval
132 );
133 }
134
143 log_writer_ptr writer,
144 std::size_t batch_size = 100,
145 std::chrono::milliseconds flush_interval = std::chrono::milliseconds(1000)
146 ) {
148 cfg.max_batch_size = batch_size;
149 cfg.flush_interval = flush_interval;
150 return std::make_unique<batch_writer>(std::move(writer), cfg);
151 }
152
153 //========================================================================
154 // Preset Factory Methods
155 //========================================================================
156
162 return create_console(false, true);
163 }
164
172 const std::string& log_directory = "./logs",
173 const std::string& filename = "app.log"
174 ) {
175 std::string full_path = log_directory + "/" + filename;
176 auto rotating = create_rotating_file(
177 full_path,
178 100 * 1024 * 1024, // 100MB
179 10
180 );
181 return create_batch(std::move(rotating), 200, std::chrono::milliseconds(2000));
182 }
183
190 const std::string& filename = "./logs/app.log"
191 ) {
192 auto file = std::make_unique<file_writer>(filename, true, 65536);
193 return create_batch(std::move(file), 500, std::chrono::milliseconds(5000));
194 }
195
196 //========================================================================
197 // Registry Pattern
198 //========================================================================
199
205 static void register_type(const std::string& name, creator_fn creator) {
206 registry()[name] = std::move(creator);
207 }
208
214 static log_writer_ptr create(const std::string& name) {
215 auto it = registry().find(name);
216 if (it == registry().end()) {
217 return nullptr;
218 }
219 return it->second();
220 }
221
227 static bool has_type(const std::string& name) {
228 return registry().find(name) != registry().end();
229 }
230
231private:
232 static std::unordered_map<std::string, creator_fn>& registry() {
233 static std::unordered_map<std::string, creator_fn> instance;
234 return instance;
235 }
236};
237
238} // namespace kcenon::logger
Abstract base class for all log output writers kcenon.
Batch writer that accumulates log entries and writes them in batches.
Factory for creating log writer instances.
static void register_type(const std::string &name, creator_fn creator)
Register a custom writer type.
static log_writer_ptr create_production(const std::string &log_directory="./logs", const std::string &filename="app.log")
Create a production preset writer.
static log_writer_ptr create_rotating_file(const std::string &filename, std::size_t max_size, std::size_t max_files, std::size_t check_interval=100)
Create a rotating file writer (size-based)
static log_writer_ptr create_console(bool use_stderr=false, bool auto_detect_color=true)
Create a console writer.
std::function< log_writer_ptr()> creator_fn
static std::unordered_map< std::string, creator_fn > & registry()
static log_writer_ptr create_high_performance(const std::string &filename="./logs/app.log")
Create a high-performance preset writer.
static log_writer_ptr create_file(const std::string &filename, bool append=true, std::size_t buffer_size=8192)
Create a file writer.
static log_writer_ptr create_development()
Create a development preset writer.
static log_writer_ptr create_rotating_file(const std::string &filename, rotation_type type, std::size_t max_files, std::size_t check_interval=100)
Create a rotating file writer (time-based)
static bool has_type(const std::string &name)
Check if a writer type is registered.
static log_writer_ptr create_batch(log_writer_ptr writer, std::size_t batch_size=100, std::chrono::milliseconds flush_interval=std::chrono::milliseconds(1000))
Wrap a writer with batch processing.
static log_writer_ptr create(const std::string &name)
Create a writer by registered name.
static log_writer_ptr create_network(const std::string &host, uint16_t port, network_writer::protocol_type protocol=network_writer::protocol_type::tcp, std::size_t buffer_size=8192, std::chrono::seconds reconnect_interval=std::chrono::seconds(5))
Create a network writer.
Console writer for logging to stdout/stderr.
File writer for logging to files with optional buffering.
rotation_type
Determines when log rotation should occur.
std::unique_ptr< log_writer_interface > log_writer_ptr
Type alias for writer unique pointer.
Network writer for sending logs over TCP/UDP.
Rotating file writer with size and time-based rotation.
Configuration for batch writer.
std::chrono::milliseconds flush_interval