This commit is contained in:
gabime 2019-03-30 13:49:54 +03:00
parent bc7cd2ccc2
commit e32c856a04
4 changed files with 158 additions and 63 deletions

View File

@ -2,22 +2,13 @@
#include "spdlog/spdlog.h" #include "spdlog/spdlog.h"
#include "spdlog/sinks/basic_file_sink.h" #include "spdlog/sinks/basic_file_sink.h"
#include "spdlog/sinks/stdout_color_sinks.h" #include "spdlog/sinks/stdout_color_sinks.h"
#include "spdlog/sinks/stdout_sinks.h"
#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
spdlite::logger spdlite::create_logger(void *ctx) spdlite::logger create_logger(void *ctx)
{ {
UNUSED(ctx); UNUSED(ctx);
std::shared_ptr<spdlog::logger> logger_impl; return spdlite::default_logger();
auto console_sink = std::make_shared<spdlog::sinks::stdout_color_sink_mt> ();
console_sink->set_level(spdlog::level::debug);
auto file_sink = std::make_shared<spdlog::sinks::basic_file_sink_mt > ("log.txt", true);
file_sink ->set_level(spdlog::level::info);
logger_impl = std::make_unique<spdlog::logger>("my-logger", spdlog::sinks_init_list{console_sink, file_sink});
logger_impl->set_level(spdlog::level::debug);
return spdlite::logger(std::move(logger_impl));
} }

View File

@ -2,18 +2,11 @@
int main() int main()
{ {
auto l = spdlite::create_logger(); spdlite::default_logger().set_level(spdlite::level::trace);
l.set_level(spdlite::level::trace); spdlite::trace_printf("Hello %d", 123);
spdlite::debug_printf("Hello %d", 123);
l.trace_printf("Hello %s ", "GABI"); spdlite::info_printf("Hello %d", 123);
l.info_printf("Hello %d", 12346); spdlite::warn_printf("Hello %d", 123);
l.warn_printf("Hello %f", 12346.5656); spdlite::error_printf("Hello %d", 123);
l.warn("Hello {}", "LITE :) "); spdlite::critical_printf("Hello %d", 123);
auto l2 = l.clone("logger2");
l2.debug("HELLO");
auto l3 = std::move(l);
l3.warn("HELLO FROM L3");
} }

View File

