Kea: db_log.cc Source File

| Kea 3.2.0-git

db_log.cc
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
8
9#include <config.h>
10
11#include <exceptions/exceptions.h>
12#include <database/db_log.h>
13#include <database/db_messages.h>
14
15using namespace isc::log;
16
17namespace isc {
18namespace db {
19
21 const int DB_DBG_TRACE_DETAIL = isc::log::DBGLVL_TRACE_DETAIL;
22
24 const DbLogger::MessageMap db_message_map = {
25 { DB_INVALID_ACCESS, DATABASE_INVALID_ACCESS },
26
27 { PGSQL_INITIAL_CONNECTION_FAIL, DATABASE_PGSQL_INITIAL_CONNECTION_FAIL },
28 { PGSQL_INITIALIZE_SCHEMA, DATABASE_PGSQL_INITIALIZE_SCHEMA },
29 { PGSQL_NO_INIT_NO_ADMIN, DATABASE_PGSQL_NO_INIT_NO_ADMIN },
30 { PGSQL_NO_INIT_READONLY, DATABASE_PGSQL_NO_INIT_READONLY },
31 { PGSQL_DEALLOC_ERROR, DATABASE_PGSQL_DEALLOC_ERROR },
32 { PGSQL_FATAL_ERROR, DATABASE_PGSQL_FATAL_ERROR },
33 { PGSQL_START_TRANSACTION, DATABASE_PGSQL_START_TRANSACTION },
34 { PGSQL_COMMIT, DATABASE_PGSQL_COMMIT },
35 { PGSQL_ROLLBACK, DATABASE_PGSQL_ROLLBACK },
36 { PGSQL_CREATE_SAVEPOINT, DATABASE_PGSQL_CREATE_SAVEPOINT },
37 { PGSQL_ROLLBACK_SAVEPOINT, DATABASE_PGSQL_ROLLBACK_SAVEPOINT },
38 { PGSQL_TCP_USER_TIMEOUT_UNSUPPORTED, DATABASE_PGSQL_TCP_USER_TIMEOUT_UNSUPPORTED },
39
40 { MYSQL_INITIAL_CONNECTION_FAIL, DATABASE_MYSQL_INITIAL_CONNECTION_FAIL },
41 { MYSQL_INITIALIZE_SCHEMA, DATABASE_MYSQL_INITIALIZE_SCHEMA },
42 { MYSQL_NO_INIT_NO_ADMIN, DATABASE_MYSQL_NO_INIT_NO_ADMIN },
43 { MYSQL_NO_INIT_READONLY, DATABASE_MYSQL_NO_INIT_READONLY },
44 { MYSQL_FATAL_ERROR, DATABASE_MYSQL_FATAL_ERROR },
45 { MYSQL_START_TRANSACTION, DATABASE_MYSQL_START_TRANSACTION },
46 { MYSQL_COMMIT, DATABASE_MYSQL_COMMIT },
47 { MYSQL_ROLLBACK, DATABASE_MYSQL_ROLLBACK },
48};
49
50 isc::log::Logger database_logger("database");
51
52DbLogger db_logger_translator(database_logger, db_message_map);
53
54 DbLoggerStack db_logger_stack = { db_logger_translator };
55
56std::mutex db_logger_mutex;
57
58const MessageID&
59DbLogger::translateMessage(const DbMessageID& id) const {
60 try {
61 return (map_.at(id));
62 } catch (const std::out_of_range&) {
63 isc_throw(isc::Unexpected, "can't map message: " << id);
64 }
65}
66
67 void checkDbLoggerStack() {
68 if (db_logger_stack.empty()) {
69 isc_throw(isc::Unexpected, "database logger stack is empty");
70 }
71}
72
73template <>
74 isc::log::Logger::Formatter
75DB_LOG<fatal>::formatter(DbMessageID const message_id,
76 int const /* debug_level = 0 */) {
77 return isc::db::db_logger_stack.back().logger_.fatal(
78 isc::db::db_logger_stack.back().translateMessage(message_id));
79}
80
81template <>
82 isc::log::Logger::Formatter
83DB_LOG<error>::formatter(DbMessageID const message_id,
84 int const /* debug_level = 0 */) {
85 return isc::db::db_logger_stack.back().logger_.error(
86 isc::db::db_logger_stack.back().translateMessage(message_id));
87}
88
89template <>
90 isc::log::Logger::Formatter
91DB_LOG<warn>::formatter(DbMessageID const message_id,
92 int const /* debug_level = 0 */) {
93 return isc::db::db_logger_stack.back().logger_.warn(
94 isc::db::db_logger_stack.back().translateMessage(message_id));
95}
96
97template <>
98 isc::log::Logger::Formatter
99DB_LOG<info>::formatter(DbMessageID const message_id,
100 int const /* debug_level = 0 */) {
101 return isc::db::db_logger_stack.back().logger_.info(
102 isc::db::db_logger_stack.back().translateMessage(message_id));
103}
104
105template <>
106 isc::log::Logger::Formatter
107DB_LOG<debug>::formatter(DbMessageID const message_id,
108 int const debug_level /* = 0 */) {
109 return isc::db::db_logger_stack.back().logger_.debug(
110 debug_level,
111 isc::db::db_logger_stack.back().translateMessage(message_id));
112}
113
114template <>
115bool
116DB_LOG<fatal>::isEnabled(int const /* debug_level = 0 */) const {
117 return db_logger_stack.back().logger_.isFatalEnabled();
118}
119
120template <>
121bool
122DB_LOG<error>::isEnabled(int const /* debug_level = 0 */) const {
123 return db_logger_stack.back().logger_.isErrorEnabled();
124}
125
126template <>
127bool
128DB_LOG<warn>::isEnabled(int const /* debug_level = 0 */) const {
129 return db_logger_stack.back().logger_.isWarnEnabled();
130}
131
132template <>
133bool
134DB_LOG<info>::isEnabled(int const /* debug_level = 0 */) const {
135 return db_logger_stack.back().logger_.isInfoEnabled();
136}
137
138template <>
139bool
140DB_LOG<debug>::isEnabled(int const debug_level /* = 0 */) const {
141 return db_logger_stack.back().logger_.isDebugEnabled(debug_level);
142}
143
144} // namespace db
145} // namespace isc
isc::Unexpected
A generic exception that is thrown when an unexpected error condition occurs.
Definition exceptions/exceptions.h:153
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::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
db_log.h
We want to reuse the database backend connection and exchange code for other uses,...
db_messages.h
exceptions.h
isc_throw
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
Definition exceptions/exceptions.h:210
isc::db
Definition audit_entry.cc:14
isc::db::DATABASE_MYSQL_INITIAL_CONNECTION_FAIL
const isc::log::MessageID DATABASE_MYSQL_INITIAL_CONNECTION_FAIL
Definition db_messages.h:16
isc::db::DB_DBG_TRACE_DETAIL
const int DB_DBG_TRACE_DETAIL
Database logging levels.
Definition db_log.cc:21
isc::db::DATABASE_PGSQL_FATAL_ERROR
const isc::log::MessageID DATABASE_PGSQL_FATAL_ERROR
Definition db_messages.h:24
isc::db::database_logger
isc::log::Logger database_logger("database")
Common database library logger.
Definition db_log.h:46
isc::db::DATABASE_MYSQL_COMMIT
const isc::log::MessageID DATABASE_MYSQL_COMMIT
Definition db_messages.h:13
isc::db::DbLoggerStack
std::list< DbLogger > DbLoggerStack
Database logger stack.
Definition db_log.h:108
isc::db::DATABASE_MYSQL_INITIALIZE_SCHEMA
const isc::log::MessageID DATABASE_MYSQL_INITIALIZE_SCHEMA
Definition db_messages.h:15
isc::db::DATABASE_PGSQL_INITIAL_CONNECTION_FAIL
const isc::log::MessageID DATABASE_PGSQL_INITIAL_CONNECTION_FAIL
Definition db_messages.h:26
isc::db::DATABASE_PGSQL_INITIALIZE_SCHEMA
const isc::log::MessageID DATABASE_PGSQL_INITIALIZE_SCHEMA
Definition db_messages.h:25
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::DATABASE_PGSQL_COMMIT
const isc::log::MessageID DATABASE_PGSQL_COMMIT
Definition db_messages.h:21
isc::db::db_logger_stack
DbLoggerStack db_logger_stack
Global database logger stack (initialized to database logger).
Definition db_log.cc:54
isc::db::DATABASE_MYSQL_NO_INIT_READONLY
const isc::log::MessageID DATABASE_MYSQL_NO_INIT_READONLY
Definition db_messages.h:18
isc::db::DATABASE_PGSQL_CREATE_SAVEPOINT
const isc::log::MessageID DATABASE_PGSQL_CREATE_SAVEPOINT
Definition db_messages.h:22
isc::db::DATABASE_INVALID_ACCESS
const isc::log::MessageID DATABASE_INVALID_ACCESS
Definition db_messages.h:12
isc::db::DATABASE_PGSQL_ROLLBACK_SAVEPOINT
const isc::log::MessageID DATABASE_PGSQL_ROLLBACK_SAVEPOINT
Definition db_messages.h:30
isc::db::DATABASE_MYSQL_NO_INIT_NO_ADMIN
const isc::log::MessageID DATABASE_MYSQL_NO_INIT_NO_ADMIN
Definition db_messages.h:17
isc::db::DATABASE_MYSQL_ROLLBACK
const isc::log::MessageID DATABASE_MYSQL_ROLLBACK
Definition db_messages.h:19
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::DATABASE_PGSQL_DEALLOC_ERROR
const isc::log::MessageID DATABASE_PGSQL_DEALLOC_ERROR
Definition db_messages.h:23
isc::db::db_message_map
const DbLogger::MessageMap db_message_map
Map of translated messages.
Definition db_log.cc:24
isc::db::DATABASE_PGSQL_NO_INIT_READONLY
const isc::log::MessageID DATABASE_PGSQL_NO_INIT_READONLY
Definition db_messages.h:28
isc::db::db_logger_translator
DbLogger db_logger_translator(database_logger, db_message_map)
Database logger translator.
Definition db_log.h:218
isc::db::DATABASE_PGSQL_START_TRANSACTION
const isc::log::MessageID DATABASE_PGSQL_START_TRANSACTION
Definition db_messages.h:31
isc::db::DATABASE_PGSQL_NO_INIT_NO_ADMIN
const isc::log::MessageID DATABASE_PGSQL_NO_INIT_NO_ADMIN
Definition db_messages.h:27
isc::db::DATABASE_PGSQL_TCP_USER_TIMEOUT_UNSUPPORTED
const isc::log::MessageID DATABASE_PGSQL_TCP_USER_TIMEOUT_UNSUPPORTED
Definition db_messages.h:32
isc::db::DATABASE_PGSQL_ROLLBACK
const isc::log::MessageID DATABASE_PGSQL_ROLLBACK
Definition db_messages.h:29
isc::db::DATABASE_MYSQL_FATAL_ERROR
const isc::log::MessageID DATABASE_MYSQL_FATAL_ERROR
Definition db_messages.h:14
isc::db::DATABASE_MYSQL_START_TRANSACTION
const isc::log::MessageID DATABASE_MYSQL_START_TRANSACTION
Definition db_messages.h:20
isc::log
Definition buffer_appender_impl.cc:17
isc::log::MessageID
const char * MessageID
Definition message_types.h:15
isc::log::DBGLVL_TRACE_DETAIL
const int DBGLVL_TRACE_DETAIL
Trace detailed operations.
Definition log_dbglevels.h:75
isc
Defines the logger used by the top-level component of kea-lfc.
Definition check_exists_add.cc:18