summaryrefslogtreecommitdiffstats
path: root/wsrep-lib/include/wsrep/logger.hpp
blob: a15873c26e046a3cee1946bc6bc271e451a87318 (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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/*
 * Copyright (C) 2018-2019 Codership Oy <info@codership.com>
 *
 * This file is part of wsrep-lib.
 *
 * Wsrep-lib is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * Wsrep-lib is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with wsrep-lib.  If not, see <https://www.gnu.org/licenses/>.
 */

#ifndef WSREP_LOGGER_HPP
#define WSREP_LOGGER_HPP

#include "mutex.hpp"
#include "lock.hpp"
#include "atomic.hpp"

#include <iosfwd>
#include <sstream>

#define WSREP_LOG_DEBUG(debug_level_fn, debug_level, msg)               \
    do {                                                                \
        if (debug_level_fn >= debug_level) wsrep::log_debug() << msg;   \
    } while (0)

namespace wsrep
{
    class log
    {
    public:
        enum level
        {
            debug,
            info,
            warning,
            error,
            unknown
        };

        enum debug_level
        {
            debug_level_server_state = 1,
            debug_level_transaction,
            debug_level_streaming,
            debug_level_client_state
        };

        /**
         * Signature for user defined logger callback function.
         *
         * @param pfx optional internally defined prefix for the message
         * @param msg message to log
         */
        typedef void (*logger_fn_type)(level l,
                                       const char* pfx, const char* msg);

        static const char* to_c_string(enum level level)
        {
            switch (level)
            {
            case debug:   return "debug";
            case info:    return "info";
            case warning: return "warning";
            case error:   return "error";
            case unknown: break;
            };
            return "unknown";
        }

        log(enum wsrep::log::level level, const char* prefix = "L:")
            : level_(level)
            , prefix_(prefix)
            , oss_()
        { }

        ~log()
        {
            if (logger_fn_)
            {
                logger_fn_(level_, prefix_, oss_.str().c_str());
            }
            else
            {
                wsrep::unique_lock<wsrep::mutex> lock(mutex_);
                os_ << prefix_ << oss_.str() << std::endl;
            }
        }

        template <typename T>
        std::ostream& operator<<(const T& val)
        {
            return (oss_ << val);
        }

        /**
         * Set user defined logger callback function.
         */
        static void logger_fn(logger_fn_type);

        /**
         * Set debug log level from client
         */
        static void debug_log_level(int debug_level);

        /**
         * Get current debug log level
         */
        static int debug_log_level();

    private:
        log(const log&);
        log& operator=(const log&);
        enum level level_;
        const char* prefix_;
        std::ostringstream oss_;
        static wsrep::mutex& mutex_;
        static std::ostream& os_;
        static logger_fn_type logger_fn_;
        static std::atomic_int debug_log_level_;
    };

    class log_error : public log
    {
    public:
        log_error()
            : log(error) { }
    };

    class log_warning : public log
    {
    public:
        log_warning()
            : log(warning) { }
    };

    class log_info : public log
    {
    public:
        log_info()
            : log(info) { }
    };

    class log_debug : public log
    {
    public:
        log_debug()
            : log(debug) { }
    };
}

#endif // WSREP_LOGGER_HPP