From f5f56e1a1c4d9e9496fcb9d81131066a964ccd23 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sat, 13 Apr 2024 14:15:43 +0200 Subject: Adding upstream version 2.4.1. Signed-off-by: Daniel Baumann --- src/lib/database/db_log.cc | 137 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 137 insertions(+) create mode 100644 src/lib/database/db_log.cc (limited to 'src/lib/database/db_log.cc') 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 + +#include +#include +#include + +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::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::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::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::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::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::isEnabled(int const /* debug_level = 0 */) const { + return db_logger_stack.back().logger_.isFatalEnabled(); +} + +template <> +bool +DB_LOG::isEnabled(int const /* debug_level = 0 */) const { + return db_logger_stack.back().logger_.isErrorEnabled(); +} + +template <> +bool +DB_LOG::isEnabled(int const /* debug_level = 0 */) const { + return db_logger_stack.back().logger_.isWarnEnabled(); +} + +template <> +bool +DB_LOG::isEnabled(int const /* debug_level = 0 */) const { + return db_logger_stack.back().logger_.isInfoEnabled(); +} + +template <> +bool +DB_LOG::isEnabled(int const debug_level /* = 0 */) const { + return db_logger_stack.back().logger_.isDebugEnabled(debug_level); +} + +} // namespace db +} // namespace isc -- cgit v1.2.3