summaryrefslogtreecommitdiffstats
path: root/src/lib/database/db_log.cc
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-13 12:15:43 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-13 12:15:43 +0000
commitf5f56e1a1c4d9e9496fcb9d81131066a964ccd23 (patch)
tree49e44c6f87febed37efb953ab5485aa49f6481a7 /src/lib/database/db_log.cc
parentInitial commit. (diff)
downloadisc-kea-f5f56e1a1c4d9e9496fcb9d81131066a964ccd23.tar.xz
isc-kea-f5f56e1a1c4d9e9496fcb9d81131066a964ccd23.zip
Adding upstream version 2.4.1.upstream/2.4.1upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'src/lib/database/db_log.cc')
-rw-r--r--src/lib/database/db_log.cc137
1 files changed, 137 insertions, 0 deletions
diff --git a/src/lib/database/db_log.cc b/src/lib/database/db_log.cc
new file mode 100644
index 0000000..395bd57
--- /dev/null
+++ b/src/lib/database/db_log.cc
@@ -0,0 +1,137 @@
+// Copyright (C) 2018-2023 Internet Systems Consortium, Inc. ("ISC")
+//
+// This Source Code Form is subject to the terms of the Mozilla Public
+// License, v. 2.0. If a copy of the MPL was not distributed with this
+// file, You can obtain one at http://mozilla.org/MPL/2.0/.
+
+/// Defines the logger used by the NSAS
+
+#include <config.h>
+
+#include <exceptions/exceptions.h>
+#include <database/db_log.h>
+#include <database/db_messages.h>
+
+using namespace isc::log;
+
+namespace isc {
+namespace db {
+
+/// @brief Database logging levels.
+const int DB_DBG_TRACE_DETAIL = isc::log::DBGLVL_TRACE_DETAIL;
+
+/// @brief Map of translated messages.
+const DbLogger::MessageMap db_message_map = {
+ { DB_INVALID_ACCESS, DATABASE_INVALID_ACCESS },
+
+ { PGSQL_DEALLOC_ERROR, DATABASE_PGSQL_DEALLOC_ERROR },
+ { PGSQL_FATAL_ERROR, DATABASE_PGSQL_FATAL_ERROR },
+ { PGSQL_START_TRANSACTION, DATABASE_PGSQL_START_TRANSACTION },
+ { PGSQL_COMMIT, DATABASE_PGSQL_COMMIT },
+ { PGSQL_ROLLBACK, DATABASE_PGSQL_ROLLBACK },
+ { PGSQL_CREATE_SAVEPOINT, DATABASE_PGSQL_CREATE_SAVEPOINT },
+ { PGSQL_ROLLBACK_SAVEPOINT, DATABASE_PGSQL_ROLLBACK_SAVEPOINT },
+ { PGSQL_TCP_USER_TIMEOUT_UNSUPPORTED, DATABASE_PGSQL_TCP_USER_TIMEOUT_UNSUPPORTED },
+
+ { MYSQL_FATAL_ERROR, DATABASE_MYSQL_FATAL_ERROR },
+ { MYSQL_START_TRANSACTION, DATABASE_MYSQL_START_TRANSACTION },
+ { MYSQL_COMMIT, DATABASE_MYSQL_COMMIT },
+ { MYSQL_ROLLBACK, DATABASE_MYSQL_ROLLBACK },
+};
+
+isc::log::Logger database_logger("database");
+
+DbLogger db_logger_translator(database_logger, db_message_map);
+
+DbLoggerStack db_logger_stack = { db_logger_translator };
+
+std::mutex db_logger_mutex;
+
+const MessageID&
+DbLogger::translateMessage(const DbMessageID& id) const {
+ try {
+ return (map_.at(id));
+ } catch (const std::out_of_range&) {
+ isc_throw(isc::Unexpected, "can't map message: " << id);
+ }
+}
+
+void checkDbLoggerStack() {
+ if (db_logger_stack.empty()) {
+ isc_throw(isc::Unexpected, "database logger stack is empty");
+ }
+}
+
+template <>
+isc::log::Logger::Formatter
+DB_LOG<fatal>::formatter(DbMessageID const message_id,
+ int const /* debug_level = 0 */) {
+ return isc::db::db_logger_stack.back().logger_.fatal(
+ isc::db::db_logger_stack.back().translateMessage(message_id));
+}
+
+template <>
+isc::log::Logger::Formatter
+DB_LOG<error>::formatter(DbMessageID const message_id,
+ int const /* debug_level = 0 */) {
+ return isc::db::db_logger_stack.back().logger_.error(
+ isc::db::db_logger_stack.back().translateMessage(message_id));
+}
+
+template <>
+isc::log::Logger::Formatter
+DB_LOG<warn>::formatter(DbMessageID const message_id,
+ int const /* debug_level = 0 */) {
+ return isc::db::db_logger_stack.back().logger_.warn(
+ isc::db::db_logger_stack.back().translateMessage(message_id));
+}
+
+template <>
+isc::log::Logger::Formatter
+DB_LOG<info>::formatter(DbMessageID const message_id,
+ int const /* debug_level = 0 */) {
+ return isc::db::db_logger_stack.back().logger_.info(
+ isc::db::db_logger_stack.back().translateMessage(message_id));
+}
+
+template <>
+isc::log::Logger::Formatter
+DB_LOG<debug>::formatter(DbMessageID const message_id,
+ int const debug_level /* = 0 */) {
+ return isc::db::db_logger_stack.back().logger_.debug(
+ debug_level,
+ isc::db::db_logger_stack.back().translateMessage(message_id));
+}
+
+template <>
+bool
+DB_LOG<fatal>::isEnabled(int const /* debug_level = 0 */) const {
+ return db_logger_stack.back().logger_.isFatalEnabled();
+}
+
+template <>
+bool
+DB_LOG<error>::isEnabled(int const /* debug_level = 0 */) const {
+ return db_logger_stack.back().logger_.isErrorEnabled();
+}
+
+template <>
+bool
+DB_LOG<warn>::isEnabled(int const /* debug_level = 0 */) const {
+ return db_logger_stack.back().logger_.isWarnEnabled();
+}
+
+template <>
+bool
+DB_LOG<info>::isEnabled(int const /* debug_level = 0 */) const {
+ return db_logger_stack.back().logger_.isInfoEnabled();
+}
+
+template <>
+bool
+DB_LOG<debug>::isEnabled(int const debug_level /* = 0 */) const {
+ return db_logger_stack.back().logger_.isDebugEnabled(debug_level);
+}
+
+} // namespace db
+} // namespace isc