support for const char* messages

This commit is contained in:
gabime 2019-03-23 20:06:49 +02:00
parent 4c240edf94
commit 569c62e528
2 changed files with 59 additions and 5 deletions

View File

@ -24,7 +24,6 @@ bool spdlog::lite::logger::should_log(spdlog::lite::level level) const SPDLOG_NO
} }
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_formatted_(const spdlog::lite::src_loc &src, spdlog::lite::level lvl, const fmt::memory_buffer &formatted)
{ {
auto spd_level = to_spdlog_level(lvl); auto spd_level = to_spdlog_level(lvl);
@ -32,11 +31,19 @@ void spdlog::lite::logger::log_formatted_(const spdlog::lite::src_loc &src, spdl
impl_->log(source_loc, spd_level, spdlog::details::fmt_helper::to_string_view(formatted)); impl_->log(source_loc, spd_level, spdlog::details::fmt_helper::to_string_view(formatted));
} }
void spdlog::lite::logger::log_formatted_(spdlog::lite::level level, const fmt::memory_buffer &formatted) void spdlog::lite::logger::log_string_view_(const spdlog::lite::src_loc &src, spdlog::lite::level lvl, const string_view_t &sv)
{ {
log_formatted_(src_loc{}, level, formatted); 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);
} }
void spdlog::lite::logger::log_string_view_(spdlog::lite::level lvl, const string_view_t &sv)
{
log_string_view_(spdlog::lite::src_loc{}, lvl, sv);
}
void spdlog::lite::logger::set_level(spdlog::lite::level level) void spdlog::lite::logger::set_level(spdlog::lite::level level)
{ {
auto spd_level = to_spdlog_level(level); auto spd_level = to_spdlog_level(level);

View File

@ -77,10 +77,22 @@
#define SPDLITE_CRITICAL(...) (void)0 #define SPDLITE_CRITICAL(...) (void)0
#endif #endif
namespace spdlog { namespace spdlog {
class logger; class logger;
namespace lite { namespace lite {
// string_view type - either std::string_view or fmt::string_view (pre c++17)
#if defined(FMT_USE_STD_STRING_VIEW)
using string_view_t = std::string_view;
#else
using string_view_t = fmt::string_view;
#endif
enum class level enum class level
{ {
trace = SPDLITE_LEVEL_TRACE, trace = SPDLITE_LEVEL_TRACE,
@ -135,12 +147,18 @@ public:
log(spdlog::lite::src_loc{}, lvl, fmt, args...); log(spdlog::lite::src_loc{}, lvl, fmt, args...);
} }
template<typename... Args> template<typename... Args>
void trace(const char *fmt, const Args &... args) void trace(const char *fmt, const Args &... args)
{ {
log(spdlog::lite::level::trace, fmt, args...); log(spdlog::lite::level::trace, fmt, args...);
} }
void trace(const char* msg)
{
log_string_view_(spdlog::lite::level::trace, msg);
}
template<typename... Args> template<typename... Args>
void debug(const char *fmt, const Args &... args) void debug(const char *fmt, const Args &... args)
@ -148,29 +166,55 @@ public:
log(spdlog::lite::level::debug, fmt, args...); log(spdlog::lite::level::debug, fmt, args...);
} }
void debug(const char* msg)
{
log_string_view_(spdlog::lite::level::debug, msg);
}
template<typename... Args> template<typename... Args>
void info(const char *fmt, const Args &... args) void info(const char *fmt, const Args &... args)
{ {
log(spdlog::lite::level::info, fmt, args...); log(spdlog::lite::level::info, fmt, args...);
} }
void info(const char* msg)
{
log_string_view_(spdlog::lite::level::info, msg);
}
template<typename... Args> template<typename... Args>
void warn(const char *fmt, const Args &... args) void warn(const char *fmt, const Args &... args)
{ {
log(spdlog::lite::level::warn, fmt, args...); log(spdlog::lite::level::warn, fmt, args...);
} }
void warn(const char* msg)
{
log_string_view_(spdlog::lite::level::warn, msg);
}
template<typename... Args> template<typename... Args>
void error(const char *fmt, const Args &... args) void error(const char *fmt, const Args &... args)
{ {
log(spdlog::lite::level::err, fmt, args...); log(spdlog::lite::level::err, fmt, args...);
} }
void error(const char* msg)
{
log_string_view_(spdlog::lite::level::err, msg);
}
template<typename... Args> template<typename... Args>
void critical(const char *fmt, const Args &... args) void critical(const char *fmt, const Args &... args)
{ {
log(spdlog::lite::level::critical, fmt, args...); log(spdlog::lite::level::critical, fmt, args...);
} }
void critical(const char* msg)
{
log_string_view_(spdlog::lite::level::critical, msg);
}
std::string name() const; std::string name() const;
@ -188,8 +232,10 @@ public:
protected: protected:
std::shared_ptr<spdlog::logger> impl_; std::shared_ptr<spdlog::logger> impl_;
void log_formatted_(spdlog::lite::level lvl, const fmt::memory_buffer &formatted);
void log_formatted_(const spdlog::lite::src_loc &src, spdlog::lite::level lvl, const fmt::memory_buffer &formatted); 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);
}; };
spdlog::lite::logger &default_logger(); spdlog::lite::logger &default_logger();
@ -199,6 +245,7 @@ void trace(const char *fmt, const Args &... args)
{ {
default_logger().trace(fmt, args...); default_logger().trace(fmt, args...);
} }
template<typename... Args> template<typename... Args>
void debug(const char *fmt, const Args &... args) void debug(const char *fmt, const Args &... args)
{ {