summaryrefslogtreecommitdiff
path: root/include/CommonAPI/Logger.hpp
blob: 0658be8faf753005f96f44bd48ebd3c019249b95 (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
// Copyright (C) 2015-2020 Bayerische Motoren Werke Aktiengesellschaft (BMW AG)
// 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/.

#if !defined (COMMONAPI_INTERNAL_COMPILATION)
#error "Only <CommonAPI/CommonAPI.hpp> can be included directly, this file may disappear or change contents."
#endif

#ifndef COMMONAPI_LOGGER_HPP_
#define COMMONAPI_LOGGER_HPP_

#include <memory>
#include <sstream>
#include <cstdint>

#include <CommonAPI/Export.hpp>

#define COMMONAPI_FATAL     CommonAPI::Logger::fatal
#define COMMONAPI_ERROR     CommonAPI::Logger::error
#define COMMONAPI_WARNING   CommonAPI::Logger::warning
#define COMMONAPI_INFO      CommonAPI::Logger::info
#define COMMONAPI_DEBUG     CommonAPI::Logger::debug
#define COMMONAPI_VERBOSE   CommonAPI::Logger::verbose

namespace CommonAPI {


class Logger {
public:
    enum class Level : std::uint8_t COMMONAPI_EXPORT {
        CAPI_LOG_NONE = 0,
        CAPI_LOG_FATAL = 1,
        CAPI_LOG_ERROR = 2,
        CAPI_LOG_WARNING = 3,
        CAPI_LOG_INFO = 4,
        CAPI_LOG_DEBUG = 5,
        CAPI_LOG_VERBOSE = 6
    };
    Logger();
    ~Logger();

    template<typename... LogEntries_>
    COMMONAPI_EXPORT static void fatal(LogEntries_&&... _entries) {
        log(Logger::Level::CAPI_LOG_FATAL, std::forward<LogEntries_>(_entries)...);
    }

    template<typename... LogEntries_>
    COMMONAPI_EXPORT static void error(LogEntries_&&... _entries) {
        log(Logger::Level::CAPI_LOG_ERROR, std::forward<LogEntries_>(_entries)...);
    }

    template<typename... LogEntries_>
    COMMONAPI_EXPORT static void warning(LogEntries_&&... _entries) {
        log(Logger::Level::CAPI_LOG_WARNING, std::forward<LogEntries_>(_entries)...);
    }

    template<typename... LogEntries_>
    COMMONAPI_EXPORT static void info(LogEntries_&&... _entries) {
        log(Logger::Level::CAPI_LOG_INFO, std::forward<LogEntries_>(_entries)...);
    }

    template<typename... LogEntries_>
    COMMONAPI_EXPORT static void debug(LogEntries_&&... _entries) {
        log(Logger::Level::CAPI_LOG_DEBUG, std::forward<LogEntries_>(_entries)...);
    }

    template<typename... LogEntries_>
    COMMONAPI_EXPORT static void verbose(LogEntries_&&... _entries) {
        log(Logger::Level::CAPI_LOG_VERBOSE, std::forward<LogEntries_>(_entries)...);
    }

    template<typename... LogEntries_>
    COMMONAPI_EXPORT static void log(Logger::Level _level, LogEntries_&&... _entries) {
        if (isLogged(_level)) {
            std::stringstream buffer;
            logIntern(buffer, std::forward<LogEntries_>(_entries)...);
            Logger::doLog(_level, buffer.str());
        }
    }

    static void init(bool _useConsole, const std::string &_fileName,
                     bool _useDlt, const std::string& _level);

private:
    class LoggerImpl;
    static std::unique_ptr<LoggerImpl> loggerImpl_;

    COMMONAPI_EXPORT static bool isLogged(Level _level);
    COMMONAPI_EXPORT static void doLog(Level _level, const std::string& _message);

    COMMONAPI_EXPORT static void logIntern(std::stringstream &_buffer) {
        (void)_buffer;
    }

    template<typename LogEntry_, typename... MoreLogEntries_>
    COMMONAPI_EXPORT static void logIntern(std::stringstream &_buffer, LogEntry_&& _entry,
                           MoreLogEntries_&& ... _moreEntries) {
        _buffer << _entry;
        logIntern(_buffer, std::forward<MoreLogEntries_>(_moreEntries)...);
    }
};

} // namespace CommonAPI

#endif // COMMONAPI_LOGGER_HPP_