spdlog/lite/spdlite.h

223 lines
5.0 KiB
C
Raw Normal View History

2019-03-23 19:31:57 +08:00
//
2019-03-23 22:39:05 +08:00
// Copyright(c) 2015-present Gabi Melman.
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
2019-03-23 22:15:58 +08:00
#pragma once
2019-03-23 19:31:57 +08:00
2019-03-24 07:50:56 +08:00
// 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.
2019-03-24 07:44:40 +08:00
2019-03-23 19:31:57 +08:00
#include <memory>
2019-03-23 22:15:58 +08:00
#include <string>
2019-03-23 19:31:57 +08:00
#include "spdlog/fmt/fmt.h"
namespace spdlog {
2019-03-23 22:39:32 +08:00
class logger;
}
namespace spdlite {
2019-03-24 02:06:49 +08:00
// string_view type - either std::string_view or fmt::string_view (pre c++17)
#if defined(FMT_USE_STD_STRING_VIEW)
2019-03-24 05:13:38 +08:00
using string_view_t = std::string_view;
2019-03-24 02:06:49 +08:00
#else
2019-03-24 05:13:38 +08:00
using string_view_t = fmt::string_view;
2019-03-24 02:06:49 +08:00
#endif
2019-03-23 22:39:32 +08:00
enum class level
{
2019-03-24 06:38:00 +08:00
trace,
debug,
info,
warn,
err,
critical,
off
2019-03-23 22:39:32 +08:00
};
class logger
{
public:
2019-03-29 19:38:40 +08:00
explicit logger(std::shared_ptr<spdlog::logger> impl);
2019-03-23 22:39:32 +08:00
logger(const logger &) = default;
logger(logger &&) = default;
logger &operator=(const logger &) = default;
~logger() = default;
bool should_log(spdlite::level lvl) const noexcept;
2019-03-23 22:39:32 +08:00
template<typename... Args>
void log(spdlite::level lvl, const char *fmt, const Args &... args)
2019-03-23 22:39:32 +08:00
{
if (!should_log(lvl))
2019-03-23 22:15:58 +08:00
{
2019-03-23 22:39:32 +08:00
return;
2019-03-23 22:15:58 +08:00
}
2019-03-23 22:39:32 +08:00
fmt::memory_buffer formatted_buf;
fmt::format_to(formatted_buf, fmt, args...);
log_formatted_(lvl, formatted_buf);
2019-03-23 22:39:32 +08:00
}
// log string view
void log(spdlite::level lvl, const string_view_t &sv);
2019-03-29 19:38:40 +08:00
// log using printf format
void log_printf(spdlite::level lvl, const char *format, va_list args);
//
2019-03-24 05:13:38 +08:00
// trace
//
2019-03-24 05:13:38 +08:00
void trace(const char *msg)
{
log(spdlite::level::trace, string_view_t(msg));
2019-03-24 05:13:38 +08:00
}
2019-03-24 02:06:49 +08:00
2019-03-23 22:39:32 +08:00
template<typename... Args>
void trace(const char *fmt, const Args &... args)
{
log(spdlite::level::trace, fmt, args...);
2019-03-23 22:39:32 +08:00
}
2019-03-24 07:21:07 +08:00
void trace_printf(const char *format, ...);
//
2019-03-24 05:13:38 +08:00
// debug
//
2019-03-24 05:13:38 +08:00
void debug(const char *msg)
2019-03-24 02:06:49 +08:00
{
log(spdlite::level::debug, string_view_t(msg));
2019-03-24 02:06:49 +08:00
}
2019-03-23 22:39:32 +08:00
template<typename... Args>
void debug(const char *fmt, const Args &... args)
{
log(spdlite::level::debug, fmt, args...);
2019-03-23 22:39:32 +08:00
}
2019-03-24 07:21:07 +08:00
void debug_printf(const char *format, ...);
2019-03-29 19:38:40 +08:00
// info
2019-03-24 05:13:38 +08:00
void info(const char *msg)
2019-03-24 02:06:49 +08:00
{
log(spdlite::level::info, string_view_t(msg));
2019-03-24 02:06:49 +08:00
}
2019-03-23 22:39:32 +08:00
template<typename... Args>
void info(const char *fmt, const Args &... args)
{
log(spdlite::level::info, fmt, args...);
2019-03-23 22:39:32 +08:00
}
2019-03-24 07:21:07 +08:00
void info_printf(const char *format, ...);
2019-03-24 05:13:38 +08:00
// warn
void warn(const char *msg)
2019-03-24 02:06:49 +08:00
{
log(spdlite::level::warn, string_view_t(msg));
2019-03-24 02:06:49 +08:00
}
2019-03-23 22:39:32 +08:00
template<typename... Args>
void warn(const char *fmt, const Args &... args)
{
log(spdlite::level::warn, fmt, args...);
2019-03-23 22:39:32 +08:00
}
2019-03-24 07:21:07 +08:00
void warn_printf(const char *format, ...);
2019-03-24 05:13:38 +08:00
// error
void error(const char *msg)
2019-03-24 02:06:49 +08:00
{
log(spdlite::level::err, string_view_t(msg));
2019-03-24 02:06:49 +08:00
}
2019-03-23 22:39:32 +08:00
template<typename... Args>
void error(const char *fmt, const Args &... args)
{
log(spdlite::level::err, fmt, args...);
2019-03-23 22:39:32 +08:00
}
2019-03-24 07:21:07 +08:00
void error_printf(const char *format, ...);
2019-03-24 05:13:38 +08:00
// critical
void critical(const char *msg)
2019-03-24 02:06:49 +08:00
{
log(spdlite::level::critical, string_view_t(msg));
2019-03-24 02:06:49 +08:00
}
2019-03-23 22:39:32 +08:00
template<typename... Args>
void critical(const char *fmt, const Args &... args)
{
log(spdlite::level::critical, fmt, args...);
2019-03-23 22:39:32 +08:00
}
2019-03-24 07:21:07 +08:00
void critical_printf(const char *format, ...);
2019-03-29 19:38:40 +08:00
// setters/getters
void set_level(spdlite::level level) noexcept;
2019-03-29 19:38:40 +08:00
void set_pattern(std::string pattern) noexcept;
spdlite::level level() const noexcept;
2019-03-29 19:38:40 +08:00
std::string name() const noexcept;
spdlite::level flush_level() const noexcept;
// flush
2019-03-23 22:39:32 +08:00
void flush();
void flush_on(spdlite::level log_level);
2019-03-29 19:38:40 +08:00
2019-03-24 07:40:20 +08:00
//clone with new name
spdlite::logger clone(std::string logger_name);
2019-03-24 07:40:20 +08:00
2019-03-23 22:39:32 +08:00
protected:
std::shared_ptr<spdlog::logger> impl_;
void log_formatted_(spdlite::level lvl, const fmt::memory_buffer &formatted);
2019-03-23 22:39:32 +08:00
};
2019-03-24 01:34:50 +08:00
spdlite::logger &default_logger();
2019-03-23 22:39:32 +08:00
template<typename... Args>
void trace(const char *fmt, const Args &... args)
{
default_logger().trace(fmt, args...);
}
2019-03-24 02:06:49 +08:00
2019-03-23 22:39:32 +08:00
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...);
}
2019-03-29 19:38:40 +08:00
// 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);
2019-03-23 22:39:32 +08:00
} // namespace spdlite
2019-03-23 22:15:58 +08:00