From fcc809f4f161f41390492bacbf36662266ab8fe2 Mon Sep 17 00:00:00 2001 From: gabime Date: Fri, 18 Oct 2019 16:10:30 +0300 Subject: [PATCH] Refactored logger --- include/spdlog/details/backtracer-inl.h | 4 -- include/spdlog/details/backtracer.h | 1 - include/spdlog/logger-inl.h | 14 ++++++- include/spdlog/logger.h | 54 +++++++------------------ 4 files changed, 28 insertions(+), 45 deletions(-) diff --git a/include/spdlog/details/backtracer-inl.h b/include/spdlog/details/backtracer-inl.h index daf1ab33..8b0eb2bf 100644 --- a/include/spdlog/details/backtracer-inl.h +++ b/include/spdlog/details/backtracer-inl.h @@ -48,10 +48,6 @@ SPDLOG_INLINE bool backtracer::enabled() const return enabled_.load(std::memory_order_relaxed); } -SPDLOG_INLINE backtracer::operator bool() const -{ - return enabled(); -} SPDLOG_INLINE void backtracer::push_back(const log_msg &msg) { diff --git a/include/spdlog/details/backtracer.h b/include/spdlog/details/backtracer.h index cf813fe6..713d00e9 100644 --- a/include/spdlog/details/backtracer.h +++ b/include/spdlog/details/backtracer.h @@ -31,7 +31,6 @@ public: void enable(size_t size); void disable(); bool enabled() const; - explicit operator bool() const; void push_back(const log_msg &msg); // pop all items in the q and apply the given fun on each of them. diff --git a/include/spdlog/logger-inl.h b/include/spdlog/logger-inl.h index f7605de5..ebb3ec92 100644 --- a/include/spdlog/logger-inl.h +++ b/include/spdlog/logger-inl.h @@ -172,6 +172,18 @@ SPDLOG_INLINE std::shared_ptr logger::clone(std::string logger_name) } // protected methods +SPDLOG_INLINE void logger::log_it_(const details::log_msg &log_msg) +{ + if (should_log(log_msg.level)) + { + sink_it_(log_msg); + } + if (tracer_.enabled()) + { + tracer_.push_back(log_msg); + } +} + SPDLOG_INLINE void logger::sink_it_(const details::log_msg &msg) { for (auto &sink : sinks_) @@ -207,7 +219,7 @@ SPDLOG_INLINE void logger::flush_() SPDLOG_INLINE void logger::dump_backtrace_() { using details::log_msg; - if (tracer_) + if (tracer_.enabled()) { sink_it_(log_msg{name(), level::info, "****************** Backtrace Start ******************"}); tracer_.foreach_pop([this](const log_msg &msg) { this->sink_it_(msg); }); diff --git a/include/spdlog/logger.h b/include/spdlog/logger.h index 56b982fe..45ba28ec 100644 --- a/include/spdlog/logger.h +++ b/include/spdlog/logger.h @@ -73,11 +73,11 @@ public: void swap(spdlog::logger &other) SPDLOG_NOEXCEPT; + template void log(source_loc loc, level::level_enum lvl, string_view_t fmt, const Args &... args) { - auto level_enabled = should_log(lvl); - if (!level_enabled && !tracer_) + if (!should_log(lvl) && !tracer_.enabled()) { return; } @@ -86,14 +86,7 @@ public: memory_buf_t buf; fmt::format_to(buf, fmt, args...); details::log_msg log_msg(loc, name_, lvl, string_view_t(buf.data(), buf.size())); - if (level_enabled) - { - sink_it_(log_msg); - } - if (tracer_) - { - tracer_.push_back(log_msg); - } + log_it_(log_msg); } SPDLOG_LOGGER_CATCH() } @@ -150,24 +143,14 @@ public: template::value, T>::type * = nullptr> void log(source_loc loc, level::level_enum lvl, const T &msg) { - auto level_enabled = should_log(lvl); - if (!level_enabled && !tracer_) + if (!should_log(lvl) && !tracer_.enabled()) { return; } - SPDLOG_TRY - { - details::log_msg log_msg(loc, name_, lvl, msg); - if (level_enabled) - { - sink_it_(log_msg); - } - if (tracer_) - { - tracer_.push_back(log_msg); - } - } - SPDLOG_LOGGER_CATCH() + + details::log_msg log_msg(loc, name_, lvl, msg); + log_it_(log_msg); + } void log(level::level_enum lvl, string_view_t msg) @@ -228,8 +211,7 @@ public: template void log(source_loc loc, level::level_enum lvl, wstring_view_t fmt, const Args &... args) { - auto level_enabled = should_log(lvl); - if (!level_enabled && !tracer_) + if (!should_log(lvl) && !tracer_.enabled()) { return; } @@ -242,15 +224,7 @@ public: memory_buf_t buf; details::os::wstr_to_utf8buf(wstring_view_t(wbuf.data(), wbuf.size()), buf); details::log_msg log_msg(loc, name_, lvl, string_view_t(buf.data(), buf.size())); - - if (level_enabled) - { - sink_it_(log_msg); - } - if (tracer_) - { - tracer_.push_back(log_msg); - } + log_it_(log_msg); } SPDLOG_LOGGER_CATCH() } @@ -301,7 +275,7 @@ public: template::value, T>::type * = nullptr> void log(source_loc loc, level::level_enum lvl, const T &msg) { - if (!should_log(lvl)) + if (!should_log(lvl) && !tracer_.enabled()) { return; } @@ -310,9 +284,8 @@ public: { memory_buf_t buf; details::os::wstr_to_utf8buf(msg, buf); - details::log_msg log_msg(loc, name_, lvl, string_view_t(buf.data(), buf.size())); - sink_it_(log_msg); + log_it_(log_msg); } SPDLOG_LOGGER_CATCH() } @@ -367,6 +340,9 @@ protected: err_handler custom_err_handler_{nullptr}; details::backtracer tracer_; + // log the given message (if the given log level is high enough), + // and save backtrace (if backtrace is enabled). + void log_it_(const details::log_msg &log_msg); virtual void sink_it_(const details::log_msg &msg); virtual void flush_(); void dump_backtrace_();