diff --git a/include/spdlog/async_logger-inl.h b/include/spdlog/async_logger-inl.h index cfeda2de..3ca5c256 100644 --- a/include/spdlog/async_logger-inl.h +++ b/include/spdlog/async_logger-inl.h @@ -56,7 +56,7 @@ SPDLOG_INLINE void spdlog::async_logger::backend_sink_it_(const details::log_msg { for (auto &sink : sinks_) { - if (sink->should_log(msg.level)) + if (msg.level.forced || sink->should_log(msg.level.value)) { SPDLOG_TRY { diff --git a/include/spdlog/common.h b/include/spdlog/common.h index 13fbe970..8866adcd 100644 --- a/include/spdlog/common.h +++ b/include/spdlog/common.h @@ -149,6 +149,15 @@ enum level_enum off = SPDLOG_LEVEL_OFF, }; +struct forceable { + level_enum value{level_enum::off}; + bool forced{false}; + + forceable() = default; + explicit forceable(level_enum level) : value{level} {} + explicit forceable(level_enum level, bool forced) : value{level}, forced{forced} {} +}; + #if !defined(SPDLOG_LEVEL_NAMES) #define SPDLOG_LEVEL_NAMES \ { \ diff --git a/include/spdlog/details/log_msg-inl.h b/include/spdlog/details/log_msg-inl.h index cb130532..ffec3a78 100644 --- a/include/spdlog/details/log_msg-inl.h +++ b/include/spdlog/details/log_msg-inl.h @@ -12,7 +12,7 @@ namespace spdlog { namespace details { -SPDLOG_INLINE log_msg::log_msg(spdlog::source_loc loc, string_view_t logger_name, spdlog::level::level_enum lvl, spdlog::string_view_t msg) +SPDLOG_INLINE log_msg::log_msg(spdlog::source_loc loc, string_view_t logger_name, spdlog::level::forceable lvl, spdlog::string_view_t msg) : logger_name(logger_name) , level(lvl) #ifndef SPDLOG_NO_DATETIME @@ -26,7 +26,7 @@ SPDLOG_INLINE log_msg::log_msg(spdlog::source_loc loc, string_view_t logger_name , payload(msg) {} -SPDLOG_INLINE log_msg::log_msg(string_view_t logger_name, spdlog::level::level_enum lvl, spdlog::string_view_t msg) +SPDLOG_INLINE log_msg::log_msg(string_view_t logger_name, spdlog::level::forceable lvl, spdlog::string_view_t msg) : log_msg(source_loc{}, logger_name, lvl, msg) {} diff --git a/include/spdlog/details/log_msg.h b/include/spdlog/details/log_msg.h index ccd41ecb..e43b8f23 100644 --- a/include/spdlog/details/log_msg.h +++ b/include/spdlog/details/log_msg.h @@ -11,12 +11,12 @@ namespace details { struct log_msg { log_msg() = default; - log_msg(source_loc loc, string_view_t logger_name, level::level_enum lvl, string_view_t msg); - log_msg(string_view_t logger_name, level::level_enum lvl, string_view_t msg); + log_msg(source_loc loc, string_view_t logger_name, level::forceable lvl, string_view_t msg); + log_msg(string_view_t logger_name, level::forceable lvl, string_view_t msg); log_msg(const log_msg &other) = default; string_view_t logger_name; - level::level_enum level{level::off}; + level::forceable level{level::off, false}; log_clock::time_point time; size_t thread_id{0}; diff --git a/include/spdlog/details/pattern_formatter-inl.h b/include/spdlog/details/pattern_formatter-inl.h index 1b95eb6d..7fd8359b 100644 --- a/include/spdlog/details/pattern_formatter-inl.h +++ b/include/spdlog/details/pattern_formatter-inl.h @@ -114,7 +114,7 @@ public: void format(const details::log_msg &msg, const std::tm &, memory_buf_t &dest) override { - string_view_t &level_name = level::to_string_view(msg.level); + string_view_t &level_name = level::to_string_view(msg.level.value); ScopedPadder p(level_name.size(), padinfo_, dest); fmt_helper::append_string_view(level_name, dest); } @@ -131,7 +131,7 @@ public: void format(const details::log_msg &msg, const std::tm &, memory_buf_t &dest) override { - string_view_t level_name{level::to_short_c_str(msg.level)}; + string_view_t level_name{level::to_short_c_str(msg.level.value)}; ScopedPadder p(level_name.size(), padinfo_, dest); fmt_helper::append_string_view(level_name, dest); } @@ -959,7 +959,7 @@ public: // wrap the level name with color msg.color_range_start = dest.size(); // fmt_helper::append_string_view(level::to_c_str(msg.level), dest); - fmt_helper::append_string_view(level::to_string_view(msg.level), dest); + fmt_helper::append_string_view(level::to_string_view(msg.level.value), dest); msg.color_range_end = dest.size(); dest.push_back(']'); dest.push_back(' '); diff --git a/include/spdlog/logger-inl.h b/include/spdlog/logger-inl.h index 05c57e8e..fa87ec31 100644 --- a/include/spdlog/logger-inl.h +++ b/include/spdlog/logger-inl.h @@ -171,7 +171,7 @@ SPDLOG_INLINE void logger::sink_it_(const details::log_msg &msg) { for (auto &sink : sinks_) { - if (sink->should_log(msg.level)) + if (msg.level.forced || sink->should_log(msg.level.value)) { SPDLOG_TRY { @@ -204,16 +204,16 @@ SPDLOG_INLINE void logger::dump_backtrace_() using details::log_msg; if (tracer_) { - sink_it_(log_msg{name(), level::info, "****************** Backtrace Start ******************"}); + sink_it_(log_msg{name(), level::forceable{level::info, false}, "****************** Backtrace Start ******************"}); tracer_.foreach_pop([this](const log_msg &msg) { this->sink_it_(msg); }); - sink_it_(log_msg{name(), level::info, "****************** Backtrace End ********************"}); + sink_it_(log_msg{name(), level::forceable{level::info, false}, "****************** Backtrace End ********************"}); } } SPDLOG_INLINE bool logger::should_flush_(const details::log_msg &msg) { auto flush_level = flush_level_.load(std::memory_order_relaxed); - return (msg.level >= flush_level) && (msg.level != level::off); + return (msg.level.value >= flush_level) && (msg.level.value != level::off); } SPDLOG_INLINE void logger::err_handler_(const std::string &msg) diff --git a/include/spdlog/logger.h b/include/spdlog/logger.h index 0e4eaa00..08e68a74 100644 --- a/include/spdlog/logger.h +++ b/include/spdlog/logger.h @@ -74,30 +74,30 @@ 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_) - { - return; - } - SPDLOG_TRY - { - 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); - } - } - SPDLOG_LOGGER_CATCH() + void log(source_loc loc, level::level_enum lvl, string_view_t fmt, const Args &... args) { + log(loc, level::forceable{lvl, false}, fmt, args...); } + template + void log(source_loc loc, level::forceable lvl, string_view_t fmt, const Args &... args) { + auto level_enabled = lvl.forced || should_log(lvl.value); + if (!level_enabled && !tracer_) { + return; + } + SPDLOG_TRY { + 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); + } + } + SPDLOG_LOGGER_CATCH() + } + template void log(level::level_enum lvl, string_view_t fmt, const Args &... args) { @@ -148,28 +148,29 @@ public: // T can be statically converted to string_view 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_) - { - 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() + void log(source_loc loc, level::level_enum lvl, const T &msg) { + log(loc, level::forceable{lvl, false}, msg); } + // T can be statically converted to string_view + template::value, T>::type * = nullptr> + void log(source_loc loc, level::forceable lvl, const T &msg) { + auto level_enabled = lvl.forced || should_log(lvl.value); + 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_) { + tracer_.push_back(log_msg); + } + } + SPDLOG_LOGGER_CATCH() + } + void log(level::level_enum lvl, string_view_t msg) { log(source_loc{}, lvl, msg); @@ -228,7 +229,13 @@ public: template void log(source_loc loc, level::level_enum lvl, wstring_view_t fmt, const Args &... args) { - auto level_enabled = should_log(lvl); + log(loc, level::forceable{lvl, false}, fmt, args...); + } + + template + void log(source_loc loc, level::forceable lvl, wstring_view_t fmt, const Args &... args) + { + auto level_enabled = lvl.forced || should_log(lvl.level); if (!level_enabled && !tracer_) { return; diff --git a/include/spdlog/sinks/ansicolor_sink-inl.h b/include/spdlog/sinks/ansicolor_sink-inl.h index 280ad7cd..93c87844 100644 --- a/include/spdlog/sinks/ansicolor_sink-inl.h +++ b/include/spdlog/sinks/ansicolor_sink-inl.h @@ -51,7 +51,7 @@ SPDLOG_INLINE void ansicolor_sink::log(const details::log_msg &msg // before color range print_range_(formatted, 0, msg.color_range_start); // in color range - print_ccode_(colors_[msg.level]); + print_ccode_(colors_[msg.level.value]); print_range_(formatted, msg.color_range_start, msg.color_range_end); print_ccode_(reset); // after color range diff --git a/include/spdlog/sinks/dist_sink.h b/include/spdlog/sinks/dist_sink.h index 8f931cf7..efb03ea0 100644 --- a/include/spdlog/sinks/dist_sink.h +++ b/include/spdlog/sinks/dist_sink.h @@ -55,7 +55,7 @@ protected: { for (auto &sink : sinks_) { - if (sink->should_log(msg.level)) + if (msg.level.forced || sink->should_log(msg.level.value)) { sink->log(msg); } diff --git a/include/spdlog/sinks/syslog_sink.h b/include/spdlog/sinks/syslog_sink.h index 697ba241..3623b0fd 100644 --- a/include/spdlog/sinks/syslog_sink.h +++ b/include/spdlog/sinks/syslog_sink.h @@ -83,7 +83,7 @@ private: // int syslog_prio_from_level(const details::log_msg &msg) const { - return syslog_levels_.at(static_cast(msg.level)); + return syslog_levels_.at(static_cast(msg.level.value)); } };