@ -16,6 +16,11 @@ spdlite::logger::logger(std::shared_ptr<spdlog::logger> impl)
impl_ = std::move(impl); impl_ = std::move(impl);
} }
void spdlite::logger::set_impl(std::shared_ptr<spdlog::logger> impl)
{
impl_ = std::move(impl);
}
bool spdlite::logger::should_log(spdlite::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);
@ -30,7 +35,7 @@ void spdlite::logger::log(spdlite::level lvl, const string_view_t &sv)
void spdlite::logger::log_printf(spdlite::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[256];
auto size = vsnprintf(buffer, sizeof(buffer), format, args); auto size = vsnprintf(buffer, sizeof(buffer), format, args);
if (size < 0) if (size < 0)
{ {
@ -136,8 +141,68 @@ void spdlite::logger::log_formatted_(spdlite::level lvl, const fmt::memory_buffe
impl_->log(spd_level, spdlog::details::fmt_helper::to_string_view(formatted)); impl_->log(spd_level, spdlog::details::fmt_helper::to_string_view(formatted));
} }
spdlite::logger &spdlite::logger::default_logger()
{
static spdlite::logger default_inst_ = spdlite::logger(spdlog::default_logger());
return default_inst_;
}
spdlite::logger &spdlite::default_logger() spdlite::logger &spdlite::default_logger()
{ {
static spdlite::logger s_default(spdlog::default_logger()); return spdlite::logger::default_logger();
return s_default;
} }
// printf
void spdlite::log_printf(spdlite::level lvl, const char *format, va_list args)
{
default_logger().log_printf(lvl, format, args);
}
void spdlite::trace_printf(const char *format, ...)
{
va_list args;
va_start(args, format);
log_printf(level::trace, format, args);
va_end(args);
}
void spdlite::debug_printf(const char *format, ...)
{
va_list args;
va_start(args, format);
log_printf(level::debug, format, args);
va_end(args);
}
void spdlite::info_printf(const char *format, ...)
{
va_list args;
va_start(args, format);
log_printf(level::info, format, args);
va_end(args);
}
void spdlite::warn_printf(const char *format, ...)
{
va_list args;
va_start(args, format);
log_printf(level::warn, format, args);
va_end(args);
}
void spdlite::error_printf(const char *format, ...)
{
va_list args;
va_start(args, format);
log_printf(level::err, format, args);
va_end(args);
}
void spdlite::critical_printf(const char *format, ...)
{
va_list args;
va_start(args, format);
log_printf(level::critical, format, args);
va_end(args);
}

View File

@ -43,12 +43,14 @@ class logger
{ {
public: public:
explicit logger(std::shared_ptr<spdlog::logger> impl); explicit logger(std::shared_ptr<spdlog::logger> impl);
// logger() = default; //logger with nullptr impl
logger(const logger &) = default; logger(const logger &) = default;
logger(logger &&) = default; logger(logger &&) = default;
logger &operator=(const logger &) = default; logger &operator=(const logger &) = default;
~logger() = default; ~logger() = default;
void set_impl(std::shared_ptr<spdlog::logger> impl);
bool should_log(spdlite::level lvl) const noexcept; bool should_log(spdlite::level lvl) const noexcept;
template<typename... Args> template<typename... Args>
@ -66,25 +68,27 @@ public:
// log string view // log string view
void log(spdlite::level lvl, const string_view_t &sv); void log(spdlite::level lvl, const string_view_t &sv);
// log using printf format
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(spdlite::level::trace, string_view_t(msg)); log(spdlite::level::trace, string_view_t(msg));
} }
template<typename T>
void trace(const 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(spdlite::level::trace, fmt, args...); log(spdlite::level::trace, fmt, args...);
} }
void trace_printf(const char *format, ...);
// //
// debug // debug
// //
@ -93,68 +97,97 @@ public:
log(spdlite::level::debug, string_view_t(msg)); log(spdlite::level::debug, string_view_t(msg));
} }
template<typename T>
void debug(const 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(spdlite::level::debug, fmt, args...); log(spdlite::level::debug, fmt, args...);
} }
void debug_printf(const char *format, ...);
// info // info
void info(const char *msg) void info(const char *msg)
{ {
log(spdlite::level::info, string_view_t(msg)); log(spdlite::level::info, string_view_t(msg));
} }
template<typename T>
void info(const 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(spdlite::level::info, fmt, args...); log(spdlite::level::info, fmt, args...);
} }
void info_printf(const char *format, ...);
// warn // warn
void warn(const char *msg) void warn(const char *msg)
{ {
log(spdlite::level::warn, string_view_t(msg)); log(spdlite::level::warn, string_view_t(msg));
} }
template<typename T>
void warn(const 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(spdlite::level::warn, fmt, args...); log(spdlite::level::warn, fmt, args...);
} }
void warn_printf(const char *format, ...);
// error // error
void error(const char *msg) void error(const char *msg)
{ {
log(spdlite::level::err, string_view_t(msg)); log(spdlite::level::err, string_view_t(msg));
} }
template<typename T>
void error(const 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(spdlite::level::err, fmt, args...); log(spdlite::level::err, fmt, args...);
} }
void error_printf(const char *format, ...);
// critical // critical
void critical(const char *msg) void critical(const char *msg)
{ {
log(spdlite::level::critical, string_view_t(msg)); log(spdlite::level::critical, string_view_t(msg));
} }
template<typename T>
void critical(const 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(spdlite::level::critical, fmt, args...); log(spdlite::level::critical, fmt, args...);
} }
// printf formatting
void log_printf(spdlite::level lvl, const char *format, va_list args);
void trace_printf(const char *format, ...);
void debug_printf(const char *format, ...);
void info_printf(const char *format, ...);
void warn_printf(const char *format, ...);
void error_printf(const char *format, ...);
void critical_printf(const char *format, ...); void critical_printf(const char *format, ...);
// setters/getters // setters/getters
@ -168,55 +201,68 @@ public:
void flush(); void flush();
void flush_on(spdlite::level log_level); void flush_on(spdlite::level log_level);
//clone with new name // clone with new name
spdlite::logger clone(std::string logger_name); spdlite::logger clone(std::string logger_name);
static spdlite::logger &default_logger();
protected: protected:
std::shared_ptr<spdlog::logger> impl_; std::shared_ptr<spdlog::logger> impl_;
void log_formatted_(spdlite::level lvl, const fmt::memory_buffer &formatted); void log_formatted_(spdlite::level lvl, const fmt::memory_buffer &formatted);
}; };
//
// spdlite namespace functions - forward the calls to the default_logger.
//
spdlite::logger &default_logger(); spdlite::logger &default_logger();
template<typename... Args> template<typename... Args>
void trace(const char *fmt, const Args &... args) inline 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) inline void debug(const char *fmt, const Args &... args)
{ {
default_logger().debug(fmt, args...); default_logger().debug(fmt, args...);
} }
template<typename... Args> template<typename... Args>
void info(const char *fmt, const Args &... args) inline void info(const char *fmt, const Args &... args)
{ {
default_logger().info(fmt, args...); default_logger().info(fmt, args...);
} }
template<typename... Args> template<typename... Args>
void warn(const char *fmt, const Args &... args) inline void warn(const char *fmt, const Args &... args)
{ {
default_logger().warn(fmt, args...); default_logger().warn(fmt, args...);
} }
template<typename... Args> template<typename... Args>
void error(const char *fmt, const Args &... args) inline void error(const char *fmt, const Args &... args)
{ {
default_logger().error(fmt, args...); default_logger().error(fmt, args...);
} }
template<typename... Args> template<typename... Args>
void critical(const char *fmt, const Args &... args) inline void critical(const char *fmt, const Args &... args)
{ {
default_logger().critical(fmt, args...); default_logger().critical(fmt, args...);
} }
// user implemented factory to create lite logger void log_printf(spdlite::level lvl, const char *format, va_list args);
// implement it in a seperated and dedicated compilation unit for fast compiles. void trace_printf(const char *format, ...);
logger create_logger(void *ctx = nullptr); void debug_printf(const char *format, ...);
void info_printf(const char *format, ...);
void warn_printf(const char *format, ...);
void error_printf(const char *format, ...);
void critical_printf(const char *format, ...);
} // namespace spdlite } // namespace spdlite
// user implemented factory to create lite logger
// implement it in a seperated and dedicated compilation unit for fast compiles.
spdlite::logger create_logger(void *ctx = nullptr);