move lite namespace to topelevel spdlite

This commit is contained in:
gabime 2019-03-30 10:58:56 +03:00
parent 1842669104
commit bc7cd2ccc2
4 changed files with 61 additions and 62 deletions

View File

@ -6,7 +6,7 @@
#define UNUSED(x) (void)(x) #define UNUSED(x) (void)(x)
// example of creating lite logger with console and file sink // example of creating lite logger with console and file sink
spdlog::lite::logger spdlog::lite::create_logger(void *ctx) spdlite::logger spdlite::create_logger(void *ctx)
{ {
UNUSED(ctx); UNUSED(ctx);
std::shared_ptr<spdlog::logger> logger_impl; std::shared_ptr<spdlog::logger> logger_impl;
@ -19,5 +19,5 @@ spdlog::lite::logger spdlog::lite::create_logger(void *ctx)
logger_impl = std::make_unique<spdlog::logger>("my-logger", spdlog::sinks_init_list{console_sink, file_sink}); logger_impl = std::make_unique<spdlog::logger>("my-logger", spdlog::sinks_init_list{console_sink, file_sink});
logger_impl->set_level(spdlog::level::debug); logger_impl->set_level(spdlog::level::debug);
return spdlog::lite::logger(std::move(logger_impl)); return spdlite::logger(std::move(logger_impl));
} }

View File

