spdlog/lite/spdlite.h

227 lines
4.7 KiB
C
Raw Normal View History

2019-03-23 07:31:57 -04:00
//
2019-03-23 10:39:05 -04:00
// Copyright(c) 2015-present Gabi Melman.
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
2019-03-23 10:15:58 -04:00
#pragma once
2019-03-23 07:31:57 -04:00
#include <memory>
2019-03-23 10:15:58 -04:00
#include <string>
2019-03-23 07:31:57 -04:00
#include "spdlog/fmt/fmt.h"
namespace spdlog {
2019-03-23 10:39:32 -04:00
class logger;
namespace lite {
2019-03-23 14:06:49 -04:00
// string_view type - either std::string_view or fmt::string_view (pre c++17)
#if defined(FMT_USE_STD_STRING_VIEW)
2019-03-23 17:13:38 -04:00
using string_view_t = std::string_view;
2019-03-23 14:06:49 -04:00
#else
2019-03-23 17:13:38 -04:00
using string_view_t = fmt::string_view;
2019-03-23 14:06:49 -04:00
#endif
2019-03-23 10:39:32 -04:00
enum class level
{
2019-03-23 18:38:00 -04:00
trace,
debug,
info,
warn,
err,
critical,
off
2019-03-23 10:39:32 -04:00
};
class logger
{
public:
logger() = default;
2019-03-23 13:34:50 -04:00
explicit logger(std::shared_ptr<spdlog::logger> impl);
2019-03-23 10:39:32 -04:00
logger(const logger &) = default;
logger(logger &&) = default;
logger &operator=(const logger &) = default;
~logger() = default;
bool should_log(spdlog::lite::level lvl) const noexcept;
template<typename... Args>
void log(spdlog::lite::level lvl, const char *fmt, const Args &... args)
2019-03-23 10:39:32 -04:00
{
if (!should_log(lvl))
2019-03-23 10:15:58 -04:00
{
2019-03-23 10:39:32 -04:00
return;
2019-03-23 10:15:58 -04:00
}
2019-03-23 10:39:32 -04:00
fmt::memory_buffer formatted_buf;
fmt::format_to(formatted_buf, fmt, args...);
log_formatted_(lvl, formatted_buf);
2019-03-23 10:39:32 -04:00
}
// log string view
void log(spdlog::lite::level lvl, const string_view_t &sv);
void log_printf(spdlog::lite::level lvl, const char* format, va_list args);
//
2019-03-23 17:13:38 -04:00
// trace
//
2019-03-23 17:13:38 -04:00
void trace(const char *msg)
{
log(spdlog::lite::level::trace, string_view_t(msg));
2019-03-23 17:13:38 -04:00
}
2019-03-23 14:06:49 -04:00
2019-03-23 10:39:32 -04:00
template<typename... Args>
void trace(const char *fmt, const Args &... args)
{
log(spdlog::lite::level::trace, fmt, args...);
}
void trace_f(const char *printf_format, ...);
//
2019-03-23 17:13:38 -04:00
// debug
//
2019-03-23 17:13:38 -04:00
void debug(const char *msg)
2019-03-23 14:06:49 -04:00
{
log(spdlog::lite::level::debug, string_view_t(msg));
2019-03-23 14:06:49 -04:00
}
2019-03-23 10:39:32 -04:00
template<typename... Args>
void debug(const char *fmt, const Args &... args)
{
log(spdlog::lite::level::debug, fmt, args...);
}
void debug_f(const char *printf_format, ...);
//
2019-03-23 17:13:38 -04:00
// info
//
2019-03-23 17:13:38 -04:00
void info(const char *msg)
2019-03-23 14:06:49 -04:00
{
log(spdlog::lite::level::info, string_view_t(msg));
2019-03-23 14:06:49 -04:00
}
2019-03-23 10:39:32 -04:00
template<typename... Args>
void info(const char *fmt, const Args &... args)
{
log(spdlog::lite::level::info, fmt, args...);
}
void info_f(const char *printf_format, ...);
//
2019-03-23 17:13:38 -04:00
// warn
//
2019-03-23 17:13:38 -04:00
void warn(const char *msg)
2019-03-23 14:06:49 -04:00
{
log(spdlog::lite::level::warn, string_view_t(msg));
2019-03-23 14:06:49 -04:00
}
2019-03-23 10:39:32 -04:00
template<typename... Args>
void warn(const char *fmt, const Args &... args)
{
log(spdlog::lite::level::warn, fmt, args...);
}
void warn_f(const char *printf_format, ...);
//
2019-03-23 17:13:38 -04:00
// error
//
2019-03-23 17:13:38 -04:00
void error(const char *msg)
2019-03-23 14:06:49 -04:00
{
log(spdlog::lite::level::err, string_view_t(msg));
2019-03-23 14:06:49 -04:00
}
2019-03-23 10:39:32 -04:00
template<typename... Args>
void error(const char *fmt, const Args &... args)
{
log(spdlog::lite::level::err, fmt, args...);
}
void error_f(const char *printf_format, ...);
//
2019-03-23 17:13:38 -04:00
// critical
//
2019-03-23 17:13:38 -04:00
void critical(const char *msg)
2019-03-23 14:06:49 -04:00
{
log(spdlog::lite::level::critical, string_view_t(msg));
2019-03-23 14:06:49 -04:00
}
2019-03-23 10:39:32 -04:00
template<typename... Args>
void critical(const char *fmt, const Args &... args)
{
log(spdlog::lite::level::critical, fmt, args...);
}
void critical_f(const char *printf_format, ...);
//
// setters/getters
//
2019-03-23 10:39:32 -04:00
std::string name() const;
void set_level(lite::level level);
lite::level get_level() const;
//
// flush
//
2019-03-23 10:39:32 -04:00
void flush();
void flush_on(spdlog::lite::level log_level);
spdlog::lite::level flush_level() const;
void set_pattern(std::string pattern);
protected:
std::shared_ptr<spdlog::logger> impl_;
void log_formatted_(spdlog::lite::level lvl, const fmt::memory_buffer &formatted);
2019-03-23 10:39:32 -04:00
};
2019-03-23 13:34:50 -04:00
2019-03-23 10:39:32 -04:00
spdlog::lite::logger &default_logger();
template<typename... Args>
void trace(const char *fmt, const Args &... args)
{
default_logger().trace(fmt, args...);
}
2019-03-23 14:06:49 -04:00
2019-03-23 10:39:32 -04: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...);
}
} // namespace lite
// factory to create lite logger
// implement it in a dedicated compilation unit for fast compiles
spdlog::lite::logger create_lite(void *ctx = nullptr);
2019-03-23 10:15:58 -04:00
} // namespace spdlog