diff --git a/include/dwarfs/logger.h b/include/dwarfs/logger.h index 0af21d75..265bb057 100644 --- a/include/dwarfs/logger.h +++ b/include/dwarfs/logger.h @@ -64,6 +64,7 @@ class logger { virtual void write(level_type level, std::string_view output, std::source_location loc) = 0; + virtual level_type threshold() const = 0; std::string_view policy_name() const { return policy_name_; } @@ -100,6 +101,7 @@ class stream_logger : public logger { void write(level_type level, std::string_view output, std::source_location loc) override; + level_type threshold() const override; void set_threshold(level_type threshold); void set_with_context(bool with_context) { with_context_ = with_context; } @@ -130,6 +132,7 @@ class null_logger : public logger { null_logger(); void write(level_type, std::string_view, std::source_location) override {} + level_type threshold() const override { return FATAL; } }; class level_log_entry { @@ -249,12 +252,17 @@ template class log_proxy { public: log_proxy(logger& lgr) - : lgr_(lgr) {} + : lgr_(lgr) + , threshold_(lgr.threshold()) {} - static constexpr bool is_enabled_for(logger::level_type level) { + static constexpr bool policy_is_enabled_for(logger::level_type level) { return LogPolicy::is_enabled_for(level); } + bool logger_is_enabled_for(logger::level_type level) const { + return level <= threshold_; + } + auto fatal(std::source_location loc) const { return level_log_entry(lgr_, logger::FATAL, loc); } @@ -353,11 +361,13 @@ class log_proxy { private: logger& lgr_; + logger::level_type threshold_; }; #define LOG_DETAIL_LEVEL(level, lgr, method) \ - if constexpr (std::decay_t::is_enabled_for( \ + if constexpr (std::decay_t::policy_is_enabled_for( \ ::dwarfs::logger::level)) \ + if (lgr.logger_is_enabled_for(::dwarfs::logger::level)) \ lgr.method(std::source_location::current()) #define LOG_PROXY(policy, lgr) ::dwarfs::log_proxy log_(lgr) diff --git a/src/logger.cpp b/src/logger.cpp index 4220ced7..eb5c7c69 100644 --- a/src/logger.cpp +++ b/src/logger.cpp @@ -158,6 +158,10 @@ void stream_logger::write_nolock(std::string_view output) { } } +logger::level_type stream_logger::threshold() const { + return threshold_.load(); +} + void stream_logger::write(level_type level, std::string_view output, std::source_location loc) { if (level <= threshold_ || level == FATAL) { diff --git a/test/test_logger.h b/test/test_logger.h index 22098864..5884a9be 100644 --- a/test/test_logger.h +++ b/test/test_logger.h @@ -59,6 +59,8 @@ class test_logger : public ::dwarfs::logger { } } + level_type threshold() const override { return threshold_; } + void write(level_type level, std::string_view output, std::source_location loc) override { if (output_ && level <= output_threshold_) {