Kea 2.7.6
db_log.h
Go to the documentation of this file.
1// Copyright (C) 2018-2024 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
47
71
74class DbLogger {
75public:
77 typedef std::map<DbMessageID, isc::log::MessageID> MessageMap;
78
84 : logger_(logger), map_(map) {
85 }
86
92 const isc::log::MessageID& translateMessage(const DbMessageID& id) const;
93
96
99};
100
102typedef std::list<DbLogger> DbLoggerStack;
103
106
108extern std::mutex db_logger_mutex;
109
113void checkDbLoggerStack();
114
123
125template <log_type_t log_type>
126struct DB_LOG {
129 DB_LOG(DbMessageID const message_id, int const debug_level = 0) {
130 std::lock_guard<std::mutex> lock(isc::db::db_logger_mutex);
132 if (isEnabled()) {
133 formatter_ = formatter(message_id, debug_level);
134 }
135 }
136
143 template <typename T, typename... Args>
144 DB_LOG& arg(T first, Args... args) {
145 formatter_.arg(first);
146 return arg(args...);
147 }
148
155 return *this;
156 }
157
158private:
166 formatter(DbMessageID const message_id, int const debug_level = 0);
167
173 bool isEnabled(int const debug_level = 0) const;
174
177};
178
181struct DB_LOG_FATAL : DB_LOG<fatal> {
182 DB_LOG_FATAL(DbMessageID const message_id) : DB_LOG(message_id) {
183 }
184};
185
186struct DB_LOG_ERROR : DB_LOG<error> {
187 DB_LOG_ERROR(DbMessageID const message_id) : DB_LOG(message_id) {
188 }
189};
190
191struct DB_LOG_WARN : DB_LOG<warn> {
192 DB_LOG_WARN(DbMessageID const message_id) : DB_LOG(message_id) {
193 }
194};
195
196struct DB_LOG_INFO : DB_LOG<info> {
197 DB_LOG_INFO(DbMessageID const message_id) : DB_LOG(message_id) {
198 }
199};
200
201struct DB_LOG_DEBUG : DB_LOG<debug> {
202 DB_LOG_DEBUG(int const debug_level, DbMessageID const message_id)
203 : DB_LOG(message_id, debug_level) {
204 }
205};
207
210
213
214} // namespace db
215} // namespace isc
216
217#endif // DB_LOG_H
Database logger class.
Definition db_log.h:74
DbLogger(isc::log::Logger &logger, const MessageMap &map)
Constructor.
Definition db_log.h:83
const MessageMap & map_
The translation map.
Definition db_log.h:98
const isc::log::MessageID & translateMessage(const DbMessageID &id) const
Translate message.
Definition db_log.cc:53
isc::log::Logger & logger_
The logger.
Definition db_log.h:95
std::map< DbMessageID, isc::log::MessageID > MessageMap
Translation map type.
Definition db_log.h:77
The log message formatter.
Formatter & arg(const Arg &value)
Replaces another placeholder.
Logger Class.
Definition log/logger.h:142
const int DB_DBG_TRACE_DETAIL
Database logging levels.
Definition db_log.cc:21
isc::log::Logger database_logger("database")
Common database library logger.
Definition db_log.h:46
std::list< DbLogger > DbLoggerStack
Database logger stack.
Definition db_log.h:102
DbMessageID
Database messages.
Definition db_log.h:51
@ DB_INVALID_ACCESS
Definition db_log.h:52
@ MYSQL_START_TRANSACTION
Definition db_log.h:66
@ PGSQL_ROLLBACK_SAVEPOINT
Definition db_log.h:61
@ PGSQL_CREATE_SAVEPOINT
Definition db_log.h:60
@ PGSQL_ROLLBACK
Definition db_log.h:59
@ MYSQL_INITIALIZE_SCHEMA
Definition db_log.h:64
@ PGSQL_TCP_USER_TIMEOUT_UNSUPPORTED
Definition db_log.h:62
@ PGSQL_COMMIT
Definition db_log.h:58
@ PGSQL_START_TRANSACTION
Definition db_log.h:57
@ PGSQL_FATAL_ERROR
Definition db_log.h:56
@ MYSQL_ROLLBACK
Definition db_log.h:68
@ PGSQL_INITIALIZE_SCHEMA
Definition db_log.h:54
@ PGSQL_DEALLOC_ERROR
Definition db_log.h:55
@ MYSQL_FATAL_ERROR
Definition db_log.h:65
@ MYSQL_COMMIT
Definition db_log.h:67
DbLoggerStack db_logger_stack
Global database logger stack (initialized to database logger)
Definition db_log.cc:48
log_type_t
log type enumerations for use in DB_LOG specializations
Definition db_log.h:116
@ fatal
Definition db_log.h:117
@ info
Definition db_log.h:120
@ debug
Definition db_log.h:121
@ error
Definition db_log.h:118
@ warn
Definition db_log.h:119
std::mutex db_logger_mutex
Global mutex to protect logger stack.
Definition db_log.cc:50
void checkDbLoggerStack()
Check database logger stack.
Definition db_log.cc:61
const DbLogger::MessageMap db_message_map
Map of translated messages.
Definition db_log.cc:24
DbLogger db_logger_translator(database_logger, db_message_map)
Database logger translator.
Definition db_log.h:212
const char * MessageID
Defines the logger used by the top-level component of kea-lfc.
DB_LOG_DEBUG(int const debug_level, DbMessageID const message_id)
Definition db_log.h:202
DB_LOG_ERROR(DbMessageID const message_id)
Definition db_log.h:187
all DB_LOG specializations
Definition db_log.h:181
DB_LOG_FATAL(DbMessageID const message_id)
Definition db_log.h:182
DB_LOG_INFO(DbMessageID const message_id)
Definition db_log.h:197
DB_LOG_WARN(DbMessageID const message_id)
Definition db_log.h:192
DB_LOG_* logic.
Definition db_log.h:126
DB_LOG & arg(T first, Args... args)
Pass parameters to replace logger placeholders.
Definition db_log.h:144
DB_LOG & arg()
The last invocation of the arg() which is without parameters.
Definition db_log.h:154
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:129