You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
spdlog/lite/spdlite.h

228 lines
4.6 KiB
C

7 years ago
//
7 years ago
// Copyright(c) 2015-present Gabi Melman.
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
7 years ago
#pragma once
7 years ago
#include <memory>
7 years ago
#include <string>
7 years ago
#include "spdlog/fmt/fmt.h"
namespace spdlog {
7 years ago
class logger;
namespace lite {
// string_view type - either std::string_view or fmt::string_view (pre c++17)
#if defined(FMT_USE_STD_STRING_VIEW)
7 years ago
using string_view_t = std::string_view;
#else
7 years ago
using string_view_t = fmt::string_view;
#endif
7 years ago
enum class level
{
trace,
debug,
info,
warn,
err,
critical,
off
7 years ago
};
class logger
{
public:
logger() = default;
7 years ago
explicit logger(std::shared_ptr<spdlog::logger> impl);
7 years ago
logger(const logger &) = default;
logger(logger &&) = default;
logger &operator=(const logger &) = default;
~logger() = default;
7 years ago
bool should_log(lite::level lvl) const noexcept;
7 years ago
template<typename... Args>
7 years ago
void log(lite::level lvl, const char *fmt, const Args &... args)
7 years ago
{
if (!should_log(lvl))
7 years ago
{
7 years ago
return;
7 years ago
}
7 years ago
fmt::memory_buffer formatted_buf;
fmt::format_to(formatted_buf, fmt, args...);
log_formatted_(lvl, formatted_buf);
7 years ago
}
// log string view
7 years ago
void log(lite::level lvl, const string_view_t &sv);
void log_printf(lite::level lvl, const char *format, va_list args);
//
7 years ago
// trace
//
7 years ago
void trace(const char *msg)
{
7 years ago
log(lite::level::trace, string_view_t(msg));
7 years ago
}
7 years ago
template<typename... Args>
void trace(const char *fmt, const Args &... args)
{
7 years ago
log(lite::level::trace, fmt, args...);
7 years ago
}
7 years ago
void trace_f(const char *printf_format, ...);
//
7 years ago
// debug
//
7 years ago
void debug(const char *msg)
{
7 years ago
log(lite::level::debug, string_view_t(msg));
}
7 years ago
template<typename... Args>
void debug(const char *fmt, const Args &... args)
{
7 years ago
log(lite::level::debug, fmt, args...);
7 years ago
}
7 years ago
void debug_f(const char *printf_format, ...);
//
7 years ago
// info
//
7 years ago
void info(const char *msg)
{
7 years ago
log(lite::level::info, string_view_t(msg));
}
7 years ago
template<typename... Args>
void info(const char *fmt, const Args &... args)
{
7 years ago
log(lite::level::info, fmt, args...);
7 years ago
}
void info_f(const char *printf_format, ...);
//
7 years ago
// warn
//
7 years ago
void warn(const char *msg)
{
7 years ago
log(lite::level::warn, string_view_t(msg));
}
7 years ago
template<typename... Args>
void warn(const char *fmt, const Args &... args)
{
7 years ago
log(lite::level::warn, fmt, args...);
7 years ago
}
7 years ago
void warn_f(const char *printf_format, ...);
//
7 years ago
// error
//
7 years ago
void error(const char *msg)
{
7 years ago
log(lite::level::err, string_view_t(msg));
}
7 years ago
template<typename... Args>
void error(const char *fmt, const Args &... args)
{
7 years ago
log(lite::level::err, fmt, args...);
7 years ago
}
7 years ago
void error_f(const char *printf_format, ...);
//
7 years ago
// critical
//
7 years ago
void critical(const char *msg)
{
7 years ago
log(lite::level::critical, string_view_t(msg));
}
7 years ago
template<typename... Args>
void critical(const char *fmt, const Args &... args)
{
7 years ago
log(lite::level::critical, fmt, args...);
7 years ago
}
7 years ago
void critical_f(const char *printf_format, ...);
//
// setters/getters
//
7 years ago
std::string name() const;
void set_level(lite::level level);
lite::level get_level() const;
//
// flush
//
7 years ago
void flush();
7 years ago
void flush_on(lite::level log_level);
lite::level flush_level() const;
//
// set pattern
//
7 years ago
void set_pattern(std::string pattern);
protected:
std::shared_ptr<spdlog::logger> impl_;
7 years ago
void log_formatted_(lite::level lvl, const fmt::memory_buffer &formatted);
7 years ago
};
7 years ago
7 years ago
spdlog::lite::logger &default_logger();
template<typename... Args>
void trace(const char *fmt, const Args &... args)
{
default_logger().trace(fmt, args...);
}
7 years ago
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);
7 years ago
} // namespace spdlog