spdlog/lite/spdlite.cpp

144 lines
3.5 KiB
C++
Raw Normal View History

2019-03-23 23:25:50 +08:00
#include "spdlite.h"
2019-03-23 19:31:57 +08:00
#include "spdlog/spdlog.h"
2019-03-23 22:39:32 +08:00
static spdlog::level::level_enum to_spdlog_level(spdlog::lite::level level)
2019-03-23 22:15:58 +08:00
{
2019-03-23 22:39:32 +08:00
return static_cast<spdlog::level::level_enum>(level);
2019-03-23 22:15:58 +08:00
}
static spdlog::lite::level to_lite_level(spdlog::level::level_enum level)
{
return static_cast<spdlog::lite::level>(level);
}
2019-03-23 19:31:57 +08:00
spdlog::lite::logger::logger(std::shared_ptr<spdlog::logger> impl)
{
impl_ = std::move(impl);
}
2019-03-23 22:15:58 +08:00
bool spdlog::lite::logger::should_log(spdlog::lite::level level) const SPDLOG_NOEXCEPT
2019-03-23 19:31:57 +08:00
{
2019-03-23 22:15:58 +08:00
auto spd_level = to_spdlog_level(level);
2019-03-24 01:34:50 +08:00
return impl_->should_log(spd_level); // TODO avoid the call using local level member?
2019-03-23 19:31:57 +08:00
}
void spdlog::lite::logger::log(spdlog::lite::level lvl, const string_view_t &sv)
2019-03-23 22:15:58 +08:00
{
auto spd_level = to_spdlog_level(lvl);
impl_->log(spd_level, sv);
2019-03-23 22:15:58 +08:00
}
2019-03-24 06:41:22 +08:00
void spdlog::lite::logger::log_printf(spdlog::lite::level lvl, const char *format, va_list args)
2019-03-24 01:34:50 +08:00
{
char buffer[500];
2019-03-24 06:41:22 +08:00
auto size = vsnprintf(buffer, sizeof(buffer), format, args);
if (size < 0)
{
size = snprintf(buffer, sizeof(buffer), "invalid format (%s)", format);
}
log(lvl, string_view_t{buffer, static_cast<size_t>(size)});
}
2019-03-24 07:21:07 +08:00
void spdlog::lite::logger::trace_printf(const char *format, ...)
{
va_list args;
2019-03-24 06:41:22 +08:00
va_start(args, format);
log_printf(lite::level::trace, format, args);
2019-03-24 06:41:22 +08:00
va_end(args);
}
2019-03-24 07:21:07 +08:00
void spdlog::lite::logger::debug_printf(const char *format, ...)
{
va_list args;
2019-03-24 06:41:22 +08:00
va_start(args, format);
log_printf(lite::level::debug, format, args);
2019-03-24 06:41:22 +08:00
va_end(args);
}
2019-03-24 07:21:07 +08:00
void spdlog::lite::logger::info_printf(const char *format, ...)
{
va_list args;
2019-03-24 06:41:22 +08:00
va_start(args, format);
log_printf(lite::level::info, format, args);
2019-03-24 06:41:22 +08:00
va_end(args);
2019-03-24 01:34:50 +08:00
}
2019-03-24 07:21:07 +08:00
void spdlog::lite::logger::warn_printf(const char *format, ...)
2019-03-24 02:06:49 +08:00
{
va_list args;
2019-03-24 06:41:22 +08:00
va_start(args, format);
log_printf(lite::level::warn, format, args);
2019-03-24 06:41:22 +08:00
va_end(args);
}
2019-03-24 07:21:07 +08:00
void spdlog::lite::logger::error_printf(const char *format, ...)
{
va_list args;
2019-03-24 06:41:22 +08:00
va_start(args, format);
log_printf(lite::level::err, format, args);
2019-03-24 06:41:22 +08:00
va_end(args);
}
2019-03-24 07:21:07 +08:00
void spdlog::lite::logger::critical_printf(const char *format, ...)
{
va_list args;
2019-03-24 06:41:22 +08:00
va_start(args, format);
log_printf(lite::level::critical, format, args);
2019-03-24 06:41:22 +08:00
va_end(args);
2019-03-24 02:06:49 +08:00
}
2019-03-29 19:38:40 +08:00
void spdlog::lite::logger::set_level(spdlog::lite::level level) noexcept
2019-03-23 22:15:58 +08:00
{
auto spd_level = to_spdlog_level(level);
impl_->set_level(spd_level);
}
2019-03-29 19:38:40 +08:00
spdlog::lite::level spdlog::lite::logger::level() const noexcept
2019-03-23 22:15:58 +08:00
{
return to_lite_level(impl_->level());
}
2019-03-29 19:38:40 +08:00
std::string spdlog::lite::logger::name() const noexcept
2019-03-23 22:15:58 +08:00
{
return impl_->name();
}
void spdlog::lite::logger::flush()
{
impl_->flush();
}
void spdlog::lite::logger::flush_on(spdlog::lite::level level)
{
auto spd_level = to_spdlog_level(level);
impl_->flush_on(spd_level);
}
2019-03-29 19:38:40 +08:00
spdlog::lite::level spdlog::lite::logger::flush_level() const noexcept
2019-03-23 22:15:58 +08:00
{
return to_lite_level(impl_->flush_level());
}
// pattern
2019-03-29 19:38:40 +08:00
void spdlog::lite::logger::set_pattern(std::string pattern) noexcept
2019-03-23 22:15:58 +08:00
{
impl_->set_pattern(std::move(pattern));
}
2019-03-24 07:40:20 +08:00
spdlog::lite::logger spdlog::lite::logger::clone(std::string logger_name)
{
return spdlog::lite::logger(impl_->clone(std::move(logger_name)));
}
void spdlog::lite::logger::log_formatted_(spdlog::lite::level lvl, const fmt::memory_buffer &formatted)
{
auto spd_level = to_spdlog_level(lvl);
impl_->log(spd_level, spdlog::details::fmt_helper::to_string_view(formatted));
}
2019-03-23 22:15:58 +08:00
spdlog::lite::logger &spdlog::lite::default_logger()
{
static spdlog::lite::logger s_default(spdlog::default_logger());
return s_default;
}