Logger System 0.1.3
High-performance C++20 thread-safe logging system with asynchronous capabilities
Loading...
Searching...
No Matches
thread_system_monitor_adapter.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 "basic_monitor.h"
17
19
20#ifdef USE_THREAD_SYSTEM
21#include <kcenon/thread/interfaces/monitorable_interface.h>
22
23namespace kcenon::logger {
24
33private:
34 kcenon::thread::monitorable_interface* monitorable_;
35 std::unique_ptr<basic_monitor> fallback_monitor_;
37 std::atomic<bool> enabled_{true};
38
39public:
45 kcenon::thread::monitorable_interface* monitorable)
46 : monitorable_(monitorable),
47 fallback_monitor_(std::make_unique<basic_monitor>()),
48 owns_monitorable_(false) {
49 if (!monitorable_) {
50 throw std::invalid_argument("Monitorable cannot be null");
51 }
52 }
53
58 : monitorable_(nullptr),
59 fallback_monitor_(std::make_unique<basic_monitor>()),
60 owns_monitorable_(false) {}
61
67 delete monitorable_;
68 }
69 }
70
76 if (!enabled_.load()) {
78 }
79
80 // Try thread_system first
81 if (monitorable_) {
82 try {
83 auto ts_metrics = monitorable_->get_metrics();
84 if (ts_metrics) {
85 // Convert thread_system metrics to our format
86 monitoring_data data;
87
88 // Extract metrics from thread_system monitoring_data
89 // Note: This assumes thread_system has similar metric structure
90 for (const auto& [key, value] : ts_metrics.value().get_values()) {
91 data.add_metric(key, value);
92 }
93
94 // Also add fallback monitor metrics for logger-specific data
96 auto fallback_result = fallback_monitor_->collect_metrics();
97 if (fallback_result) {
98 for (const auto& metric : fallback_result.value().get_metrics()) {
99 data.add_metric(metric);
100 }
101 }
102 }
103
104 return data;
105 }
106 } catch (...) {
107 // Fall through to fallback
108 }
109 }
110
111 // Fallback to basic monitor
112 if (fallback_monitor_) {
113 return fallback_monitor_->collect_metrics();
114 }
115
117 }
118
125
126 // Check thread_system health if available
127 if (monitorable_) {
128 try {
129 auto ts_health = monitorable_->get_health_status();
130 if (ts_health) {
131 // Convert thread_system health to our format
132 auto status = ts_health.value();
133
134 if (!status.is_healthy()) {
135 result.set_status(health_status::degraded);
136 result.add_issue("Thread system reports unhealthy status");
137
138 for (const auto& issue : status.get_issues()) {
139 result.add_issue("Thread system: " + issue);
140 }
141 }
142 }
143 } catch (...) {
144 result.add_issue("Failed to check thread_system health");
145 }
146 }
147
148 // Also check fallback monitor for logger-specific health
149 if (fallback_monitor_) {
150 auto fallback_health = fallback_monitor_->check_health();
151 if (fallback_health) {
152 auto& fb_result = fallback_health.value();
153
154 // Merge health status
155 if (!fb_result.is_healthy()) {
156 if (result.get_status() == health_status::healthy) {
157 result.set_status(fb_result.get_status());
158 } else if (fb_result.get_status() == health_status::unhealthy) {
159 result.set_status(health_status::unhealthy);
160 }
161
162 // Add logger-specific issues
163 for (const auto& issue : fb_result.get_issues()) {
164 result.add_issue(issue);
165 }
166 }
167 }
168 }
169
170 // Set overall message
171 if (result.is_healthy()) {
172 result.set_message("All systems operational");
173 } else {
174 result.set_message("Issues detected in monitoring");
175 }
176
177 return result;
178 }
179
186
187 // Reset thread_system metrics if available
188 if (monitorable_) {
189 try {
190 auto ts_result = monitorable_->reset_metrics();
191 if (ts_result.is_err()) {
193 }
194 } catch (...) {
196 }
197 }
198
199 // Reset fallback monitor
200 if (fallback_monitor_) {
201 auto fb_result = fallback_monitor_->reset_metrics();
202 if (fb_result.is_err() && result.is_ok()) {
203 result = fb_result;
204 }
205 }
206
207 return result;
208 }
209
215 common::VoidResult set_enabled(bool enable) override {
216 enabled_ = enable;
217
218 if (fallback_monitor_) {
219 fallback_monitor_->set_enabled(enable);
220 }
221
222 return common::ok();
223 }
224
229 bool is_enabled() const override {
230 return enabled_.load();
231 }
232
237 std::string get_backend_name() const override {
238 if (monitorable_) {
239 return "thread_system+basic";
240 }
241 return "basic(via adapter)";
242 }
243
249 void increment_counter(const std::string& name, double value = 1.0) override {
250 if (!enabled_.load()) return;
251
252 // Always use fallback for logger-specific counters
253 if (fallback_monitor_) {
254 fallback_monitor_->increment_counter(name, value);
255 }
256
257 // Also update thread_system if it supports this
258 if (monitorable_) {
259 try {
260 monitorable_->update_metric(name, value);
261 } catch (...) {
262 // Ignore failures
263 }
264 }
265 }
266
272 void update_gauge(const std::string& name, double value) override {
273 if (!enabled_.load()) return;
274
275 // Always use fallback for logger-specific gauges
276 if (fallback_monitor_) {
277 fallback_monitor_->update_gauge(name, value);
278 }
279
280 // Also update thread_system if it supports this
281 if (monitorable_) {
282 try {
283 monitorable_->update_metric(name, value);
284 } catch (...) {
285 // Ignore failures
286 }
287 }
288 }
289
295 void record_histogram(const std::string& name, double value) override {
296 if (!enabled_.load()) return;
297
298 // Always use fallback for histograms
299 if (fallback_monitor_) {
300 fallback_monitor_->record_histogram(name, value);
301 }
302 }
303
309 void set_monitorable(kcenon::thread::monitorable_interface* monitorable,
310 bool take_ownership = false) {
312 delete monitorable_;
313 }
314
315 monitorable_ = monitorable;
316 owns_monitorable_ = take_ownership;
317 }
318
326};
327
328} // namespace kcenon::logger
329
330#endif // USE_THREAD_SYSTEM
Basic monitoring implementation with no external dependencies.
Basic monitoring implementation.
void add_metric(const std::string &name, double value, metric_type type=metric_type::gauge)
Add a metric to the collection.
Adapter to use thread_system's monitoring capabilities.
basic_monitor * get_fallback_monitor()
Get the fallback monitor for direct access.
thread_system_monitor_adapter()
Default constructor with fallback to basic monitor.
kcenon::thread::monitorable_interface * monitorable_
result< monitoring_data > collect_metrics() const override
Collect current metrics.
bool is_enabled() const override
Check if monitoring is enabled.
common::VoidResult set_enabled(bool enable) override
Enable or disable monitoring.
void update_gauge(const std::string &name, double value) override
Update a gauge.
thread_system_monitor_adapter(kcenon::thread::monitorable_interface *monitorable)
Constructor with external monitorable.
common::VoidResult reset_metrics() override
Reset all metrics.
void record_histogram(const std::string &name, double value) override
Record a histogram value.
result< health_check_result > check_health() const override
Perform health check.
void increment_counter(const std::string &name, double value=1.0) override
Increment a counter.
void set_monitorable(kcenon::thread::monitorable_interface *monitorable, bool take_ownership=false)
Set the thread_system monitorable.
std::string get_backend_name() const override
Get backend name.
Abstract interface for monitoring and metrics collection.
VoidResult ok()
common::VoidResult make_logger_void_result(logger_error_code code, const std::string &message="")
Conditionally enables thread_system integration when available.