Database System 0.1.0
Advanced C++20 Database System with Multi-Backend Support
Loading...
Searching...
No Matches
common_logger_backend.cpp
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
6
7#if KCENON_HAS_COMMON_SYSTEM
8#include <kcenon/common/interfaces/global_logger_registry.h>
9#include <kcenon/common/logging/log_macros.h>
10#endif
11
12#include <stdexcept>
13#include <string>
14
15namespace
16{
17 inline common::VoidResult make_error(const std::string& msg, int code = -1)
18 {
19 return common::VoidResult(common::error_info{ code, msg, "" });
20 }
21}
22
23namespace database
24{
25namespace integrated
26{
27namespace adapters
28{
29namespace backends
30{
31
33 : config_(config), initialized_(false)
34{
35}
36
44
46{
47 if (initialized_)
48 {
49 return common::ok();
50 }
51
52#if KCENON_HAS_COMMON_SYSTEM
53 try
54 {
55 // The GlobalLoggerRegistry is already initialized by common_system.
56 // We just need to verify we can access it.
57 auto& registry = kcenon::common::interfaces::GlobalLoggerRegistry::instance();
58
59 // Check if a default logger is available
60 if (!registry.has_default_logger())
61 {
62 // No default logger registered, but that's okay.
63 // LOG_* macros will use NullLogger as fallback.
64 }
65
66 initialized_ = true;
67 return common::ok();
68 }
69 catch (const std::exception& e)
70 {
71 return make_error(std::string("Logger initialization failed: ") + e.what());
72 }
73#else
74 return make_error("common_system not available");
75#endif
76}
77
79{
80 if (!initialized_)
81 {
82 return common::ok();
83 }
84
85 // GlobalLoggerRegistry is a singleton managed by common_system.
86 // We don't need to shut it down here.
87 initialized_ = false;
88 return common::ok();
89}
90
95
96void common_logger_backend::log(db_log_level level, const std::string& message)
97{
98 if (!initialized_)
99 {
100 return;
101 }
102
103 // Check if this level should be logged
104 if (level < config_.min_log_level)
105 {
106 return;
107 }
108
109#if KCENON_HAS_COMMON_SYSTEM
110 // Use common_system's logging functions
111 auto common_level = convert_log_level(level);
112 kcenon::common::logging::log(common_level, message);
113#endif
114}
115
117{
118#if KCENON_HAS_COMMON_SYSTEM
119 if (initialized_)
120 {
121 kcenon::common::logging::flush();
122 }
123#endif
124}
125
126#if KCENON_HAS_COMMON_SYSTEM
127kcenon::common::interfaces::log_level common_logger_backend::convert_log_level(db_log_level level)
128{
129 switch (level)
130 {
132 return kcenon::common::interfaces::log_level::trace;
134 return kcenon::common::interfaces::log_level::debug;
136 return kcenon::common::interfaces::log_level::info;
138 return kcenon::common::interfaces::log_level::warning;
140 return kcenon::common::interfaces::log_level::error;
143 return kcenon::common::interfaces::log_level::critical;
144 default:
145 return kcenon::common::interfaces::log_level::info;
146 }
147}
148#endif
149
150} // namespace backends
151} // namespace adapters
152} // namespace integrated
153} // namespace database
void log(db_log_level level, const std::string &message) override
Log a message.
common::VoidResult initialize() override
Initialize the logger backend.
common::VoidResult shutdown() override
Shutdown the logger backend gracefully.
bool is_initialized() const override
Check if backend is initialized.
common_logger_backend(const db_logger_config &config)
Construct common logger backend.
Logger backend using common_system's ILogger and GlobalLoggerRegistry.
VoidResult ok()
Result< std::monostate > VoidResult
db_log_level
Database logging level enumeration.
@ trace
Most verbose, includes all operations.
@ critical
Critical failures requiring immediate attention.
@ debug
Debug information for development.
@ info
Informational messages (default)
@ fatal
Fatal errors causing system shutdown.
db_log_level min_log_level
Minimum log level to output.