Logger System 0.1.3
High-performance C++20 thread-safe logging system with asynchronous capabilities
Loading...
Searching...
No Matches
environment_strategy.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
23#include "../logger_config.h"
24#include <cstdlib>
25#include <string>
26#include <algorithm>
27
28namespace kcenon::logger {
29
41public:
42 std::string get_name() const override {
43 return "environment";
44 }
45
46 void apply(logger_config& config) const override {
47 // LOG_LEVEL
48 if (const char* level = std::getenv("LOG_LEVEL")) {
49 config.min_level = parse_log_level(level);
50 }
51
52 // LOG_ASYNC
53 if (const char* async_str = std::getenv("LOG_ASYNC")) {
54 config.async = parse_bool(async_str);
55 if (!config.async && config.batch_size > 1) {
56 config.batch_size = 1;
57 }
58 }
59
60 // LOG_BUFFER_SIZE
61 if (const char* buffer = std::getenv("LOG_BUFFER_SIZE")) {
62 config.buffer_size = parse_size(buffer, config.buffer_size);
63 }
64
65 // LOG_BATCH_SIZE
66 if (const char* batch = std::getenv("LOG_BATCH_SIZE")) {
67 config.batch_size = parse_size(batch, config.batch_size);
68 }
69
70 // LOG_FLUSH_INTERVAL
71 if (const char* interval = std::getenv("LOG_FLUSH_INTERVAL")) {
72 config.flush_interval = std::chrono::milliseconds(
73 parse_size(interval, static_cast<std::size_t>(config.flush_interval.count()))
74 );
75 }
76
77 // LOG_COLOR
78 if (const char* color = std::getenv("LOG_COLOR")) {
79 config.enable_color_output = parse_bool(color);
80 }
81
82 // LOG_METRICS
83 if (const char* metrics = std::getenv("LOG_METRICS")) {
84 config.enable_metrics = parse_bool(metrics);
85 }
86
87 // LOG_STRUCTURED
88 if (const char* structured = std::getenv("LOG_STRUCTURED")) {
89 config.enable_structured_logging = parse_bool(structured);
90 }
91
92 // LOG_CRASH_HANDLER
93 if (const char* crash = std::getenv("LOG_CRASH_HANDLER")) {
94 config.enable_crash_handler = parse_bool(crash);
95 }
96
97 // LOG_MAX_QUEUE_SIZE
98 if (const char* queue = std::getenv("LOG_MAX_QUEUE_SIZE")) {
99 config.max_queue_size = parse_size(queue, config.max_queue_size);
100 }
101
102 // LOG_BATCH_WRITING
103 if (const char* batch_write = std::getenv("LOG_BATCH_WRITING")) {
104 config.enable_batch_writing = parse_bool(batch_write);
105 }
106 }
107
108 bool is_applicable() const override {
109 // Applicable if any relevant environment variable is set
110 return std::getenv("LOG_LEVEL") != nullptr ||
111 std::getenv("LOG_ASYNC") != nullptr ||
112 std::getenv("LOG_BUFFER_SIZE") != nullptr ||
113 std::getenv("LOG_BATCH_SIZE") != nullptr ||
114 std::getenv("LOG_FLUSH_INTERVAL") != nullptr ||
115 std::getenv("LOG_COLOR") != nullptr ||
116 std::getenv("LOG_METRICS") != nullptr;
117 }
118
119 int priority() const override {
120 // High priority - environment overrides other strategies
121 return 100;
122 }
123
124private:
125 static log_level parse_log_level(const char* str) {
126 std::string level_str(str);
127 std::transform(level_str.begin(), level_str.end(), level_str.begin(), ::tolower);
128
129 if (level_str == "trace") return log_level::trace;
130 if (level_str == "debug") return log_level::debug;
131 if (level_str == "info") return log_level::info;
132 if (level_str == "warn" || level_str == "warning") return log_level::warning;
133 if (level_str == "error") return log_level::error;
134 if (level_str == "fatal" || level_str == "critical") return log_level::critical;
135
136 return log_level::info; // Default
137 }
138
139 static bool parse_bool(const char* str) {
140 std::string value(str);
141 std::transform(value.begin(), value.end(), value.begin(), ::tolower);
142 return value == "true" || value == "1" || value == "yes" || value == "on";
143 }
144
145 static std::size_t parse_size(const char* str, std::size_t default_value) {
146 try {
147 return std::stoull(str);
148 } catch (...) {
149 return default_value;
150 }
151 }
152};
153
154} // namespace kcenon::logger
Abstract interface for logger configuration strategies.
Configuration strategy based on environment variables.
static log_level parse_log_level(const char *str)
bool is_applicable() const override
Check if this strategy is applicable in the current context.
static bool parse_bool(const char *str)
static std::size_t parse_size(const char *str, std::size_t default_value)
int priority() const override
Get the strategy priority.
void apply(logger_config &config) const override
Apply this strategy to a logger configuration.
std::string get_name() const override
Get the strategy name.
Interface for logger configuration strategies (Strategy Pattern)
Configuration structure for logger with validation.
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.
bool enable_batch_writing
Enable batch writing mode.
bool async
Enable asynchronous logging.
bool enable_color_output
Enable ANSI color output.
bool enable_metrics
Enable performance metrics collection.
std::chrono::milliseconds flush_interval
Interval between automatic flushes.
std::size_t buffer_size
Internal buffer size in bytes.
bool enable_crash_handler
Enable crash signal handler.
std::size_t max_queue_size
Maximum number of queued messages.
bool enable_structured_logging
Enable structured (JSON) log output.