logger.h 5.97 KB
Newer Older
1
/*
2
 *  Copyright (C) 2004-2022 Savoir-faire Linux Inc.
Guillaume Roguez's avatar
Guillaume Roguez committed
3
 *
4
 *  Author: Julien Bonjean <julien.bonjean@savoirfairelinux.com>
Guillaume Roguez's avatar
Guillaume Roguez committed
5
 *  Author: Guillaume Roguez <guillaume.roguez@savoirfairelinux.com>
6
7
8
9
10
11
12
13
14
15
16
17
18
 *
 *  This program 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 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program 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 this program; if not, write to the Free Software
19
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA.
20
21
 */

Guillaume Roguez's avatar
Guillaume Roguez committed
22
#pragma once
Julien Bonjean's avatar
Julien Bonjean committed
23

Adrien Béraud's avatar
Adrien Béraud committed
24
25
#include "jami/def.h"

Guillaume Roguez's avatar
Guillaume Roguez committed
26
//#define __STDC_FORMAT_MACROS 1
27
28
#include <fmt/core.h>
#include <fmt/format.h>
29
#include <fmt/chrono.h>
30

Guillaume Roguez's avatar
Guillaume Roguez committed
31
32
#include <cinttypes> // for PRIx64
#include <cstdarg>
Vittorio Giovara's avatar
Vittorio Giovara committed
33

Olivier Dion's avatar
Olivier Dion committed
34
#include <atomic>
Guillaume Roguez's avatar
Guillaume Roguez committed
35
36
37
#include <sstream>
#include <string>
#include "string_utils.h" // to_string
38

39
#ifdef __ANDROID__
40

41
42
43
44
45
46
#include <android/log.h>
#define LOG_ERR     ANDROID_LOG_ERROR
#define LOG_WARNING ANDROID_LOG_WARN
#define LOG_INFO    ANDROID_LOG_INFO
#define LOG_DEBUG   ANDROID_LOG_DEBUG

47
#elif defined(_WIN32)
Edric Milaret's avatar
Edric Milaret committed
48
49
50
51
52
53
54

#include "winsyslog.h"
#define LOG_ERR     EVENTLOG_ERROR_TYPE
#define LOG_WARNING EVENTLOG_WARNING_TYPE
#define LOG_INFO    EVENTLOG_INFORMATION_TYPE
#define LOG_DEBUG   EVENTLOG_SUCCESS

55
#else
56

Guillaume Roguez's avatar
Guillaume Roguez committed
57
#include <syslog.h> // Defines LOG_XXXX
58

Guillaume Roguez's avatar
Guillaume Roguez committed
59
#endif /* __ANDROID__ / _WIN32 */
60

61
#if defined(_WIN32) && !defined(_MSC_VER)
Guillaume Roguez's avatar
Guillaume Roguez committed
62
63
64
65
66
#define PRINTF_ATTRIBUTE(a, b) __attribute__((format(gnu_printf, a, b)))
#elif defined(__GNUC__)
#define PRINTF_ATTRIBUTE(a, b) __attribute__((format(printf, a, b)))
#else
#define PRINTF_ATTRIBUTE(a, b)
Vittorio Giovara's avatar
Vittorio Giovara committed
67
#endif
Guillaume Roguez's avatar
Guillaume Roguez committed
68

Adrien Béraud's avatar
Adrien Béraud committed
69
namespace jami {
Guillaume Roguez's avatar
Guillaume Roguez committed
70

Adrien Béraud's avatar
Adrien Béraud committed
71
72
73
74
75
/**
 * Thread-safe function to print the stringified contents of errno
 */
void strErr();

Guillaume Roguez's avatar
Guillaume Roguez committed
76
77
78
79
80
81
///
/// Level-driven logging class that support printf and C++ stream logging fashions.
///
class Logger
{
public:
Olivier Dion's avatar
Olivier Dion committed
82
83
84
85

    class Handler;
    struct Msg;

Guillaume Roguez's avatar
Guillaume Roguez committed
86
87
88
89
    Logger(int level, const char* file, int line, bool linefeed)
        : level_ {level}
        , file_ {file}
        , line_ {line}
Sébastien Blin's avatar
Sébastien Blin committed
90
91
        , linefeed_ {linefeed}
    {}
Guillaume Roguez's avatar
Guillaume Roguez committed
92

93
    Logger() = delete;
94
    Logger(const Logger&) = delete;
95
    Logger(Logger&&) = default;
Guillaume Roguez's avatar
Guillaume Roguez committed
96

Sébastien Blin's avatar
Sébastien Blin committed
97
    ~Logger() { log(level_, file_, line_, linefeed_, "%s", os_.str().c_str()); }
Guillaume Roguez's avatar
Guillaume Roguez committed
98

Sébastien Blin's avatar
Sébastien Blin committed
99
100
101
    template<typename T>
    inline Logger& operator<<(const T& value)
    {
Guillaume Roguez's avatar
Guillaume Roguez committed
102
103
104
105
        os_ << value;
        return *this;
    }

Olivier Dion's avatar
Olivier Dion committed
106
    LIBJAMI_PUBLIC
107
108
    static void write(int level, const char* file, int line, std::string&& message);

Guillaume Roguez's avatar
Guillaume Roguez committed
109
110
111
    ///
    /// Printf fashion logging.
    ///
Adrien Béraud's avatar
Adrien Béraud committed
112
    /// Example: JAMI_DBG("%s", "Hello, World!")
Guillaume Roguez's avatar
Guillaume Roguez committed
113
    ///
Olivier Dion's avatar
Olivier Dion committed
114
    LIBJAMI_PUBLIC
Sébastien Blin's avatar
Sébastien Blin committed
115
116
    static void log(int level, const char* file, int line, bool linefeed, const char* const fmt, ...)
        PRINTF_ATTRIBUTE(5, 6);
Guillaume Roguez's avatar
Guillaume Roguez committed
117
118
119
120

