added printf syntax support for even faster compilation!

This commit is contained in:
gabime 2019-03-24 00:30:08 +02:00
parent 4449bf6f83
commit 621e0a8330
4 changed files with 121 additions and 46 deletions

View File

@ -3,9 +3,10 @@
int main()
{
// auto l = spdlog::create_lite();
// spdlog::lite::info("HELLO info {}", 123);
using namespace spdlog;
auto l = spdlog::default_logger();
l->set_level(spdlog::level::trace);
l->trace("hello123123213 ");
SPDLOG_TRACE("SOME MACRO {}", 123);
SPDLOG_INFO("SOME MACRO {}", "HHHHH");
}

View File

@ -5,7 +5,9 @@ int main()
using namespace spdlog;
auto l = spdlog::create_lite();
l.set_level(spdlog::lite::level::trace);
lite::default_logger().set_level(l.get_level());
lite::trace("hello");
lite::trace("hello {}", std::string("again"));
l.trace_f("Hello %s ","GABI");
l.info_f("Hello %d", 12346);
}

View File

@ -23,23 +23,71 @@ bool spdlog::lite::logger::should_log(spdlog::lite::level level) const SPDLOG_NO
return impl_->should_log(spd_level); // TODO avoid the call using local level member?
}
void spdlog::lite::logger::log_formatted_(const spdlog::lite::src_loc &src, spdlog::lite::level lvl, const fmt::memory_buffer &formatted)
void spdlog::lite::logger::log(spdlog::lite::level lvl, const string_view_t &sv)
{
auto spd_level = to_spdlog_level(lvl);
spdlog::source_loc source_loc{src.filename, src.line, src.funcname};
impl_->log(source_loc, spd_level, spdlog::details::fmt_helper::to_string_view(formatted));
impl_->log(spd_level, sv);
}
void spdlog::lite::logger::log_string_view_(const spdlog::lite::src_loc &src, spdlog::lite::level lvl, const string_view_t &sv)
void spdlog::lite::logger::log_printf(spdlog::lite::level lvl, const char* format, va_list args)
{
auto spd_level = to_spdlog_level(lvl);
spdlog::source_loc source_loc{src.filename, src.line, src.funcname};
impl_->log(source_loc, spd_level, sv);
char buffer[500];
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)});
}
void spdlog::lite::logger::log_string_view_(spdlog::lite::level lvl, const string_view_t &sv)
void spdlog::lite::logger::trace_f(const char *format, ...)
{
log_string_view_(spdlog::lite::src_loc{}, lvl, sv);
va_list args;
va_start (args, format);
log_printf(lite::level::trace, format, args);
va_end (args);
}
void spdlog::lite::logger::debug_f(const char *format, ...)
{
va_list args;
va_start (args, format);
log_printf(lite::level::debug, format, args);
va_end (args);
}
void spdlog::lite::logger::info_f(const char *format, ...)
{
va_list args;
va_start (args, format);
log_printf(lite::level::info, format, args);
va_end (args);
}
void spdlog::lite::logger::warn_f(const char *format, ...)
{
va_list args;
va_start (args, format);
log_printf(lite::level::warn, format, args);
va_end (args);
}
void spdlog::lite::logger::error_f(const char *format, ...)
{
va_list args;
va_start (args, format);
log_printf(lite::level::err, format, args);
va_end (args);
}
void spdlog::lite::logger::critical_f(const char *format, ...)
{
va_list args;
va_start (args, format);
log_printf(lite::level::critical, format, args);
va_end (args);
}
void spdlog::lite::logger::set_level(spdlog::lite::level level)
@ -80,8 +128,16 @@ void spdlog::lite::logger::set_pattern(std::string pattern)
impl_->set_pattern(std::move(pattern));
}
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));
}
spdlog::lite::logger &spdlog::lite::default_logger()
{
static spdlog::lite::logger s_default(spdlog::default_logger());
return s_default;
}

View File

