diff --git a/include/spdlog/details/logger_impl.h b/include/spdlog/details/logger_impl.h index 428cd189..9f2f13d7 100644 --- a/include/spdlog/details/logger_impl.h +++ b/include/spdlog/details/logger_impl.h @@ -21,6 +21,7 @@ inline spdlog::logger::logger(const std::string& logger_name, const It& begin, c // no support under vs2013 for member initialization for std::atomic _level = level::info; + _flush_level = level::off; } // ctor with sinks as init list @@ -266,6 +267,11 @@ inline void spdlog::logger::set_level(spdlog::level::level_enum log_level) _level.store(log_level); } +inline void spdlog::logger::flush_on(level::level_enum log_level) +{ + _flush_level.store(log_level); +} + inline spdlog::level::level_enum spdlog::logger::level() const { return static_cast(_level.load(std::memory_order_relaxed)); @@ -284,6 +290,10 @@ inline void spdlog::logger::_log_msg(details::log_msg& msg) _formatter->format(msg); for (auto &sink : _sinks) sink->log(msg); + + const auto flush_level = _flush_level.load(std::memory_order_relaxed); + if (msg.level >= flush_level) + flush(); } inline void spdlog::logger::_set_pattern(const std::string& pattern) diff --git a/include/spdlog/logger.h b/include/spdlog/logger.h index 41d51fbf..ca94e554 100644 --- a/include/spdlog/logger.h +++ b/include/spdlog/logger.h @@ -41,6 +41,9 @@ public: const std::string& name() const; bool should_log(level::level_enum) const; + // automatically call flush() after a message of level log_level or higher is emitted + void flush_on(level::level_enum log_level); + // logger.info(cppformat_string, arg1, arg2, arg3, ...) call style template details::line_logger trace(const char* fmt, const Args&... args); template details::line_logger debug(const char* fmt, const Args&... args); @@ -104,6 +107,7 @@ protected: std::vector _sinks; formatter_ptr _formatter; spdlog::level_t _level; + spdlog::level_t _flush_level; }; }