@ -2,8 +2,8 @@
int main() int main()
{ {
auto l = spdlog::lite::create_logger(); auto l = spdlite::create_logger();
l.set_level(spdlog::lite::level::trace); l.set_level(spdlite::level::trace);
l.trace_printf("Hello %s ", "GABI"); l.trace_printf("Hello %s ", "GABI");
l.info_printf("Hello %d", 12346); l.info_printf("Hello %d", 12346);

View File

@ -1,34 +1,34 @@
#include "spdlite.h" #include "spdlite.h"
#include "spdlog/spdlog.h" #include "spdlog/spdlog.h"
static spdlog::level::level_enum to_spdlog_level(spdlog::lite::level level) static spdlog::level::level_enum to_spdlog_level(spdlite::level level)
{ {
return static_cast<spdlog::level::level_enum>(level); return static_cast<spdlog::level::level_enum>(level);
} }
static spdlog::lite::level to_lite_level(spdlog::level::level_enum level) static spdlite::level to_lite_level(spdlog::level::level_enum level)
{ {
return static_cast<spdlog::lite::level>(level); return static_cast<spdlite::level>(level);
} }
spdlog::lite::logger::logger(std::shared_ptr<spdlog::logger> impl) spdlite::logger::logger(std::shared_ptr<spdlog::logger> impl)
{ {
impl_ = std::move(impl); impl_ = std::move(impl);
} }
bool spdlog::lite::logger::should_log(spdlog::lite::level level) const SPDLOG_NOEXCEPT bool spdlite::logger::should_log(spdlite::level level) const SPDLOG_NOEXCEPT
{ {
auto spd_level = to_spdlog_level(level); auto spd_level = to_spdlog_level(level);
return impl_->should_log(spd_level); // TODO avoid the call using local level member? return impl_->should_log(spd_level); // TODO avoid the call using local level member?
} }
void spdlog::lite::logger::log(spdlog::lite::level lvl, const string_view_t &sv) void spdlite::logger::log(spdlite::level lvl, const string_view_t &sv)
{ {
auto spd_level = to_spdlog_level(lvl); auto spd_level = to_spdlog_level(lvl);
impl_->log(spd_level, sv); impl_->log(spd_level, sv);
} }
void spdlog::lite::logger::log_printf(spdlog::lite::level lvl, const char *format, va_list args) void spdlite::logger::log_printf(spdlite::level lvl, const char *format, va_list args)
{ {
char buffer[500]; char buffer[500];
auto size = vsnprintf(buffer, sizeof(buffer), format, args); auto size = vsnprintf(buffer, sizeof(buffer), format, args);
@ -39,105 +39,105 @@ void spdlog::lite::logger::log_printf(spdlog::lite::level lvl, const char *forma
log(lvl, string_view_t{buffer, static_cast<size_t>(size)}); log(lvl, string_view_t{buffer, static_cast<size_t>(size)});
} }
void spdlog::lite::logger::trace_printf(const char *format, ...) void spdlite::logger::trace_printf(const char *format, ...)
{ {
va_list args; va_list args;
va_start(args, format); va_start(args, format);
log_printf(lite::level::trace, format, args); log_printf(spdlite::level::trace, format, args);
va_end(args); va_end(args);
} }
void spdlog::lite::logger::debug_printf(const char *format, ...) void spdlite::logger::debug_printf(const char *format, ...)
{ {
va_list args; va_list args;
va_start(args, format); va_start(args, format);
log_printf(lite::level::debug, format, args); log_printf(spdlite::level::debug, format, args);
va_end(args); va_end(args);
} }
void spdlog::lite::logger::info_printf(const char *format, ...) void spdlite::logger::info_printf(const char *format, ...)
{ {
va_list args; va_list args;
va_start(args, format); va_start(args, format);
log_printf(lite::level::info, format, args); log_printf(spdlite::level::info, format, args);
va_end(args); va_end(args);
} }
void spdlog::lite::logger::warn_printf(const char *format, ...) void spdlite::logger::warn_printf(const char *format, ...)
{ {
va_list args; va_list args;
va_start(args, format); va_start(args, format);
log_printf(lite::level::warn, format, args); log_printf(spdlite::level::warn, format, args);
va_end(args); va_end(args);
} }
void spdlog::lite::logger::error_printf(const char *format, ...) void spdlite::logger::error_printf(const char *format, ...)
{ {
va_list args; va_list args;
va_start(args, format); va_start(args, format);
log_printf(lite::level::err, format, args); log_printf(spdlite::level::err, format, args);
va_end(args); va_end(args);
} }
void spdlog::lite::logger::critical_printf(const char *format, ...) void spdlite::logger::critical_printf(const char *format, ...)
{ {
va_list args; va_list args;
va_start(args, format); va_start(args, format);
log_printf(lite::level::critical, format, args); log_printf(spdlite::level::critical, format, args);
va_end(args); va_end(args);
} }
void spdlog::lite::logger::set_level(spdlog::lite::level level) noexcept void spdlite::logger::set_level(spdlite::level level) noexcept
{ {
auto spd_level = to_spdlog_level(level); auto spd_level = to_spdlog_level(level);
impl_->set_level(spd_level); impl_->set_level(spd_level);
} }
spdlog::lite::level spdlog::lite::logger::level() const noexcept spdlite::level spdlite::logger::level() const noexcept
{ {
return to_lite_level(impl_->level()); return to_lite_level(impl_->level());
} }
std::string spdlog::lite::logger::name() const noexcept std::string spdlite::logger::name() const noexcept
{ {
return impl_->name(); return impl_->name();
} }
void spdlog::lite::logger::flush() void spdlite::logger::flush()
{ {
impl_->flush(); impl_->flush();
} }
void spdlog::lite::logger::flush_on(spdlog::lite::level level) void spdlite::logger::flush_on(spdlite::level level)
{ {
auto spd_level = to_spdlog_level(level); auto spd_level = to_spdlog_level(level);
impl_->flush_on(spd_level); impl_->flush_on(spd_level);
} }
spdlog::lite::level spdlog::lite::logger::flush_level() const noexcept spdlite::level spdlite::logger::flush_level() const noexcept
{ {
return to_lite_level(impl_->flush_level()); return to_lite_level(impl_->flush_level());
} }
// pattern // pattern
void spdlog::lite::logger::set_pattern(std::string pattern) noexcept void spdlite::logger::set_pattern(std::string pattern) noexcept
{ {
impl_->set_pattern(std::move(pattern)); impl_->set_pattern(std::move(pattern));
} }
spdlog::lite::logger spdlog::lite::logger::clone(std::string logger_name) spdlite::logger spdlite::logger::clone(std::string logger_name)
{ {
return spdlog::lite::logger(impl_->clone(std::move(logger_name))); return spdlite::logger(impl_->clone(std::move(logger_name)));
} }
void spdlog::lite::logger::log_formatted_(spdlog::lite::level lvl, const fmt::memory_buffer &formatted) void spdlite::logger::log_formatted_(spdlite::level lvl, const fmt::memory_buffer &formatted)
{ {
auto spd_level = to_spdlog_level(lvl); auto spd_level = to_spdlog_level(lvl);
impl_->log(spd_level, spdlog::details::fmt_helper::to_string_view(formatted)); impl_->log(spd_level, spdlog::details::fmt_helper::to_string_view(formatted));
} }
spdlog::lite::logger &spdlog::lite::default_logger() spdlite::logger &spdlite::default_logger()
{ {
static spdlog::lite::logger s_default(spdlog::default_logger()); static spdlite::logger s_default(spdlog::default_logger());
return s_default; return s_default;
} }

View File

@ -18,8 +18,8 @@
namespace spdlog { namespace spdlog {
class logger; class logger;
}
namespace lite { namespace spdlite {
// string_view type - either std::string_view or fmt::string_view (pre c++17) // string_view type - either std::string_view or fmt::string_view (pre c++17)
#if defined(FMT_USE_STD_STRING_VIEW) #if defined(FMT_USE_STD_STRING_VIEW)
@ -49,10 +49,10 @@ public:
~logger() = default; ~logger() = default;
bool should_log(lite::level lvl) const noexcept; bool should_log(spdlite::level lvl) const noexcept;
template<typename... Args> template<typename... Args>
void log(lite::level lvl, const char *fmt, const Args &... args) void log(spdlite::level lvl, const char *fmt, const Args &... args)
{ {
if (!should_log(lvl)) if (!should_log(lvl))
{ {
@ -64,23 +64,23 @@ public:
} }
// log string view // log string view
void log(lite::level lvl, const string_view_t &sv); void log(spdlite::level lvl, const string_view_t &sv);
// log using printf format // log using printf format
void log_printf(lite::level lvl, const char *format, va_list args); void log_printf(spdlite::level lvl, const char *format, va_list args);
// //
// trace // trace
// //
void trace(const char *msg) void trace(const char *msg)
{ {
log(lite::level::trace, string_view_t(msg)); log(spdlite::level::trace, string_view_t(msg));
} }
template<typename... Args> template<typename... Args>
void trace(const char *fmt, const Args &... args) void trace(const char *fmt, const Args &... args)
{ {
log(lite::level::trace, fmt, args...); log(spdlite::level::trace, fmt, args...);
} }
void trace_printf(const char *format, ...); void trace_printf(const char *format, ...);
@ -90,13 +90,13 @@ public:
// //
void debug(const char *msg) void debug(const char *msg)
{ {
log(lite::level::debug, string_view_t(msg)); log(spdlite::level::debug, string_view_t(msg));
} }
template<typename... Args> template<typename... Args>
void debug(const char *fmt, const Args &... args) void debug(const char *fmt, const Args &... args)
{ {
log(lite::level::debug, fmt, args...); log(spdlite::level::debug, fmt, args...);
} }
void debug_printf(const char *format, ...); void debug_printf(const char *format, ...);
@ -104,13 +104,13 @@ public:
// info // info
void info(const char *msg) void info(const char *msg)
{ {
log(lite::level::info, string_view_t(msg)); log(spdlite::level::info, string_view_t(msg));
} }
template<typename... Args> template<typename... Args>
void info(const char *fmt, const Args &... args) void info(const char *fmt, const Args &... args)
{ {
log(lite::level::info, fmt, args...); log(spdlite::level::info, fmt, args...);
} }
void info_printf(const char *format, ...); void info_printf(const char *format, ...);
@ -118,13 +118,13 @@ public:
// warn // warn
void warn(const char *msg) void warn(const char *msg)
{ {
log(lite::level::warn, string_view_t(msg)); log(spdlite::level::warn, string_view_t(msg));
} }
template<typename... Args> template<typename... Args>
void warn(const char *fmt, const Args &... args) void warn(const char *fmt, const Args &... args)
{ {
log(lite::level::warn, fmt, args...); log(spdlite::level::warn, fmt, args...);
} }
void warn_printf(const char *format, ...); void warn_printf(const char *format, ...);
@ -132,13 +132,13 @@ public:
// error // error
void error(const char *msg) void error(const char *msg)
{ {
log(lite::level::err, string_view_t(msg)); log(spdlite::level::err, string_view_t(msg));
} }
template<typename... Args> template<typename... Args>
void error(const char *fmt, const Args &... args) void error(const char *fmt, const Args &... args)
{ {
log(lite::level::err, fmt, args...); log(spdlite::level::err, fmt, args...);
} }
void error_printf(const char *format, ...); void error_printf(const char *format, ...);
@ -146,37 +146,37 @@ public:
// critical // critical
void critical(const char *msg) void critical(const char *msg)
{ {
log(lite::level::critical, string_view_t(msg)); log(spdlite::level::critical, string_view_t(msg));
} }
template<typename... Args> template<typename... Args>
void critical(const char *fmt, const Args &... args) void critical(const char *fmt, const Args &... args)
{ {
log(lite::level::critical, fmt, args...); log(spdlite::level::critical, fmt, args...);
} }
void critical_printf(const char *format, ...); void critical_printf(const char *format, ...);
// setters/getters // setters/getters
void set_level(lite::level level) noexcept; void set_level(spdlite::level level) noexcept;
void set_pattern(std::string pattern) noexcept; void set_pattern(std::string pattern) noexcept;
lite::level level() const noexcept; spdlite::level level() const noexcept;
std::string name() const noexcept; std::string name() const noexcept;
lite::level flush_level() const noexcept; spdlite::level flush_level() const noexcept;
// flush // flush
void flush(); void flush();
void flush_on(lite::level log_level); void flush_on(spdlite::level log_level);
//clone with new name //clone with new name
spdlog::lite::logger clone(std::string logger_name); spdlite::logger clone(std::string logger_name);
protected: protected:
std::shared_ptr<spdlog::logger> impl_; std::shared_ptr<spdlog::logger> impl_;
void log_formatted_(lite::level lvl, const fmt::memory_buffer &formatted); void log_formatted_(spdlite::level lvl, const fmt::memory_buffer &formatted);
}; };
spdlog::lite::logger &default_logger(); spdlite::logger &default_logger();
template<typename... Args> template<typename... Args>
void trace(const char *fmt, const Args &... args) void trace(const char *fmt, const Args &... args)
@ -218,6 +218,5 @@ void critical(const char *fmt, const Args &... args)
// implement it in a seperated and dedicated compilation unit for fast compiles. // implement it in a seperated and dedicated compilation unit for fast compiles.
logger create_logger(void *ctx = nullptr); logger create_logger(void *ctx = nullptr);
} // namespace lite } // namespace spdlite
} // namespace spdlog