@ -97,20 +97,6 @@ enum class level
off = SPDLITE_LEVEL_OFF
};
struct src_loc
{
src_loc() = default;
src_loc(const char *filename, int line, const char *funcname)
: filename(filename)
, line(line)
, funcname(funcname)
{
}
const char *filename = nullptr;
int line = 0;
const char *funcname = nullptr;
};
class logger
{
@ -127,7 +113,7 @@ public:
bool should_log(spdlog::lite::level lvl) const noexcept;
template<typename... Args>
void log(const spdlog::lite::src_loc &src, spdlog::lite::level lvl, const char *fmt, const Args &... args)
void log(spdlog::lite::level lvl, const char *fmt, const Args &... args)
{
if (!should_log(lvl))
{
@ -135,19 +121,20 @@ public:
}
fmt::memory_buffer formatted_buf;
fmt::format_to(formatted_buf, fmt, args...);
log_formatted_(src, lvl, formatted_buf);
log_formatted_(lvl, formatted_buf);
}
template<typename... Args>
void log(spdlog::lite::level lvl, const char *fmt, const Args &... args)
{
log(spdlog::lite::src_loc{}, lvl, fmt, args...);
}
// 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);
//
// trace
//
void trace(const char *msg)
{
log_string_view_(spdlog::lite::level::trace, msg);
log(spdlog::lite::level::trace, string_view_t(msg));
}
template<typename... Args>
@ -156,10 +143,14 @@ public:
log(spdlog::lite::level::trace, fmt, args...);
}
void trace_f(const char *printf_format, ...);
//
// debug
//
void debug(const char *msg)
{
log_string_view_(spdlog::lite::level::debug, msg);
log(spdlog::lite::level::debug, string_view_t(msg));
}
template<typename... Args>
@ -168,10 +159,14 @@ public:
log(spdlog::lite::level::debug, fmt, args...);
}
void debug_f(const char *printf_format, ...);
//
// info
//
void info(const char *msg)
{
log_string_view_(spdlog::lite::level::info, msg);
log(spdlog::lite::level::info, string_view_t(msg));
}
template<typename... Args>
@ -180,10 +175,14 @@ public:
log(spdlog::lite::level::info, fmt, args...);
}
void info_f(const char *printf_format, ...);
//
// warn
//
void warn(const char *msg)
{
log_string_view_(spdlog::lite::level::warn, msg);
log(spdlog::lite::level::warn, string_view_t(msg));
}
template<typename... Args>
@ -192,22 +191,31 @@ public:
log(spdlog::lite::level::warn, fmt, args...);
}
void warn_f(const char *printf_format, ...);
//
// error
//
void error(const char *msg)
{
log_string_view_(spdlog::lite::level::err, msg);
log(spdlog::lite::level::err, string_view_t(msg));
}
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, ...);
//
// critical
//
void critical(const char *msg)
{
log_string_view_(spdlog::lite::level::critical, msg);
log(spdlog::lite::level::critical, string_view_t(msg));
}
template<typename... Args>
@ -216,9 +224,18 @@ public:
log(spdlog::lite::level::critical, fmt, args...);
}
void critical_f(const char *printf_format, ...);
//
// setters/getters
//
std::string name() const;
void set_level(lite::level level);
lite::level get_level() const;
//
// flush
//
void flush();
void flush_on(spdlog::lite::level log_level);
spdlog::lite::level flush_level() const;
@ -226,9 +243,8 @@ public:
protected:
std::shared_ptr<spdlog::logger> impl_;
void log_formatted_(const spdlog::lite::src_loc &src, spdlog::lite::level lvl, const fmt::memory_buffer &formatted);
void log_string_view_(const spdlog::lite::src_loc &src, spdlog::lite::level lvl, const string_view_t &sv);
void log_string_view_(spdlog::lite::level lvl, const string_view_t &sv);
void log_formatted_(spdlog::lite::level lvl, const fmt::memory_buffer &formatted);
};
spdlog::lite::logger &default_logger();