| Kea 3.0.2-git

db_log.cc
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
8
9#include <config.h>
10
11#include <exceptions/exceptions.h>
12#include <database/db_log.h>
13#include <database/db_messages.h>
14
15 using namespace isc::log;
16
17 namespace isc {
18 namespace 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_INITIALIZE_SCHEMA, DATABASE_PGSQL_INITIALIZE_SCHEMA },
28 { PGSQL_DEALLOC_ERROR, DATABASE_PGSQL_DEALLOC_ERROR },
29 { PGSQL_FATAL_ERROR, DATABASE_PGSQL_FATAL_ERROR },
30 { PGSQL_START_TRANSACTION, DATABASE_PGSQL_START_TRANSACTION },
31 { PGSQL_COMMIT, DATABASE_PGSQL_COMMIT },
32 { PGSQL_ROLLBACK, DATABASE_PGSQL_ROLLBACK },
33 { PGSQL_CREATE_SAVEPOINT, DATABASE_PGSQL_CREATE_SAVEPOINT },
34 { PGSQL_ROLLBACK_SAVEPOINT, DATABASE_PGSQL_ROLLBACK_SAVEPOINT },
35 { PGSQL_TCP_USER_TIMEOUT_UNSUPPORTED, DATABASE_PGSQL_TCP_USER_TIMEOUT_UNSUPPORTED },
36
37 { MYSQL_INITIALIZE_SCHEMA, DATABASE_MYSQL_INITIALIZE_SCHEMA },
38 { MYSQL_FATAL_ERROR, DATABASE_MYSQL_FATAL_ERROR },
39 { MYSQL_START_TRANSACTION, DATABASE_MYSQL_START_TRANSACTION },
40 { MYSQL_COMMIT, DATABASE_MYSQL_COMMIT },
41 { MYSQL_ROLLBACK, DATABASE_MYSQL_ROLLBACK },
42};
43
44 isc::log::Logger database_logger("database");
45
46DbLogger db_logger_translator(database_logger, db_message_map);
47
48 DbLoggerStack db_logger_stack = { db_logger_translator };
49
50std::mutex db_logger_mutex;
51
52 const MessageID&
53 DbLogger::translateMessage(const DbMessageID& id) const {
54 try {
55 return (map_.at(id));
56 } catch (const std::out_of_range&) {
57 isc_throw(isc::Unexpected, "can't map message: " << id);
58 }
59}
60
61 void checkDbLoggerStack() {
62 if (db_logger_stack.empty()) {
63 isc_throw(isc::Unexpected, "database logger stack is empty");
64 }
65}
66
67 template <>
68 isc::log::Logger::Formatter
69DB_LOG<fatal>::formatter(DbMessageID const message_id,
70 int const /* debug_level = 0 */) {
71 return isc::db::db_logger_stack.back().logger_.fatal(
72 isc::db::db_logger_stack.back().translateMessage(message_id));
73}
74
75 template <>
76 isc::log::Logger::Formatter
77DB_LOG<error>::formatter(DbMessageID const message_id,
78 int const /* debug_level = 0 */) {
79 return isc::db::db_logger_stack.back().logger_.error(
80 isc::db::db_logger_stack.back().translateMessage(message_id));
81}
82
83 template <>
84 isc::log::Logger::Formatter
85DB_LOG<warn>::formatter(DbMessageID const message_id,
86 int const /* debug_level = 0 */) {
87 return isc::db::db_logger_stack.back().logger_.warn(
88 isc::db::db_logger_stack.back().translateMessage(message_id));
89}
90
91 template <>
92 isc::log::Logger::Formatter
93DB_LOG<info>::formatter(DbMessageID const message_id,
94 int const /* debug_level = 0 */) {
95 return isc::db::db_logger_stack.back().logger_.info(
96 isc::db::db_logger_stack.back().translateMessage(message_id));
97}
98
99 template <>
100 isc::log::Logger::Formatter
101DB_LOG<debug>::formatter(DbMessageID const message_id,
102 int const debug_level /* = 0 */) {
103 return isc::db::db_logger_stack.back().logger_.debug(
104 debug_level,
105 isc::db::db_logger_stack.back().translateMessage(message_id));
106}
107
108 template <>
109 bool
110DB_LOG<fatal>::isEnabled(int const /* debug_level = 0 */) const {
111 return db_logger_stack.back().logger_.isFatalEnabled();
112}
113
114 template <>
115 bool
116DB_LOG<error>::isEnabled(int const /* debug_level = 0 */) const {
117 return db_logger_stack.back().logger_.isErrorEnabled();
118}
119
120 template <>
121 bool
122DB_LOG<warn>::isEnabled(int const /* debug_level = 0 */) const {
123 return db_logger_stack.back().logger_.isWarnEnabled();
124}
125
126 template <>
127 bool
128DB_LOG<info>::isEnabled(int const /* debug_level = 0 */) const {
129 return db_logger_stack.back().logger_.isInfoEnabled();
130}
131
132 template <>
133 bool
134DB_LOG<debug>::isEnabled(int const debug_level /* = 0 */) const {
135 return db_logger_stack.back().logger_.isDebugEnabled(debug_level);
136}
137
138} // namespace db
139} // 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: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::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::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:21
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:102
isc::db::DATABASE_MYSQL_INITIALIZE_SCHEMA
const isc::log::MessageID DATABASE_MYSQL_INITIALIZE_SCHEMA
Definition db_messages.h:15
isc::db::DATABASE_PGSQL_INITIALIZE_SCHEMA
const isc::log::MessageID DATABASE_PGSQL_INITIALIZE_SCHEMA
Definition db_messages.h:22
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::DATABASE_PGSQL_COMMIT
const isc::log::MessageID DATABASE_PGSQL_COMMIT
Definition db_messages.h:18
isc::db::db_logger_stack
DbLoggerStack db_logger_stack
Global database logger stack (initialized to database logger).
Definition db_log.cc:48
isc::db::DATABASE_PGSQL_CREATE_SAVEPOINT
const isc::log::MessageID DATABASE_PGSQL_CREATE_SAVEPOINT
Definition db_messages.h:19
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:24
isc::db::DATABASE_MYSQL_ROLLBACK
const isc::log::MessageID DATABASE_MYSQL_ROLLBACK
Definition db_messages.h:16
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::DATABASE_PGSQL_DEALLOC_ERROR
const isc::log::MessageID DATABASE_PGSQL_DEALLOC_ERROR
Definition db_messages.h:20
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::db::DATABASE_PGSQL_START_TRANSACTION
const isc::log::MessageID DATABASE_PGSQL_START_TRANSACTION
Definition db_messages.h:25
isc::db::DATABASE_PGSQL_TCP_USER_TIMEOUT_UNSUPPORTED
const isc::log::MessageID DATABASE_PGSQL_TCP_USER_TIMEOUT_UNSUPPORTED
Definition db_messages.h:26
isc::db::DATABASE_PGSQL_ROLLBACK
const isc::log::MessageID DATABASE_PGSQL_ROLLBACK
Definition db_messages.h:23
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:17
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 agent_parser.cc:148