Kea  2.1.7-git
db_log.cc
Go to the documentation of this file.
1 // Copyright (C) 2018-2022 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 
22 
26 
34 
39 };
40 
42 
43 DbLogger db_logger_translator(database_logger, db_message_map);
44 
45 DbLoggerStack db_logger_stack = { db_logger_translator };
46 
47 std::mutex db_logger_mutex;
48 
49 const MessageID&
50 DbLogger::translateMessage(const DbMessageID& id) const {
51  try {
52  return (map_.at(id));
53  } catch (const std::out_of_range&) {
54  isc_throw(isc::Unexpected, "can't map message: " << id);
55  }
56 }
57 
59  if (db_logger_stack.empty()) {
60  isc_throw(isc::Unexpected, "database logger stack is empty");
61  }
62 }
63 
64 template <>
66 DB_LOG<fatal>::formatter(DbMessageID const message_id,
67  int const /* debug_level = 0 */) {
68  return isc::db::db_logger_stack.back().logger_.fatal(
69  isc::db::db_logger_stack.back().translateMessage(message_id));
70 }
71 
72 template <>
74 DB_LOG<error>::formatter(DbMessageID const message_id,
75  int const /* debug_level = 0 */) {
76  return isc::db::db_logger_stack.back().logger_.error(
77  isc::db::db_logger_stack.back().translateMessage(message_id));
78 }
79 
80 template <>
82 DB_LOG<warn>::formatter(DbMessageID const message_id,
83  int const /* debug_level = 0 */) {
84  return isc::db::db_logger_stack.back().logger_.warn(
85  isc::db::db_logger_stack.back().translateMessage(message_id));
86 }
87 
88 template <>
90 DB_LOG<info>::formatter(DbMessageID const message_id,
91  int const /* debug_level = 0 */) {
92  return isc::db::db_logger_stack.back().logger_.info(
93  isc::db::db_logger_stack.back().translateMessage(message_id));
94 }
95 
96 template <>
98 DB_LOG<debug>::formatter(DbMessageID const message_id,
99  int const debug_level /* = 0 */) {
100  return isc::db::db_logger_stack.back().logger_.debug(
101  debug_level,
102  isc::db::db_logger_stack.back().translateMessage(message_id));
103 }
104 
105 template <>
106 bool
107 DB_LOG<fatal>::isEnabled(int const /* debug_level = 0 */) const {
108  return db_logger_stack.back().logger_.isFatalEnabled();
109 }
110 
111 template <>
112 bool
113 DB_LOG<error>::isEnabled(int const /* debug_level = 0 */) const {
114  return db_logger_stack.back().logger_.isErrorEnabled();
115 }
116 
117 template <>
118 bool
119 DB_LOG<warn>::isEnabled(int const /* debug_level = 0 */) const {
120  return db_logger_stack.back().logger_.isWarnEnabled();
121 }
122 
123 template <>
124 bool
125 DB_LOG<info>::isEnabled(int const /* debug_level = 0 */) const {
126  return db_logger_stack.back().logger_.isInfoEnabled();
127 }
128 
129 template <>
130 bool
131 DB_LOG<debug>::isEnabled(int const debug_level /* = 0 */) const {
132  return db_logger_stack.back().logger_.isDebugEnabled(debug_level);
133 }
134 
135 } // namespace db
136 } // namespace isc
We want to reuse the database backend connection and exchange code for other uses, in particular for hook libraries.
Logger Class.
Definition: log/logger.h:141
const isc::log::MessageID DATABASE_PGSQL_ROLLBACK_SAVEPOINT
Definition: db_messages.h:21
const isc::log::MessageID DATABASE_PGSQL_CREATE_SAVEPOINT
Definition: db_messages.h:17
Database logger class.
Definition: db_log.h:71
DbMessageID
Database messages.
Definition: db_log.h:51
const isc::log::MessageID DATABASE_PGSQL_COMMIT
Definition: db_messages.h:16
const isc::log::MessageID DATABASE_PGSQL_DEALLOC_ERROR
Definition: db_messages.h:18
const DbLogger::MessageMap db_message_map
Map of translated messages.
Definition: db_log.cc:24
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
std::map< DbMessageID, isc::log::MessageID > MessageMap
Translation map type.
Definition: db_log.h:74
std::mutex db_logger_mutex
Global mutex to protect logger stack.
Definition: db_log.cc:47
A generic exception that is thrown when an unexpected error condition occurs.
std::list< DbLogger > DbLoggerStack
Database logger stack.
Definition: db_log.h:99
DbLoggerStack db_logger_stack
Global database logger stack (initialized to database logger)
Definition: db_log.cc:45
DB_LOG_* logic.
Definition: db_log.h:123
const isc::log::MessageID DATABASE_PGSQL_FATAL_ERROR
Definition: db_messages.h:19
const isc::log::MessageID DATABASE_MYSQL_ROLLBACK
Definition: db_messages.h:14
const int DB_DBG_TRACE_DETAIL
Database logging levels.
Definition: db_log.cc:21
Defines the logger used by the top-level component of kea-lfc.
const isc::log::MessageID DATABASE_PGSQL_ROLLBACK
Definition: db_messages.h:20
The log message formatter.
const isc::log::MessageID DATABASE_MYSQL_FATAL_ERROR
Definition: db_messages.h:13
const isc::log::MessageID DATABASE_MYSQL_COMMIT
Definition: db_messages.h:12
void checkDbLoggerStack()
Check database logger stack.
Definition: db_log.cc:58
DbLogger db_logger_translator(database_logger, db_message_map)
Database logger translator.
Definition: db_log.h:209
isc::log::Logger database_logger("database")
Common database library logger.
Definition: db_log.h:46
const int DBGLVL_TRACE_DETAIL
Trace detailed operations.
Definition: log_dbglevels.h:75
const isc::log::MessageID DATABASE_MYSQL_START_TRANSACTION
Definition: db_messages.h:15
const isc::log::MessageID DATABASE_PGSQL_START_TRANSACTION
Definition: db_messages.h:22
const isc::log::MessageID DATABASE_INVALID_ACCESS
Definition: db_messages.h:11
const char * MessageID
Definition: message_types.h:15