spdlite.h 4.99 KB
Newer Older
gabime's avatar
gabime committed
1
//
gabime's avatar
gabime committed
2 3 4
// Copyright(c) 2015-present Gabi Melman.
// Distributed under the MIT License (http://opensource.org/licenses/MIT)

gabime's avatar
gabime committed
5
#pragma once
gabime's avatar
gabime committed
6

gabime's avatar
gabime committed
7 8 9 10 11 12 13
// lite logger - a pimpl around spdlog::logger shared_ptr:
//  much faster compile times.
//  can be used as lib or separate compilation unit.
//  very cheap copy and move.
//  supports printf format for even faster compile (by avoiding variadic templates).
//
// see lite-example/ for usage.
gabime's avatar
gabime committed
14

gabime's avatar
gabime committed
15
#include <memory>
gabime's avatar
gabime committed
16
#include <string>
gabime's avatar
gabime committed
17 18 19
#include "spdlog/fmt/fmt.h"

namespace spdlog {
gabime's avatar
gabime committed
20
class logger;
21 22
}
namespace spdlite {
23 24 25

// string_view type - either std::string_view or fmt::string_view (pre c++17)
#if defined(FMT_USE_STD_STRING_VIEW)
gabime's avatar
gabime committed
26
using string_view_t = std::string_view;
27
#else
gabime's avatar
gabime committed
28
using string_view_t = fmt::string_view;
29 30
#endif

gabime's avatar
gabime committed
31 32
enum class level
{
33 34 35 36 37 38 39
    trace,
    debug,
    info,
    warn,
    err,
    critical,
    off
gabime's avatar
gabime committed
40 41 42 43 44
};

class logger
{
public:
gabime's avatar
gabime committed
45
    explicit logger(std::shared_ptr<spdlog::logger> impl);    
gabime's avatar
gabime committed
46 47 48 49 50 51
    logger(const logger &) = default;
    logger(logger &&) = default;
    logger &operator=(const logger &) = default;

    ~logger() = default;

52
    bool should_log(spdlite::level lvl) const noexcept;
gabime's avatar
gabime committed
53 54

    template<typename... Args>
55
    void log(spdlite::level lvl, const char *fmt, const Args &... args)
gabime's avatar
gabime committed
56 57
    {
        if (!should_log(lvl))
gabime's avatar
gabime committed
58
        {
gabime's avatar
gabime committed
59
            return;
gabime's avatar
gabime committed
60
        }
gabime's avatar
gabime committed
61 62
        fmt::memory_buffer formatted_buf;
        fmt::format_to(formatted_buf, fmt, args...);
63
        log_formatted_(lvl, formatted_buf);
gabime's avatar
gabime committed
64 65
    }

66
    // log string view
67
    void log(spdlite::level lvl, const string_view_t &sv);
gabime's avatar
gabime committed
68 69

	// log using printf format
70
    void log_printf(spdlite::level lvl, const char *format, va_list args);
71 72

    //
gabime's avatar
gabime committed
73
    // trace
74
    //
gabime's avatar
gabime committed
75 76
    void trace(const char *msg)
    {
77
        log(spdlite::level::trace, string_view_t(msg));
gabime's avatar
gabime committed
78
    }
79

gabime's avatar
gabime committed
80 81 82
    template<typename... Args>
    void trace(const char *fmt, const Args &... args)
    {
83
        log(spdlite::level::trace, fmt, args...);
gabime's avatar
gabime committed
84 85
    }

gabime's avatar
gabime committed
86
    void trace_printf(const char *format, ...);
87 88

    //
gabime's avatar
gabime committed
89
    // debug
90
    //
gabime's avatar
gabime committed
91
    void debug(const char *msg)
92
    {
93
        log(spdlite::level::debug, string_view_t(msg));
94 95
    }

gabime's avatar
gabime committed
96 97 98
    template<typename... Args>
    void debug(const char *fmt, const Args &... args)
    {
99
        log(spdlite::level::debug, fmt, args...);
gabime's avatar
gabime committed
100 101
    }

gabime's avatar
gabime committed
102
    void debug_printf(const char *format, ...);
gabime's avatar
gabime committed
103 104
    
