Logger System 0.1.3
High-performance C++20 thread-safe logging system with asynchronous capabilities
Loading...
Searching...
No Matches
composite_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
17#include "../logger_config.h"
18#include <vector>
19#include <algorithm>
20
21namespace kcenon::logger {
22
33public:
34 std::string get_name() const override {
35 return "composite";
36 }
37
38 void apply(logger_config& config) const override {
39 // Create sorted copy of strategies by priority (descending)
40 std::vector<const config_strategy_interface*> sorted;
41 sorted.reserve(strategies_.size());
42 for (const auto& strategy : strategies_) {
43 sorted.push_back(strategy.get());
44 }
45 std::sort(sorted.begin(), sorted.end(),
46 [](const auto* a, const auto* b) {
47 return a->priority() > b->priority();
48 });
49
50 // Apply each applicable strategy in order
51 for (const auto* strategy : sorted) {
52 if (strategy->is_applicable()) {
53 strategy->apply(config);
54 }
55 }
56 }
57
58 bool is_applicable() const override {
59 // Applicable if any contained strategy is applicable
60 return std::any_of(strategies_.begin(), strategies_.end(),
61 [](const auto& strategy) {
62 return strategy->is_applicable();
63 });
64 }
65
71 composite_strategy& add(std::unique_ptr<config_strategy_interface> strategy) {
72 if (strategy) {
73 strategies_.push_back(std::move(strategy));
74 }
75 return *this;
76 }
77
85 template<typename Strategy, typename... Args>
86 composite_strategy& add(Args&&... args) {
87 strategies_.push_back(
88 std::make_unique<Strategy>(std::forward<Args>(args)...)
89 );
90 return *this;
91 }
92
99 return *this;
100 }
101
106 std::size_t size() const {
107 return strategies_.size();
108 }
109
114 bool empty() const {
115 return strategies_.empty();
116 }
117
118private:
119 std::vector<std::unique_ptr<config_strategy_interface>> strategies_;
120};
121
122} // namespace kcenon::logger
Combines multiple configuration strategies.
composite_strategy & add(Args &&... args)
Add a strategy by constructing it in-place.
composite_strategy & clear()
Clear all strategies.
bool is_applicable() const override
Check if this strategy is applicable in the current context.
std::size_t size() const
Get the number of strategies.
void apply(logger_config &config) const override
Apply this strategy to a logger configuration.
std::string get_name() const override
Get the strategy name.
composite_strategy & add(std::unique_ptr< config_strategy_interface > strategy)
Add a strategy to the composite.
bool empty() const
Check if composite is empty.
std::vector< std::unique_ptr< config_strategy_interface > > strategies_
Abstract interface for logger configuration strategies.
Interface for logger configuration strategies (Strategy Pattern)
Configuration structure for logger with validation.
Configuration structure for logger with validation.