Logger System 0.1.3
High-performance C++20 thread-safe logging system with asynchronous capabilities
Loading...
Searching...
No Matches
logger_config.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
12#pragma once
13
14#include <chrono>
15#include <string>
16#include <limits>
19
20// Use common_system's standard interface
21#include <kcenon/common/interfaces/logger_interface.h>
22
23namespace kcenon::logger {
24
25// Type alias for log_level using common::interfaces::log_level as the canonical type
26using log_level = common::interfaces::log_level;
27
38 bool async = true;
39 std::size_t buffer_size = 8192;
40 log_level min_level = log_level::info;
42
45 std::size_t batch_size = 100;
46 std::chrono::milliseconds flush_interval{1000};
47 bool use_lock_free = false;
48 std::size_t max_writers = 10;
49 bool enable_batch_writing = false;
51
54 bool enable_metrics = false;
55 bool enable_crash_handler = false;
57 bool enable_color_output = true;
58 bool enable_timestamp = true;
61
64 std::size_t max_queue_size = 10000;
69 enum class overflow_policy {
72 block,
73 grow
74 };
75 overflow_policy queue_overflow_policy = overflow_policy::drop_newest;
77
80 std::size_t max_file_size = 100 * 1024 * 1024;
81 std::size_t max_file_count = 5;
82 std::string log_directory = "./logs";
83 std::string log_file_prefix = "app";
85
88 std::string remote_host = "";
89 uint16_t remote_port = 0;
90 std::chrono::milliseconds network_timeout{5000};
91 std::size_t network_retry_count = 3;
93
96 std::size_t writer_thread_count = 1;
97 bool enable_compression = false;
98
104 // Validate buffer size
105 if (buffer_size == 0) {
107 "Buffer size must be greater than 0");
108 }
109
110 if (buffer_size > std::numeric_limits<std::size_t>::max() / 2) {
112 "Buffer size is too large");
113 }
114
115 // Validate batch size
116 if (batch_size == 0) {
118 "Batch size must be greater than 0");
119 }
120
121 if (batch_size > buffer_size) {
123 "Batch size cannot exceed buffer size");
124 }
125
126 // Validate flush interval
127 if (flush_interval.count() < 0) {
129 "Flush interval must be non-negative");
130 }
131
132 if (flush_interval.count() > 3600000) { // 1 hour max
134 "Flush interval too large (max 1 hour)");
135 }
136
137 // Validate queue settings
138 if (max_queue_size == 0) {
140 "Max queue size must be greater than 0");
141 }
142
145 "Max queue size must be at least as large as batch size");
146 }
147
148 // Validate file settings
149 if (max_file_size < 1024) { // Minimum 1KB
151 "Max file size too small (minimum 1KB)");
152 }
153
154 if (max_file_count == 0) {
156 "Max file count must be greater than 0");
157 }
158
159 if (max_file_count > 1000) {
161 "Max file count too large (max 1000)");
162 }
163
164 // Validate network settings if configured
165 if (!remote_host.empty()) {
166 if (remote_port == 0) {
168 "Remote port must be specified when remote host is set");
169 }
170
171 if (network_timeout.count() <= 0) {
173 "Network timeout must be positive");
174 }
175
176 if (network_retry_count > 100) {
178 "Network retry count too large (max 100)");
179 }
180 }
181
182 // Validate writer settings
183 if (max_writers == 0) {
185 "Must allow at least one writer");
186 }
187
188 if (max_writers > 100) {
190 "Max writers too large (max 100)");
191 }
192
193 // Validate thread count
194 if (writer_thread_count == 0) {
196 "Writer thread count must be at least 1");
197 }
198
199 if (writer_thread_count > 32) {
201 "Writer thread count too large (max 32)");
202 }
203
204 // Validate feature combinations
205 if (use_lock_free && queue_overflow_policy == overflow_policy::grow) {
207 "Lock-free queue cannot use grow overflow policy");
208 }
209
210 if (!async && batch_size > 1) {
212 "Batch processing requires async mode");
213 }
214
215 return common::ok();
216 }
217
223 return logger_config{};
224 }
225
231 logger_config config;
232 config.async = true;
233 config.buffer_size = 65536;
234 config.batch_size = 500;
235 config.flush_interval = std::chrono::milliseconds(5000);
236 config.use_lock_free = true;
237 config.max_queue_size = 100000;
238 config.writer_thread_count = 2;
239 config.enable_compression = true;
240 config.enable_batch_writing = true;
241 return config;
242 }
243
249 logger_config config;
250 config.async = true;
251 config.buffer_size = 4096;
252 config.batch_size = 10;
253 config.flush_interval = std::chrono::milliseconds(10);
254 config.use_lock_free = true;
255 config.max_queue_size = 10000;
256 config.queue_overflow_policy = overflow_policy::drop_oldest;
257 return config;
258 }
259
265 logger_config config;
266 config.async = false; // Synchronous for immediate output
267 config.min_level = log_level::trace;
268 config.enable_metrics = true;
269 config.enable_crash_handler = true;
270 config.enable_color_output = true;
271 config.batch_size = 1;
272 config.flush_interval = std::chrono::milliseconds(0);
273 return config;
274 }
275
281 logger_config config;
282 config.async = true;
283 config.buffer_size = 16384;
284 config.min_level = log_level::warning;
285 config.enable_metrics = true;
286 config.enable_crash_handler = true;
287 config.enable_color_output = false;
288 config.max_file_size = 500 * 1024 * 1024; // 500MB
289 config.max_file_count = 10;
290 config.enable_compression = true;
291 config.enable_batch_writing = true;
292 config.batch_size = 200;
293 return config;
294 }
295};
296
297} // namespace kcenon::logger
Error codes specific to the logger system.
VoidResult ok()
common::interfaces::log_level log_level
common::VoidResult make_logger_void_result(logger_error_code code, const std::string &message="")
Configuration structure for logger with validation.
log_level min_level
Minimum log level to process.
std::size_t batch_size
Number of messages per batch write.
std::size_t network_retry_count
Number of network retry attempts.
static logger_config low_latency()
Create a low-latency configuration.
common::VoidResult validate() const
Validate the configuration.
std::size_t writer_thread_count
Number of dedicated writer threads.
static logger_config production()
Create a production configuration.
bool enable_batch_writing
Enable batch writing mode.
std::size_t max_file_size
Maximum file size in bytes (default: 100MB).
bool async
Enable asynchronous logging.
std::chrono::milliseconds network_timeout
Network operation timeout.
bool enable_color_output
Enable ANSI color output.
bool enable_timestamp
Include timestamp in log entries.
static logger_config default_config()
Create a default configuration.
overflow_policy queue_overflow_policy
Active overflow policy.
bool enable_source_location
Include source file/line in log entries.
bool enable_compression
Enable log compression.
bool enable_metrics
Enable performance metrics collection.
static logger_config high_performance()
Create a high-performance configuration.
std::string log_directory
Directory for log file output.
std::string remote_host
Remote log collector hostname.
overflow_policy
Policy for handling queue overflow when max_queue_size is reached.
@ drop_newest
Drop new messages when queue is full (default).
@ block
Block until space is available.
@ grow
Dynamically grow the queue (use with caution).
@ drop_oldest
Drop oldest messages when queue is full.
std::chrono::milliseconds flush_interval
Interval between automatic flushes.
std::size_t buffer_size
Internal buffer size in bytes.
uint16_t remote_port
Remote log collector port.
std::size_t max_writers
Maximum number of concurrent writers.
std::size_t max_file_count
Maximum number of rotating log files.
bool enable_crash_handler
Enable crash signal handler.
bool use_lock_free
Use lock-free queue implementation.
std::size_t max_queue_size
Maximum number of queued messages.
bool enable_structured_logging
Enable structured (JSON) log output.
std::string log_file_prefix
Prefix for log file names.
static logger_config debug_config()
Create a debug configuration.
Conditionally enables thread_system integration when available.