Kea: db_log.h Source File

| Kea 3.2.0-git

db_log.h
Go to the documentation of this file.
1// Copyright (C) 2018-2026 Internet Systems Consortium, Inc. ("ISC")
2//
3// This Source Code Form is subject to the terms of the Mozilla Public
4// License, v. 2.0. If a copy of the MPL was not distributed with this
5// file, You can obtain one at http://mozilla.org/MPL/2.0/.
6
7#ifndef DB_LOG_H
8#define DB_LOG_H
9
10#include <log/macros.h>
11
12#include <map>
13#include <mutex>
14#include <list>
15
26
27namespace isc {
28namespace db {
29
36
41extern const int DB_DBG_TRACE_DETAIL;
42
44
46 extern isc::log::Logger database_logger;
47
51 enum DbMessageID {
52 DB_INVALID_ACCESS,
53
54 PGSQL_INITIAL_CONNECTION_FAIL,
55 PGSQL_INITIALIZE_SCHEMA,
56 PGSQL_NO_INIT_NO_ADMIN,
57 PGSQL_NO_INIT_READONLY,
58 PGSQL_DEALLOC_ERROR,
59 PGSQL_FATAL_ERROR,
60 PGSQL_START_TRANSACTION,
61 PGSQL_COMMIT,
62 PGSQL_ROLLBACK,
63 PGSQL_CREATE_SAVEPOINT,
64 PGSQL_ROLLBACK_SAVEPOINT,
65 PGSQL_TCP_USER_TIMEOUT_UNSUPPORTED,
66
67 MYSQL_INITIAL_CONNECTION_FAIL,
68 MYSQL_INITIALIZE_SCHEMA,
69 MYSQL_NO_INIT_NO_ADMIN,
70 MYSQL_NO_INIT_READONLY,
71 MYSQL_FATAL_ERROR,
72 MYSQL_START_TRANSACTION,
73 MYSQL_COMMIT,
74 MYSQL_ROLLBACK,
75};
76
77
80 class DbLogger {
81public:
83 typedef std::map<DbMessageID, isc::log::MessageID> MessageMap;
84
89 DbLogger(isc::log::Logger& logger, const MessageMap& map)
90 : logger_(logger), map_(map) {
91 }
92
98 const isc::log::MessageID& translateMessage(const DbMessageID& id) const;
99
101 isc::log::Logger& logger_;
102
104 const MessageMap& map_;
105};
106
108 typedef std::list<DbLogger> DbLoggerStack;
109
111extern DbLoggerStack db_logger_stack;
112
114extern std::mutex db_logger_mutex;
115
119void checkDbLoggerStack();
120
122 enum log_type_t {
123 fatal,
124 error,
125 warn,
126 info,
127 debug,
128};
129
131template <log_type_t log_type>
132 struct DB_LOG {
135 DB_LOG(DbMessageID const message_id, int const debug_level = 0) {
136 std::lock_guard<std::mutex> lock(isc::db::db_logger_mutex);
137 isc::db::checkDbLoggerStack();
138 if (isEnabled()) {
139 formatter_ = formatter(message_id, debug_level);
140 }
141 }
142
149 template <typename T, typename... Args>
150 DB_LOG& arg(T first, Args... args) {
151 formatter_.arg(first);
152 return arg(args...);
153 }
154
160 DB_LOG& arg() {
161 return *this;
162 }
163
164private:
171 isc::log::Logger::Formatter
172 formatter(DbMessageID const message_id, int const debug_level = 0);
173
179 bool isEnabled(int const debug_level = 0) const;
180
182 isc::log::Logger::Formatter formatter_;
183};
184
187 struct DB_LOG_FATAL : DB_LOG<fatal> {
188 DB_LOG_FATAL(DbMessageID const message_id) : DB_LOG(message_id) {
189 }
190};
191
192 struct DB_LOG_ERROR : DB_LOG<error> {
193 DB_LOG_ERROR(DbMessageID const message_id) : DB_LOG(message_id) {
194 }
195};
196
197 struct DB_LOG_WARN : DB_LOG<warn> {
198 DB_LOG_WARN(DbMessageID const message_id) : DB_LOG(message_id) {
199 }
200};
201
202 struct DB_LOG_INFO : DB_LOG<info> {
203 DB_LOG_INFO(DbMessageID const message_id) : DB_LOG(message_id) {
204 }
205};
206
207 struct DB_LOG_DEBUG : DB_LOG<debug> {
208 DB_LOG_DEBUG(int const debug_level, DbMessageID const message_id)
209 : DB_LOG(message_id, debug_level) {
210 }
211};
212
213
215extern const db::DbLogger::MessageMap db_message_map;
216
218 extern db::DbLogger db_logger_translator;
219
220} // namespace db
221} // namespace isc
222
223#endif // DB_LOG_H
isc::db::DbLogger
Database logger class.
Definition db_log.h:80
isc::db::DbLogger::DbLogger
DbLogger(isc::log::Logger &logger, const MessageMap &map)
Constructor.
Definition db_log.h:89
isc::db::DbLogger::map_
const MessageMap & map_
The translation map.
Definition db_log.h:104
isc::db::DbLogger::translateMessage
const isc::log::MessageID & translateMessage(const DbMessageID &id) const
Translate message.
Definition db_log.cc:59
isc::db::DbLogger::MessageMap
std::map< DbMessageID, isc::log::MessageID > MessageMap
Translation map type.
Definition db_log.h:83
isc::db::DbLogger::logger_
isc::log::Logger & logger_
The logger.
Definition db_log.h:101
isc::log::Logger
Logger Class.
Definition log/logger.h:142
isc::log::Logger::Formatter
isc::log::Formatter< Logger > Formatter
The formatter used to replace placeholders.
Definition log/logger.h:194
macros.h
isc::db
Definition audit_entry.cc:14
isc::db::DB_DBG_TRACE_DETAIL
const int DB_DBG_TRACE_DETAIL
Database logging levels.
Definition db_log.cc:21
isc::db::database_logger
isc::log::Logger database_logger("database")
Common database library logger.
Definition db_log.h:46
isc::db::DbLoggerStack
std::list< DbLogger > DbLoggerStack
Database logger stack.
Definition db_log.h:108
isc::db::DbMessageID
DbMessageID
Database messages.
Definition db_log.h:51
isc::db::DB_INVALID_ACCESS
@ DB_INVALID_ACCESS
Definition db_log.h:52
isc::db::MYSQL_START_TRANSACTION
@ MYSQL_START_TRANSACTION
Definition db_log.h:72
isc::db::MYSQL_NO_INIT_NO_ADMIN
@ MYSQL_NO_INIT_NO_ADMIN
Definition db_log.h:69
isc::db::PGSQL_ROLLBACK_SAVEPOINT
@ PGSQL_ROLLBACK_SAVEPOINT
Definition db_log.h:64
isc::db::PGSQL_CREATE_SAVEPOINT
@ PGSQL_CREATE_SAVEPOINT
Definition db_log.h:63
isc::db::PGSQL_ROLLBACK
@ PGSQL_ROLLBACK
Definition db_log.h:62
isc::db::MYSQL_INITIALIZE_SCHEMA
@ MYSQL_INITIALIZE_SCHEMA
Definition db_log.h:68
isc::db::PGSQL_TCP_USER_TIMEOUT_UNSUPPORTED
@ PGSQL_TCP_USER_TIMEOUT_UNSUPPORTED
Definition db_log.h:65
isc::db::PGSQL_COMMIT
@ PGSQL_COMMIT
Definition db_log.h:61
isc::db::PGSQL_NO_INIT_NO_ADMIN
@ PGSQL_NO_INIT_NO_ADMIN
Definition db_log.h:56
isc::db::PGSQL_START_TRANSACTION
@ PGSQL_START_TRANSACTION
Definition db_log.h:60
isc::db::MYSQL_INITIAL_CONNECTION_FAIL
@ MYSQL_INITIAL_CONNECTION_FAIL
Definition db_log.h:67
isc::db::PGSQL_FATAL_ERROR
@ PGSQL_FATAL_ERROR
Definition db_log.h:59
isc::db::MYSQL_ROLLBACK
@ MYSQL_ROLLBACK
Definition db_log.h:74
isc::db::PGSQL_NO_INIT_READONLY
@ PGSQL_NO_INIT_READONLY
Definition db_log.h:57
isc::db::PGSQL_INITIALIZE_SCHEMA
@ PGSQL_INITIALIZE_SCHEMA
Definition db_log.h:55
isc::db::MYSQL_NO_INIT_READONLY
@ MYSQL_NO_INIT_READONLY
Definition db_log.h:70
isc::db::PGSQL_DEALLOC_ERROR
@ PGSQL_DEALLOC_ERROR
Definition db_log.h:58
isc::db::MYSQL_FATAL_ERROR
@ MYSQL_FATAL_ERROR
Definition db_log.h:71
isc::db::PGSQL_INITIAL_CONNECTION_FAIL
@ PGSQL_INITIAL_CONNECTION_FAIL
Definition db_log.h:54
isc::db::MYSQL_COMMIT
@ MYSQL_COMMIT
Definition db_log.h:73
isc::db::db_logger_stack
DbLoggerStack db_logger_stack
Global database logger stack (initialized to database logger).
Definition db_log.cc:54
isc::db::log_type_t
log_type_t
log type enumerations for use in DB_LOG specializations
Definition db_log.h:122
isc::db::fatal
@ fatal
Definition db_log.h:123
isc::db::info
@ info
Definition db_log.h:126
isc::db::debug
@ debug
Definition db_log.h:127
isc::db::error
@ error
Definition db_log.h:124
isc::db::warn
@ warn
Definition db_log.h:125
isc::db::db_logger_mutex
std::mutex db_logger_mutex
Global mutex to protect logger stack.
Definition db_log.cc:56
isc::db::checkDbLoggerStack
void checkDbLoggerStack()
Check database logger stack.
Definition db_log.cc:67
isc::db::db_message_map
const DbLogger::MessageMap db_message_map
Map of translated messages.
Definition db_log.cc:24
isc::db::db_logger_translator
DbLogger db_logger_translator(database_logger, db_message_map)
Database logger translator.
Definition db_log.h:218
isc::log::MessageID
const char * MessageID
Definition message_types.h:15
isc
Defines the logger used by the top-level component of kea-lfc.
Definition check_exists_add.cc:18
isc::db::DB_LOG_DEBUG::DB_LOG_DEBUG
DB_LOG_DEBUG(int const debug_level, DbMessageID const message_id)
Definition db_log.h:208
isc::db::DB_LOG_ERROR::DB_LOG_ERROR
DB_LOG_ERROR(DbMessageID const message_id)
Definition db_log.h:193
isc::db::DB_LOG_FATAL::DB_LOG_FATAL
DB_LOG_FATAL(DbMessageID const message_id)
Definition db_log.h:188
isc::db::DB_LOG_INFO::DB_LOG_INFO
DB_LOG_INFO(DbMessageID const message_id)
Definition db_log.h:203
isc::db::DB_LOG_WARN::DB_LOG_WARN
DB_LOG_WARN(DbMessageID const message_id)
Definition db_log.h:198
isc::db::DB_LOG::arg
DB_LOG & arg(T first, Args... args)
Pass parameters to replace logger placeholders.
Definition db_log.h:150
isc::db::DB_LOG::arg
DB_LOG & arg()
The last invocation of the arg() which is without parameters.
Definition db_log.h:160
isc::db::DB_LOG::DB_LOG
DB_LOG(DbMessageID const message_id, int const debug_level=0)
To preserve the old way of logging, this constructor facilitates initiating the DB_LOG_* chain call.
Definition db_log.h:135