logger.h 4.72 KB
Newer Older
1
/*
2
 *  Copyright (C) 2004-2019 Savoir-faire Linux Inc.
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
23

24 25 26
//#define __STDC_FORMAT_MACROS 1
#include <cinttypes> // for PRIx64
#include <cstdarg>
Vittorio Giovara's avatar
Vittorio Giovara committed
27

28 29 30
#include <sstream>
#include <string>
#include "string_utils.h" // to_string
31

32 33
#ifdef __cplusplus
extern "C" {
34
#endif
Vittorio Giovara's avatar
Vittorio Giovara committed
35 36 37 38 39 40 41 42 43 44

/**
 * Allow writing on the console
 */
void setConsoleLog(int c);

/**
 * When debug mode is not set, logging will not print anything
 */
void setDebugMode(int d);
45

Vittorio Giovara's avatar
Vittorio Giovara committed
46 47 48 49
/**
 * Return the current mode
 */
int getDebugMode(void);
50

51
/**
Vittorio Giovara's avatar
Vittorio Giovara committed
52 53 54
 * Thread-safe function to print the stringified contents of errno
 */
void strErr();
55

56 57
#ifdef __cplusplus
}
atraczyk's avatar
atraczyk committed
58
#endif
59

60
#ifdef __ANDROID__
61

62 63 64 65 66 67
#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

68
#elif defined(_WIN32)
Edric Milaret's avatar
Edric Milaret committed
69 70 71 72 73 74 75

#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

76
#else
77

78
#include <syslog.h> // Defines LOG_XXXX
79

80
#endif /* __ANDROID__ / _WIN32 */
81

82
#if defined(_WIN32) && !defined(_MSC_VER)
83 84 85 86 87
#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
88
#endif
89

Adrien Béraud's avatar
Adrien Béraud committed
90
namespace jami {
91 92 93 94 95 96 97 98 99 100 101 102 103 104

///
/// Level-driven logging class that support printf and C++ stream logging fashions.
///
class Logger
{
public:
    Logger(int level, const char* file, int line, bool linefeed)
        : level_ {level}
        , file_ {file}
        , line_ {line}
        , linefeed_ {linefeed} {}

    Logger() = delete;
105
    Logger(const Logger&) = delete;
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
    Logger(Logger&&) = default;

    ~Logger() {
        log(level_, file_, line_, linefeed_, "%s", os_.str().c_str());
    }

    template <typename T>
    inline Logger& operator<<(const T& value) {
        os_ << value;
        return *this;
    }

    ///
    /// Printf fashion logging.
    ///
Adrien Béraud's avatar
Adrien Béraud committed
121
    /// Example: JAMI_DBG("%s", "Hello, World!")
122 123 124 125 126 127 128 129 130 131 132 133 134
    ///
    static void log(int level, const char* file, int line, bool linefeed,
                    const char* const fmt, ...) PRINTF_ATTRIBUTE(5, 6);

    ///
    /// Printf fashion logging (using va_list parameters)
    ///
    static void vlog(const int level, const char* file, int line, bool linefeed,
                     const char* format, va_list);

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

private:
    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<<)
};

// We need to use macros for contextual information
Adrien Béraud's avatar
Adrien Béraud committed
150 151 152 153
#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__)
154

Adrien Béraud's avatar
Adrien Béraud committed
155 156 157 158
#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__)
159

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