diff --git a/include/spdlog/details/backtracer.h b/include/spdlog/details/backtracer.h index bdd151ae..91dfea90 100644 --- a/include/spdlog/details/backtracer.h +++ b/include/spdlog/details/backtracer.h @@ -24,7 +24,7 @@ public: : messages_{n_message} {} - void add_msg(const log_msg &msg) + void add(const log_msg &msg) { std::lock_guard lock{mutex_}; messages_.push_back(log_msg_buffer{msg}); diff --git a/include/spdlog/logger-inl.h b/include/spdlog/logger-inl.h index a482d17e..c86ba426 100644 --- a/include/spdlog/logger-inl.h +++ b/include/spdlog/logger-inl.h @@ -64,26 +64,6 @@ SPDLOG_INLINE void swap(logger &a, logger &b) a.swap(b); } -SPDLOG_INLINE void logger::log(source_loc loc, level::level_enum lvl, string_view_t msg) -{ - if (tracer_) - { - save_backtrace_(details::log_msg(loc, string_view_t(name_), lvl, msg)); - } - - if (!should_log(lvl)) - { - return; - } - - details::log_msg log_msg(loc, string_view_t(name_), lvl, msg); - sink_it_(log_msg); -} - -SPDLOG_INLINE void logger::log(level::level_enum lvl, string_view_t msg) -{ - log(source_loc{}, lvl, msg); -} SPDLOG_INLINE bool logger::should_log(level::level_enum msg_level) const { @@ -220,9 +200,9 @@ SPDLOG_INLINE void logger::flush_() } } -SPDLOG_INLINE void logger::save_backtrace_(const details::log_msg &msg) +SPDLOG_INLINE void logger::backtrace_add_(const details::log_msg &msg) { - tracer_->add_msg(msg); + tracer_->add(msg); } SPDLOG_INLINE void logger::dump_backtrace_() diff --git a/include/spdlog/logger.h b/include/spdlog/logger.h index 4c686ba8..09815447 100644 --- a/include/spdlog/logger.h +++ b/include/spdlog/logger.h @@ -78,40 +78,30 @@ public: void swap(spdlog::logger &other) SPDLOG_NOEXCEPT; template - void force_log(source_loc loc, level::level_enum lvl, string_view_t fmt, const Args &... args) + 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_) + { + return; + } SPDLOG_TRY { fmt::memory_buffer buf; fmt::format_to(buf, fmt, args...); details::log_msg log_msg(loc, name_, lvl, string_view_t(buf.data(), buf.size())); - sink_it_(log_msg); - if (tracer_) - { - save_backtrace_(log_msg); - } + if (level_enabled) sink_it_(log_msg); + if (tracer_) backtrace_add_(log_msg); } SPDLOG_LOGGER_CATCH() } - template - void log(source_loc loc, level::level_enum lvl, string_view_t fmt, const Args &... args) - { - if (should_log(lvl)) - { - force_log(loc, lvl, fmt, args...); - } - } - template void log(level::level_enum lvl, string_view_t fmt, const Args &... args) { log(source_loc{}, lvl, fmt, args...); } - void log(source_loc loc, level::level_enum lvl, const string_view_t msg); - void log(level::level_enum lvl, string_view_t msg); - template void trace(string_view_t fmt, const Args &... args) { @@ -158,17 +148,23 @@ public: template::value, T>::type * = nullptr> void log(source_loc loc, level::level_enum lvl, const T &msg) { - if (tracer_) - { - save_backtrace_(details::log_msg(loc, name_, lvl, msg)); - } - if (!should_log(lvl)) + auto level_enabled = should_log(lvl); + if(!level_enabled && !tracer_) { return; } + SPDLOG_TRY + { + details::log_msg log_msg(loc, name_, lvl, msg); + if (level_enabled) sink_it_(log_msg); + if (tracer_) backtrace_add_(log_msg); + } + SPDLOG_LOGGER_CATCH() + } - details::log_msg log_msg(loc, name_, lvl, msg); - sink_it_(log_msg); + void log(level::level_enum lvl, string_view_t msg) + { + log(source_loc{}, lvl, msg); } // T cannot be statically converted to string_view or wstring_view @@ -177,25 +173,7 @@ public: T>::type * = nullptr> void log(source_loc loc, level::level_enum lvl, const T &msg) { - if (tracer_) - { - fmt::memory_buffer buf; - fmt::format_to(buf, "{}", msg); - save_backtrace_(details::log_msg(loc, name_, lvl, string_view_t(buf.data(), buf.size()))); - } - - if (!should_log(lvl)) - { - return; - } - SPDLOG_TRY - { - fmt::memory_buffer buf; - fmt::format_to(buf, "{}", msg); - details::log_msg log_msg(loc, name_, lvl, string_view_t(buf.data(), buf.size())); - sink_it_(log_msg); - } - SPDLOG_LOGGER_CATCH() + log(loc, lvl, "{}", msg); } template @@ -376,7 +354,7 @@ protected: virtual void sink_it_(const details::log_msg &msg); virtual void flush_(); - void save_backtrace_(const details::log_msg &msg); + void backtrace_add_(const details::log_msg &msg); void dump_backtrace_(); bool should_flush_(const details::log_msg &msg); diff --git a/include/spdlog/spdlog.h b/include/spdlog/spdlog.h index 4563c900..cf20dba8 100644 --- a/include/spdlog/spdlog.h +++ b/include/spdlog/spdlog.h @@ -285,12 +285,8 @@ inline void critical(wstring_view_t fmt, const Args &... args) // SPDLOG_LEVEL_OFF // -#define SPDLOG_LOGGER_CALL(logger, level, ...) \ - do \ - { \ - if (logger->should_log(level)) \ - logger->force_log(spdlog::source_loc{__FILE__, __LINE__, SPDLOG_FUNCTION}, level, __VA_ARGS__); \ - } while (0) +#define SPDLOG_LOGGER_CALL(logger, level, ...)\ + logger->log(spdlog::source_loc{__FILE__, __LINE__, SPDLOG_FUNCTION}, level, __VA_ARGS__); \ #if SPDLOG_ACTIVE_LEVEL <= SPDLOG_LEVEL_TRACE #define SPDLOG_LOGGER_TRACE(logger, ...) SPDLOG_LOGGER_CALL(logger, spdlog::level::trace, __VA_ARGS__)