summaryrefslogtreecommitdiffstats
path: root/src/lib/database/db_log.cc
blob: 8a9d1ab18b6f5abc05879a2bae3acf650349e28a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
// Copyright (C) 2018-2022 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 },

    { 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