From c7535a91a6b61f94a54a658ff076639028787b76 Mon Sep 17 00:00:00 2001 From: gabime Date: Fri, 29 Mar 2019 14:38:40 +0300 Subject: [PATCH] wip lite --- lite-example/create_logger.cpp | 4 +-- lite-example/example.cpp | 9 ++++-- lite/spdlite.cpp | 10 +++---- lite/spdlite.h | 50 ++++++++++++---------------------- 4 files changed, 30 insertions(+), 43 deletions(-) diff --git a/lite-example/create_logger.cpp b/lite-example/create_logger.cpp index 74892d64..07608139 100644 --- a/lite-example/create_logger.cpp +++ b/lite-example/create_logger.cpp @@ -6,7 +6,7 @@ #define UNUSED(x) (void)(x) // example of creating lite logger with console and file sink -spdlog::lite::logger spdlog::create_lite(void *ctx) +spdlog::lite::logger spdlog::lite::create_logger(void *ctx) { UNUSED(ctx); std::shared_ptr logger_impl; @@ -17,7 +17,7 @@ spdlog::lite::logger spdlog::create_lite(void *ctx) auto file_sink = std::make_shared ("log.txt", true); file_sink ->set_level(spdlog::level::info); - logger_impl = std::make_shared("my-logger", spdlog::sinks_init_list{console_sink, file_sink}); + logger_impl = std::make_unique("my-logger", spdlog::sinks_init_list{console_sink, file_sink}); logger_impl->set_level(spdlog::level::debug); return spdlog::lite::logger(std::move(logger_impl)); } diff --git a/lite-example/example.cpp b/lite-example/example.cpp index 34771028..3668a400 100644 --- a/lite-example/example.cpp +++ b/lite-example/example.cpp @@ -2,15 +2,18 @@ int main() { - auto l = spdlog::create_lite(); + auto l = spdlog::lite::create_logger(); l.set_level(spdlog::lite::level::trace); - l.trace_printf("Hello %s ", "GABI"); + 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"); + auto l2 = l.clone("logger2"); l2.debug("HELLO"); + auto l3 = std::move(l); + l3.warn("HELLO FROM L3"); + } \ No newline at end of file diff --git a/lite/spdlite.cpp b/lite/spdlite.cpp index 71a1aacb..f3557543 100644 --- a/lite/spdlite.cpp +++ b/lite/spdlite.cpp @@ -87,18 +87,18 @@ void spdlog::lite::logger::critical_printf(const char *format, ...) va_end(args); } -void spdlog::lite::logger::set_level(spdlog::lite::level level) +void spdlog::lite::logger::set_level(spdlog::lite::level level) noexcept { auto spd_level = to_spdlog_level(level); impl_->set_level(spd_level); } -spdlog::lite::level spdlog::lite::logger::get_level() const +spdlog::lite::level spdlog::lite::logger::level() const noexcept { return to_lite_level(impl_->level()); } -std::string spdlog::lite::logger::name() const +std::string spdlog::lite::logger::name() const noexcept { return impl_->name(); } @@ -114,13 +114,13 @@ void spdlog::lite::logger::flush_on(spdlog::lite::level level) impl_->flush_on(spd_level); } -spdlog::lite::level spdlog::lite::logger::flush_level() const +spdlog::lite::level spdlog::lite::logger::flush_level() const noexcept { return to_lite_level(impl_->flush_level()); } // pattern -void spdlog::lite::logger::set_pattern(std::string pattern) +void spdlog::lite::logger::set_pattern(std::string pattern) noexcept { impl_->set_pattern(std::move(pattern)); } diff --git a/lite/spdlite.h b/lite/spdlite.h index bb9972ee..636dbdab 100644 --- a/lite/spdlite.h +++ b/lite/spdlite.h @@ -42,7 +42,7 @@ enum class level class logger { public: - explicit logger(std::shared_ptr impl); + explicit logger(std::shared_ptr impl); logger(const logger &) = default; logger(logger &&) = default; logger &operator=(const logger &) = default; @@ -65,6 +65,8 @@ public: // log string view void log(lite::level lvl, const string_view_t &sv); + + // log using printf format void log_printf(lite::level lvl, const char *format, va_list args); // @@ -98,10 +100,8 @@ public: } void debug_printf(const char *format, ...); - - // - // info - // + + // info void info(const char *msg) { log(lite::level::info, string_view_t(msg)); @@ -115,9 +115,7 @@ public: void info_printf(const char *format, ...); - // // warn - // void warn(const char *msg) { log(lite::level::warn, string_view_t(msg)); @@ -131,9 +129,7 @@ public: void warn_printf(const char *format, ...); - // // error - // void error(const char *msg) { log(lite::level::err, string_view_t(msg)); @@ -147,9 +143,7 @@ public: void error_printf(const char *format, ...); - // // critical - // void critical(const char *msg) { log(lite::level::critical, string_view_t(msg)); @@ -163,28 +157,18 @@ public: void critical_printf(const char *format, ...); - // - // setters/getters - // - std::string name() const; - void set_level(lite::level level); - lite::level get_level() const; + // setters/getters + void set_level(lite::level level) noexcept; + void set_pattern(std::string pattern) noexcept; + lite::level level() const noexcept; + std::string name() const noexcept; + lite::level flush_level() const noexcept; - // // flush - // void flush(); void flush_on(lite::level log_level); - lite::level flush_level() const; - - // - // set pattern - // - void set_pattern(std::string pattern); - - // + //clone with new name - // spdlog::lite::logger clone(std::string logger_name); protected: @@ -230,10 +214,10 @@ 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); + } // namespace lite +} // namespace spdlog -// factory to create lite logger -// implement it in a dedicated compilation unit for fast compiles -spdlog::lite::logger create_lite(void *ctx = nullptr); - -} // namespace spdlog \ No newline at end of file