    // info    
gabime's avatar
gabime committed
105
    void info(const char *msg)
106
    {
107
        log(spdlite::level::info, string_view_t(msg));
108 109
    }

gabime's avatar
gabime committed
110 111 112
    template<typename... Args>
    void info(const char *fmt, const Args &... args)
    {
113
        log(spdlite::level::info, fmt, args...);
gabime's avatar
gabime committed
114 115
    }

gabime's avatar
gabime committed
116
    void info_printf(const char *format, ...);
117

gabime's avatar
gabime committed
118 119
    // warn
    void warn(const char *msg)
120
    {
121
        log(spdlite::level::warn, string_view_t(msg));
122 123
    }

gabime's avatar
gabime committed
124 125 126
    template<typename... Args>
    void warn(const char *fmt, const Args &... args)
    {
127
        log(spdlite::level::warn, fmt, args...);
gabime's avatar
gabime committed
128 129
    }

gabime's avatar
gabime committed
130
    void warn_printf(const char *format, ...);
131

gabime's avatar
gabime committed
132 133
    // error
    void error(const char *msg)
134
    {
135
        log(spdlite::level::err, string_view_t(msg));
136 137
    }

gabime's avatar
gabime committed
138 139 140
    template<typename... Args>
    void error(const char *fmt, const Args &... args)
    {
141
        log(spdlite::level::err, fmt, args...);
gabime's avatar
gabime committed
142 143
    }

gabime's avatar
gabime committed
144
    void error_printf(const char *format, ...);
145

gabime's avatar
gabime committed
146 147
    // critical
    void critical(const char *msg)
148
    {
149
        log(spdlite::level::critical, string_view_t(msg));
150 151
    }

gabime's avatar
gabime committed
152 153 154
    template<typename... Args>
    void critical(const char *fmt, const Args &... args)
    {
155
        log(spdlite::level::critical, fmt, args...);
gabime's avatar
gabime committed
156 157
    }

gabime's avatar
gabime committed
158
    void critical_printf(const char *format, ...);
159

gabime's avatar
gabime committed
160
    // setters/getters	
161
    void set_level(spdlite::level level) noexcept;    
gabime's avatar
gabime committed
162
    void set_pattern(std::string pattern) noexcept;
163
    spdlite::level level() const noexcept;
gabime's avatar
gabime committed
164
    std::string name() const noexcept;
165
    spdlite::level flush_level() const noexcept;
166 167

    // flush
gabime's avatar
gabime committed
168
    void flush();
169
    void flush_on(spdlite::level log_level);
gabime's avatar
gabime committed
170
          
gabime's avatar
gabime committed
171
    //clone with new name
172
    spdlite::logger clone(std::string logger_name);
gabime's avatar
gabime committed
173

gabime's avatar
gabime committed
174 175
protected:
    std::shared_ptr<spdlog::logger> impl_;
176
    void log_formatted_(spdlite::level lvl, const fmt::memory_buffer &formatted);
gabime's avatar
gabime committed
177
};
gabime's avatar
gabime committed
178

179
spdlite::logger &default_logger();
gabime's avatar
gabime committed
180 181 182 183 184 185

template<typename... Args>
void trace(const char *fmt, const Args &... args)
{
    default_logger().trace(fmt, args...);
}
186

gabime's avatar
gabime committed
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
template<typename... Args>
void debug(const char *fmt, const Args &... args)
{
    default_logger().debug(fmt, args...);
}

template<typename... Args>
void info(const char *fmt, const Args &... args)
{
    default_logger().info(fmt, args...);
}

template<typename... Args>
void warn(const char *fmt, const Args &... args)
{
    default_logger().warn(fmt, args...);
}

template<typename... Args>
void error(const char *fmt, const Args &... args)
{
    default_logger().error(fmt, args...);
}

template<typename... Args>
void critical(const char *fmt, const Args &... args)
{
    default_logger().critical(fmt, args...);
}

gabime's avatar
gabime committed
217 218 219
// user implemented factory to create lite logger
// implement it in a seperated and dedicated compilation unit for fast compiles.
logger create_logger(void *ctx = nullptr);
gabime's avatar
gabime committed
220

221
} // namespace spdlite
gabime's avatar
gabime committed
222