    ///
    /// Printf fashion logging (using va_list parameters)
    ///
Olivier Dion's avatar
Olivier Dion committed
121
    LIBJAMI_PUBLIC
Olivier Dion's avatar
Olivier Dion committed
122
123
124
125
126
127
128
129
    static void vlog(int level, const char* file, int line, bool linefeed, const char* fmt, va_list);

    static void setConsoleLog(bool enable);
    static void setSysLog(bool enable);
    static void setMonitorLog(bool enable);
    static void setFileLog(const std::string& path);

    static void setDebugMode(bool enable);
130
    static bool debugEnabled();
Olivier Dion's avatar
Olivier Dion committed
131
132

    static void fini();
Guillaume Roguez's avatar
Guillaume Roguez committed
133
134
135
136

    ///
    /// Stream fashion logging.
    ///
Adrien Béraud's avatar
Adrien Béraud committed
137
    /// Example: JAMI_DBG() << "Hello, World!"
Guillaume Roguez's avatar
Guillaume Roguez committed
138
    ///
Sébastien Blin's avatar
Sébastien Blin committed
139
140
    static Logger log(int level, const char* file, int line, bool linefeed)
    {
Guillaume Roguez's avatar
Guillaume Roguez committed
141
142
143
144
        return {level, file, line, linefeed};
    }

private:
Olivier Dion's avatar
Olivier Dion committed
145

Sébastien Blin's avatar
Sébastien Blin committed
146
147
148
149
150
151
    int level_;              ///< LOG_XXXX values
    const char* const file_; ///< contextual filename (printed as header)
    const int line_;         ///< contextual line number (printed as header)
    bool linefeed_ {
        true}; ///< true if a '\n' (or any platform equivalent) has to be put at line end in consoleMode
    std::ostringstream os_; ///< string stream used with C++ stream style (stream operator<<)
Guillaume Roguez's avatar
Guillaume Roguez committed
152
153
};

154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
namespace log {

template<typename S, typename... Args>
void dbg(const char* file, int line, S&& format, Args&&... args) {
    Logger::write(LOG_DEBUG, file, line, fmt::format(std::forward<S>(format), std::forward<Args>(args)...));
}

template<typename S, typename... Args>
void warn(const char* file, int line, S&& format, Args&&... args) {
    Logger::write(LOG_WARNING, file, line, fmt::format(std::forward<S>(format), std::forward<Args>(args)...));
}

template<typename S, typename... Args>
void error(const char* file, int line, S&& format, Args&&... args) {
    Logger::write(LOG_ERR, file, line, fmt::format(std::forward<S>(format), std::forward<Args>(args)...));
}

}

Guillaume Roguez's avatar
Guillaume Roguez committed
173
// We need to use macros for contextual information
Sébastien Blin's avatar
Sébastien Blin committed
174
175
176
177
178
179
180
181
182
#define JAMI_INFO(...) ::jami::Logger::log(LOG_INFO, __FILE__, __LINE__, true, ##__VA_ARGS__)
#define JAMI_DBG(...)  ::jami::Logger::log(LOG_DEBUG, __FILE__, __LINE__, true, ##__VA_ARGS__)
#define JAMI_WARN(...) ::jami::Logger::log(LOG_WARNING, __FILE__, __LINE__, true, ##__VA_ARGS__)
#define JAMI_ERR(...)  ::jami::Logger::log(LOG_ERR, __FILE__, __LINE__, true, ##__VA_ARGS__)

#define JAMI_XINFO(...) ::jami::Logger::log(LOG_INFO, __FILE__, __LINE__, false, ##__VA_ARGS__)
#define JAMI_XDBG(...)  ::jami::Logger::log(LOG_DEBUG, __FILE__, __LINE__, false, ##__VA_ARGS__)
#define JAMI_XWARN(...) ::jami::Logger::log(LOG_WARNING, __FILE__, __LINE__, false, ##__VA_ARGS__)
#define JAMI_XERR(...)  ::jami::Logger::log(LOG_ERR, __FILE__, __LINE__, false, ##__VA_ARGS__)
Guillaume Roguez's avatar
Guillaume Roguez committed
183

184
185
186
#define JAMI_DEBUG(formatstr, ...) if(::jami::Logger::debugEnabled()) { ::jami::log::dbg(__FILE__, __LINE__, FMT_STRING(formatstr), ##__VA_ARGS__); }
#define JAMI_WARNING(formatstr, ...) ::jami::log::warn(__FILE__, __LINE__, FMT_STRING(formatstr), ##__VA_ARGS__)
#define JAMI_ERROR(formatstr, ...) ::jami::log::error(__FILE__, __LINE__, FMT_STRING(formatstr), ##__VA_ARGS__)
187

Adrien Béraud's avatar
Adrien Béraud committed
188
} // namespace jami