Kea  2.1.7-git
db_log.h
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 
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 
47 
53 
61 
66 };
68 
71 class DbLogger {
72 public:
74  typedef std::map<DbMessageID, isc::log::MessageID> MessageMap;
75 
80  DbLogger(isc::log::Logger& logger, const MessageMap& map)
81  : logger_(logger), map_(map) {
82  }
83 
89  const isc::log::MessageID& translateMessage(const DbMessageID& id) const;
90 
93 
95  const MessageMap& map_;
96 };
97 
99 typedef std::list<DbLogger> DbLoggerStack;
100 
102 extern DbLoggerStack db_logger_stack;
103 
105 extern std::mutex db_logger_mutex;
106 
110 void checkDbLoggerStack();
111 
119 };
120 
122 template <log_type_t log_type>
123 struct DB_LOG {
126  DB_LOG(DbMessageID const message_id, int const debug_level = 0) {
127  std::lock_guard<std::mutex> lock(isc::db::db_logger_mutex);
129  if (isEnabled()) {
130  formatter_ = formatter(message_id, debug_level);
131  }
132  }
133 
140  template <typename T, typename... Args>
141  DB_LOG& arg(T first, Args... args) {
142  formatter_.arg(first);
143  return arg(args...);
144  }
145 
151  DB_LOG& arg() {
152  return *this;
153  }
154 
155 private:
163  formatter(DbMessageID const message_id, int const debug_level = 0);
164 
170  bool isEnabled(int const debug_level = 0) const;
171 
173  isc::log::Logger::Formatter formatter_;
174 };
175 
178 struct DB_LOG_FATAL : DB_LOG<fatal> {
179  DB_LOG_FATAL(DbMessageID const message_id) : DB_LOG(message_id) {
180  }
181 };
182 
183 struct DB_LOG_ERROR : DB_LOG<error> {
184  DB_LOG_ERROR(DbMessageID const message_id) : DB_LOG(message_id) {
185  }
186 };
187 
188 struct DB_LOG_WARN : DB_LOG<warn> {
189  DB_LOG_WARN(DbMessageID const message_id) : DB_LOG(message_id) {
190  }
191 };
192 
193 struct DB_LOG_INFO : DB_LOG<info> {
194  DB_LOG_INFO(DbMessageID const message_id) : DB_LOG(message_id) {
195  }
196 };
197 
198 struct DB_LOG_DEBUG : DB_LOG<debug> {
199  DB_LOG_DEBUG(int const debug_level, DbMessageID const message_id)
200  : DB_LOG(message_id, debug_level) {
201  }
202 };
204 
207 
210 
211 } // namespace db
212 } // namespace isc
213 
214 #endif // DB_LOG_H
DB_LOG_DEBUG(int const debug_level, DbMessageID const message_id)
Definition: db_log.h:199
Logger Class.
Definition: log/logger.h:141
Database logger class.
Definition: db_log.h:71
const isc::log::MessageID & translateMessage(const DbMessageID &id) const
Translate message.
Definition: db_log.cc:50
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:126
DbMessageID
Database messages.
Definition: db_log.h:51
DB_LOG & arg()
The last invocation of the arg() which is without parameters.
Definition: db_log.h:151
isc::log::Logger & logger_
The logger.
Definition: db_log.h:92
const DbLogger::MessageMap db_message_map
Map of translated messages.
Definition: db_log.cc:24
DB_LOG_FATAL(DbMessageID const message_id)
Definition: db_log.h:179
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
DbLogger(isc::log::Logger &logger, const MessageMap &map)
Constructor.
Definition: db_log.h:80
DB_LOG_WARN(DbMessageID const message_id)
Definition: db_log.h:189
log_type_t
log type enumerations for use in DB_LOG specializations
Definition: db_log.h:113
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
DB_LOG_ERROR(DbMessageID const message_id)
Definition: db_log.h:184
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.
isc::log::Logger logger("asiodns")
Use the ASIO logger.
The log message formatter.
DB_LOG & arg(T first, Args... args)
Pass parameters to replace logger placeholders.
Definition: db_log.h:141
DB_LOG_INFO(DbMessageID const message_id)
Definition: db_log.h:194
all DB_LOG specializations
Definition: db_log.h:178
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 MessageMap & map_
The translation map.
Definition: db_log.h:95
const char * MessageID
Definition: message_types.h:15