summaryrefslogtreecommitdiff
path: root/include/llmr/platform/log.hpp
blob: 7b393700462b72fd6b3515e8fedc4ee57eb5ca05 (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
#ifndef LLMR_PLATFORM_LOG
#define LLMR_PLATFORM_LOG

#include "event.hpp"

#include <memory>
#include <string>

namespace llmr {

class LogBackend {
public:
    virtual inline ~LogBackend() = default;
    virtual void record(EventSeverity severity, Event event, const std::string &msg) = 0;
    virtual void record(EventSeverity severity, Event event, const char* format, ...) = 0;
    virtual void record(EventSeverity severity, Event event, int64_t code) = 0;
    virtual void record(EventSeverity severity, Event event, int64_t code, const std::string &msg) = 0;
};

class Log {
private:
    template <typename T>
    constexpr static bool includes(const T e, T const *l, const size_t i = 0) {
        return i >= sizeof l ? false : *(l + i) == e ? true : includes(e, l, i + 1);
    }

public:
    template <typename ...Args>
    static inline void Debug(Event event, Args&& ...args) {
        Record(EventSeverity::Debug, event, ::std::forward<Args>(args)...);
    }

    template <typename ...Args>
    static inline void Info(Event event, Args&& ...args) {
        Record(EventSeverity::Info, event, ::std::forward<Args>(args)...);
    }

    template <typename ...Args>
    static inline void Warning(Event event, Args&& ...args) {
        Record(EventSeverity::Warning, event, ::std::forward<Args>(args)...);
    }

    template <typename ...Args>
    static inline void Error(Event event, Args&& ...args) {
        Record(EventSeverity::Error, event, ::std::forward<Args>(args)...);
    }

    template <typename ...Args>
    static inline void Record(EventSeverity severity, Event event, Args&& ...args) {
        if (!includes(severity, disabledEventSeverities) &&
            !includes(event, disabledEvents) &&
            !includes({ severity, event }, disabledEventPermutations)) {
            if (Backend) {
                Backend->record(severity, event, ::std::forward<Args>(args)...);
            }
        }
    }

    template<typename T, typename ...Args>
    static inline const T &Set(Args&& ...args) {
        Backend = ::std::unique_ptr<T>(new T(::std::forward<Args>(args)...));
        return *dynamic_cast<T *>(Backend.get());
    }

private:
    static std::unique_ptr<LogBackend> Backend;
};

}

#endif