support flush_on(..) in async loggers too

This commit is contained in:
gabime 2016-09-02 16:19:29 +03:00
parent 3a12f3c560
commit 1df30a0733
4 changed files with 475 additions and 462 deletions

View File

@ -102,6 +102,10 @@ void async_example()
size_t q_size = 4096; //queue size must be power of 2 size_t q_size = 4096; //queue size must be power of 2
spdlog::set_async_mode(q_size); spdlog::set_async_mode(q_size);
auto async_file = spd::daily_logger_st("async_file_logger", "logs/async_log.txt"); auto async_file = spd::daily_logger_st("async_file_logger", "logs/async_log.txt");
// auto flush if the log severity is error or higher
async_file->flush_on(spd::level::err);
for (int i = 0; i < 100; ++i) for (int i = 0; i < 100; ++i)
async_file->info("Async message #{}", i); async_file->info("Async message #{}", i);
} }

View File

@ -54,7 +54,6 @@ inline spdlog::async_logger::async_logger(const std::string& logger_name,
inline void spdlog::async_logger::flush() inline void spdlog::async_logger::flush()
{ {
_async_log_helper->flush(); _async_log_helper->flush();
} }
@ -76,6 +75,8 @@ inline void spdlog::async_logger::_sink_it(details::log_msg& msg)
try try
{ {
_async_log_helper->log(msg); _async_log_helper->log(msg);
if (_should_flush_on(msg))
flush();
} }
catch (const std::exception &ex) catch (const std::exception &ex)
{ {

View File

@ -244,13 +244,11 @@ inline bool spdlog::logger::should_log(spdlog::level::level_enum msg_level) cons
// //
inline void spdlog::logger::_sink_it(details::log_msg& msg) inline void spdlog::logger::_sink_it(details::log_msg& msg)
{ {
_formatter->format(msg); _formatter->format(msg);
for (auto &sink : _sinks) for (auto &sink : _sinks)
sink->log(msg); sink->log(msg);
const auto flush_level = _flush_level.load(std::memory_order_relaxed); if(_should_flush_on(msg))
if (msg.level >= flush_level)
flush(); flush();
} }
@ -282,3 +280,9 @@ inline void spdlog::logger::_default_err_handler(const std::string &msg)
sinks::stderr_sink_mt::instance()->log(err_msg); sinks::stderr_sink_mt::instance()->log(err_msg);
_last_err_time = now; _last_err_time = now;
} }
inline bool spdlog::logger::_should_flush_on(const details::log_msg &msg)
{
const auto flush_level = _flush_level.load(std::memory_order_relaxed);
return (msg.level >= flush_level) && (msg.level != level::off);
}

View File

@ -65,6 +65,7 @@ public:
// automatically call flush() if message level >= log_level // automatically call flush() if message level >= log_level
void flush_on(level::level_enum log_level); void flush_on(level::level_enum log_level);
virtual void flush(); virtual void flush();
protected: protected:
@ -75,6 +76,9 @@ protected:
// default error handler: print the error to stderr with the max rate of 1 message/minute // default error handler: print the error to stderr with the max rate of 1 message/minute
virtual void _default_err_handler(const std::string &msg); virtual void _default_err_handler(const std::string &msg);
// return true if the given message level should trigger a flush
bool _should_flush_on(const details::log_msg&);
const std::string _name; const std::string _name;
std::vector<sink_ptr> _sinks; std::vector<sink_ptr> _sinks;
formatter_ptr _formatter; formatter_ptr _formatter;