| Kea 3.0.2-git

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
27 namespace isc {
28 namespace db {
29
36
41 extern 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_INITIALIZE_SCHEMA,
55 PGSQL_DEALLOC_ERROR,
56 PGSQL_FATAL_ERROR,
57 PGSQL_START_TRANSACTION,
58 PGSQL_COMMIT,
59 PGSQL_ROLLBACK,
60 PGSQL_CREATE_SAVEPOINT,
61 PGSQL_ROLLBACK_SAVEPOINT,
62 PGSQL_TCP_USER_TIMEOUT_UNSUPPORTED,
63
64 MYSQL_INITIALIZE_SCHEMA,
65 MYSQL_FATAL_ERROR,
66 MYSQL_START_TRANSACTION,
67 MYSQL_COMMIT,
68 MYSQL_ROLLBACK,
69};
70
71
74 class DbLogger {
75 public:
77 typedef std::map<DbMessageID, isc::log::MessageID> MessageMap;
78
83 DbLogger(isc::log::Logger& logger, const MessageMap& map)
84 : logger_(logger), map_(map) {
85 }
86
92 const isc::log::MessageID& translateMessage(const DbMessageID& id) const;
93
95 isc::log::Logger& logger_;
96
98 const MessageMap& map_;
99};
100
102 typedef std::list<DbLogger> DbLoggerStack;
103
105 extern DbLoggerStack db_logger_stack;
106
108 extern std::mutex db_logger_mutex;
109
113 void checkDbLoggerStack();
114
116 enum log_type_t {
117 fatal,
118 error,
119 warn,
120 info,
121 debug,
122};
123
125 template <log_type_t log_type>
126 struct 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);
131 isc::db::checkDbLoggerStack();
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
154 DB_LOG& arg() {
155 return *this;
156 }
157
158 private:
165 isc::log::Logger::Formatter
166 formatter(DbMessageID const message_id, int const debug_level = 0);
167
173 bool isEnabled(int const debug_level = 0) const;
174
176 isc::log::Logger::Formatter formatter_;
177};
178
181 struct DB_LOG_FATAL : DB_LOG<fatal> {
182 DB_LOG_FATAL(DbMessageID const message_id) : DB_LOG(message_id) {
183 }
184};
185
186 struct DB_LOG_ERROR : DB_LOG<error> {
187 DB_LOG_ERROR(DbMessageID const message_id) : DB_LOG(message_id) {
188 }
189};
190
191 struct DB_LOG_WARN : DB_LOG<warn> {
192 DB_LOG_WARN(DbMessageID const message_id) : DB_LOG(message_id) {
193 }
194};
195
196 struct DB_LOG_INFO : DB_LOG<info> {
197 DB_LOG_INFO(DbMessageID const message_id) : DB_LOG(message_id) {
198 }
199};
200
201 struct 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};
206
207
209 extern const db::DbLogger::MessageMap db_message_map;
210
212 extern db::DbLogger db_logger_translator;
213
214} // namespace db
215} // namespace isc
216
217#endif // DB_LOG_H
isc::db::DbLogger
Database logger class.
Definition db_log.h:74
isc::db::DbLogger::DbLogger
DbLogger(isc::log::Logger &logger, const MessageMap &map)
Constructor.
Definition db_log.h:83
isc::db::DbLogger::map_
const MessageMap & map_
The translation map.
Definition db_log.h:98
isc::db::DbLogger::translateMessage
const isc::log::MessageID & translateMessage(const DbMessageID &id) const
Translate message.
Definition db_log.cc:53
isc::db::DbLogger::MessageMap
std::map< DbMessageID, isc::log::MessageID > MessageMap
Translation map type.
Definition db_log.h:77
isc::db::DbLogger::logger_
isc::log::Logger & logger_
The logger.
Definition db_log.h:95
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:102
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:66
isc::db::PGSQL_ROLLBACK_SAVEPOINT
@ PGSQL_ROLLBACK_SAVEPOINT
Definition db_log.h:61
isc::db::PGSQL_CREATE_SAVEPOINT
@ PGSQL_CREATE_SAVEPOINT
Definition db_log.h:60
isc::db::PGSQL_ROLLBACK
@ PGSQL_ROLLBACK
Definition db_log.h:59
isc::db::MYSQL_INITIALIZE_SCHEMA
@ MYSQL_INITIALIZE_SCHEMA
Definition db_log.h:64
isc::db::PGSQL_TCP_USER_TIMEOUT_UNSUPPORTED
@ PGSQL_TCP_USER_TIMEOUT_UNSUPPORTED
Definition db_log.h:62
isc::db::PGSQL_COMMIT
@ PGSQL_COMMIT
Definition db_log.h:58
isc::db::PGSQL_START_TRANSACTION
@ PGSQL_START_TRANSACTION
Definition db_log.h:57
isc::db::PGSQL_FATAL_ERROR
@ PGSQL_FATAL_ERROR
Definition db_log.h:56
isc::db::MYSQL_ROLLBACK
@ MYSQL_ROLLBACK
Definition db_log.h:68
isc::db::PGSQL_INITIALIZE_SCHEMA
@ PGSQL_INITIALIZE_SCHEMA
Definition db_log.h:54
isc::db::PGSQL_DEALLOC_ERROR
@ PGSQL_DEALLOC_ERROR
Definition db_log.h:55
isc::db::MYSQL_FATAL_ERROR
@ MYSQL_FATAL_ERROR
Definition db_log.h:65
isc::db::MYSQL_COMMIT
@ MYSQL_COMMIT
Definition db_log.h:67
isc::db::db_logger_stack
DbLoggerStack db_logger_stack
Global database logger stack (initialized to database logger).
Definition db_log.cc:48
isc::db::log_type_t
log_type_t
log type enumerations for use in DB_LOG specializations
Definition db_log.h:116
isc::db::fatal
@ fatal
Definition db_log.h:117
isc::db::info
@ info
Definition db_log.h:120
isc::db::debug
@ debug
Definition db_log.h:121
isc::db::error
@ error
Definition db_log.h:118
isc::db::warn
@ warn
Definition db_log.h:119
isc::db::db_logger_mutex
std::mutex db_logger_mutex
Global mutex to protect logger stack.
Definition db_log.cc:50
isc::db::checkDbLoggerStack
void checkDbLoggerStack()
Check database logger stack.
Definition db_log.cc:61
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:212
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 agent_parser.cc:148
isc::db::DB_LOG_DEBUG::DB_LOG_DEBUG
DB_LOG_DEBUG(int const debug_level, DbMessageID const message_id)
Definition db_log.h:202
isc::db::DB_LOG_ERROR::DB_LOG_ERROR
DB_LOG_ERROR(DbMessageID const message_id)
Definition db_log.h:187
isc::db::DB_LOG_FATAL::DB_LOG_FATAL
DB_LOG_FATAL(DbMessageID const message_id)
Definition db_log.h:182
isc::db::DB_LOG_INFO::DB_LOG_INFO
DB_LOG_INFO(DbMessageID const message_id)
Definition db_log.h:197
isc::db::DB_LOG_WARN::DB_LOG_WARN
DB_LOG_WARN(DbMessageID const message_id)
Definition db_log.h:192
isc::db::DB_LOG::arg
DB_LOG & arg(T first, Args... args)
Pass parameters to replace logger placeholders.
Definition db_log.h:144
isc::db::DB_LOG::arg
DB_LOG & arg()
The last invocation of the arg() which is without parameters.
Definition db_log.h:154
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:129