From 79468cf6769cca63bed2bb1f74bdbeae1c82da61 Mon Sep 17 00:00:00 2001 From: gabime Date: Sun, 3 Nov 2019 17:00:12 +0200 Subject: [PATCH] Micro-optimze log_it_ --- include/spdlog/logger-inl.h | 6 +++--- include/spdlog/logger.h | 26 +++++++++++++++++--------- 2 files changed, 20 insertions(+), 12 deletions(-) diff --git a/include/spdlog/logger-inl.h b/include/spdlog/logger-inl.h index 4908ede0..84cbefbf 100644 --- a/include/spdlog/logger-inl.h +++ b/include/spdlog/logger-inl.h @@ -172,13 +172,13 @@ 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) +SPDLOG_INLINE void logger::log_it_(const spdlog::details::log_msg &log_msg, bool log_enabled, bool traceback_enabled) {}(const details::log_msg &log_msg, bool log_enabled, bool traceback_enabled) { - if (should_log(log_msg.level)) + if (log_enabled) { sink_it_(log_msg); } - if (tracer_.enabled()) + if (traceback_enabled) { tracer_.push_back(log_msg); } diff --git a/include/spdlog/logger.h b/include/spdlog/logger.h index 96847579..314e5c42 100644 --- a/include/spdlog/logger.h +++ b/include/spdlog/logger.h @@ -76,7 +76,9 @@ public: template void log(source_loc loc, level::level_enum lvl, string_view_t fmt, const Args &... args) { - if (!should_log(lvl) && !tracer_.enabled()) + bool log_enabled = should_log(lvl); + bool traceback_enabled = tracer_.enabled(); + if (!log_enabled && !traceback_enabled) { return; } @@ -85,7 +87,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())); - log_it_(log_msg); + log_it_(log_msg, log_enabled, traceback_enabled); } SPDLOG_LOGGER_CATCH() } @@ -142,13 +144,15 @@ public: template::value, T>::type * = nullptr> void log(source_loc loc, level::level_enum lvl, const T &msg) { - if (!should_log(lvl) && !tracer_.enabled()) + bool log_enabled = should_log(lvl); + bool traceback_enabled = tracer_.enabled(); + if (!log_enabled && !traceback_enabled) { return; } details::log_msg log_msg(loc, name_, lvl, msg); - log_it_(log_msg); + log_it_(log_msg, log_enabled, traceback_enabled); } void log(level::level_enum lvl, string_view_t msg) @@ -209,7 +213,9 @@ public: template void log(source_loc loc, level::level_enum lvl, wstring_view_t fmt, const Args &... args) { - if (!should_log(lvl) && !tracer_.enabled()) + bool log_enabled = should_log(lvl); + bool traceback_enabled = tracer_.enabled(); + if (!log_enabled && !traceback_enabled) { return; } @@ -222,7 +228,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())); - log_it_(log_msg); + log_it_(log_msg, log_enabled, traceback_enabled); } SPDLOG_LOGGER_CATCH() } @@ -273,7 +279,9 @@ public: template::value, T>::type * = nullptr> void log(source_loc loc, level::level_enum lvl, const T &msg) { - if (!should_log(lvl) && !tracer_.enabled()) + bool log_enabled = should_log(lvl); + bool traceback_enabled = tracer_.enabled(); + if (!log_enabled && !traceback_enabled) { return; } @@ -283,7 +291,7 @@ 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())); - log_it_(log_msg); + log_it_(log_msg, log_enabled, traceback_enabled); } SPDLOG_LOGGER_CATCH() } @@ -340,7 +348,7 @@ protected: // 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); + void log_it_(const details::log_msg &log_msg, bool log_enabled, bool traceback_enabled); virtual void sink_it_(const details::log_msg &msg); virtual void flush_(); void dump_backtrace_();