diff --git a/lite-example/create_logger.cpp b/lite-example/create_logger.cpp index bc1b1a8e..6d7e7690 100644 --- a/lite-example/create_logger.cpp +++ b/lite-example/create_logger.cpp @@ -2,22 +2,13 @@ #include "spdlog/spdlog.h" #include "spdlog/sinks/basic_file_sink.h" #include "spdlog/sinks/stdout_color_sinks.h" +#include "spdlog/sinks/stdout_sinks.h" #define UNUSED(x) (void)(x) // 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); - std::shared_ptr logger_impl; - - auto console_sink = std::make_shared (); - console_sink->set_level(spdlog::level::debug); - - auto file_sink = std::make_shared ("log.txt", true); - file_sink ->set_level(spdlog::level::info); - - logger_impl = std::make_unique("my-logger", spdlog::sinks_init_list{console_sink, file_sink}); - logger_impl->set_level(spdlog::level::debug); - return spdlite::logger(std::move(logger_impl)); + return spdlite::default_logger(); } diff --git a/lite-example/example.cpp b/lite-example/example.cpp index 5c945ecf..cb3e2446 100644 --- a/lite-example/example.cpp +++ b/lite-example/example.cpp @@ -1,19 +1,12 @@ #include "spdlite.h" int main() -{ - auto l = spdlite::create_logger(); - l.set_level(spdlite::level::trace); - - l.trace_printf("Hello %s ", "GABI"); - l.info_printf("Hello %d", 12346); - l.warn_printf("Hello %f", 12346.5656); - l.warn("Hello {}", "LITE :) "); - - auto l2 = l.clone("logger2"); - l2.debug("HELLO"); - - auto l3 = std::move(l); - l3.warn("HELLO FROM L3"); - +{ + spdlite::default_logger().set_level(spdlite::level::trace); + spdlite::trace_printf("Hello %d", 123); + spdlite::debug_printf("Hello %d", 123); + spdlite::info_printf("Hello %d", 123); + spdlite::warn_printf("Hello %d", 123); + spdlite::error_printf("Hello %d", 123); + spdlite::critical_printf("Hello %d", 123); } \ No newline at end of file diff --git a/lite/spdlite.cpp b/lite/spdlite.cpp index 945e867d..88e5e23d 100644 --- a/lite/spdlite.cpp +++ b/lite/spdlite.cpp @@ -16,6 +16,11 @@ spdlite::logger::logger(std::shared_ptr impl) impl_ = std::move(impl); } +void spdlite::logger::set_impl(std::shared_ptr impl) +{ + impl_ = std::move(impl); +} + bool spdlite::logger::should_log(spdlite::level level) const SPDLOG_NOEXCEPT { 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) { - char buffer[500]; + char buffer[256]; auto size = vsnprintf(buffer, sizeof(buffer), format, args); 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)); } +spdlite::logger &spdlite::logger::default_logger() +{ + static spdlite::logger default_inst_ = spdlite::logger(spdlog::default_logger()); + return default_inst_; +} + spdlite::logger &spdlite::default_logger() { - static spdlite::logger s_default(spdlog::default_logger()); - return s_default; + return spdlite::logger::default_logger(); } + +// 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); +} + diff --git a/lite/spdlite.h b/lite/spdlite.h index da4e589c..cadb1ccb 100644 --- a/lite/spdlite.h +++ b/lite/spdlite.h @@ -42,13 +42,15 @@ enum class level class logger { public: - explicit logger(std::shared_ptr impl); + explicit logger(std::shared_ptr impl); + // logger() = default; //logger with nullptr impl logger(const logger &) = default; logger(logger &&) = default; logger &operator=(const logger &) = default; ~logger() = default; + void set_impl(std::shared_ptr impl); bool should_log(spdlite::level lvl) const noexcept; template @@ -65,26 +67,28 @@ public: // log string view 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 // + void trace(const char *msg) { log(spdlite::level::trace, string_view_t(msg)); } + template + void trace(const T &msg) + { + log(spdlite::level::trace, string_view_t(msg)); + } + template void trace(const char *fmt, const Args &... args) { log(spdlite::level::trace, fmt, args...); } - - void trace_printf(const char *format, ...); - + // // debug // @@ -93,72 +97,101 @@ public: log(spdlite::level::debug, string_view_t(msg)); } + template + void debug(const T &msg) + { + log(spdlite::level::debug, string_view_t(msg)); + } + template void debug(const char *fmt, const Args &... args) { log(spdlite::level::debug, fmt, args...); } - - void debug_printf(const char *format, ...); - - // info + + // info void info(const char *msg) { log(spdlite::level::info, string_view_t(msg)); } + template + void info(const T &msg) + { + log(spdlite::level::info, string_view_t(msg)); + } + template void info(const char *fmt, const Args &... args) { log(spdlite::level::info, fmt, args...); } - void info_printf(const char *format, ...); - // warn void warn(const char *msg) { log(spdlite::level::warn, string_view_t(msg)); } + template + void warn(const T &msg) + { + log(spdlite::level::warn, string_view_t(msg)); + } + template void warn(const char *fmt, const Args &... args) { log(spdlite::level::warn, fmt, args...); } - - void warn_printf(const char *format, ...); - + // error void error(const char *msg) { log(spdlite::level::err, string_view_t(msg)); } + template + void error(const T &msg) + { + log(spdlite::level::err, string_view_t(msg)); + } + template void error(const char *fmt, const Args &... args) { log(spdlite::level::err, fmt, args...); } - void error_printf(const char *format, ...); - // critical void critical(const char *msg) { log(spdlite::level::critical, string_view_t(msg)); } + template + void critical(const T &msg) + { + log(spdlite::level::critical, string_view_t(msg)); + } + template void critical(const char *fmt, const Args &... 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, ...); - - // setters/getters - void set_level(spdlite::level level) noexcept; + + // setters/getters + void set_level(spdlite::level level) noexcept; void set_pattern(std::string pattern) noexcept; spdlite::level level() const noexcept; std::string name() const noexcept; @@ -167,56 +200,69 @@ public: // flush void flush(); void flush_on(spdlite::level log_level); - - //clone with new name + + // clone with new name spdlite::logger clone(std::string logger_name); + static spdlite::logger &default_logger(); + protected: std::shared_ptr impl_; 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(); template -void trace(const char *fmt, const Args &... args) +inline void trace(const char *fmt, const Args &... args) { default_logger().trace(fmt, args...); } template -void debug(const char *fmt, const Args &... args) +inline void debug(const char *fmt, const Args &... args) { default_logger().debug(fmt, args...); } + template -void info(const char *fmt, const Args &... args) +inline void info(const char *fmt, const Args &... args) { default_logger().info(fmt, args...); } template -void warn(const char *fmt, const Args &... args) +inline void warn(const char *fmt, const Args &... args) { default_logger().warn(fmt, args...); } template -void error(const char *fmt, const Args &... args) +inline void error(const char *fmt, const Args &... args) { default_logger().error(fmt, args...); } template -void critical(const char *fmt, const Args &... args) +inline void critical(const char *fmt, const Args &... args) { default_logger().critical(fmt, args...); } -// user implemented factory to create lite logger -// implement it in a seperated and dedicated compilation unit for fast compiles. -logger create_logger(void *ctx = nullptr); +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, ...); } // 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);