support flush_on(..) in async loggers too
This commit is contained in:
		@@ -102,6 +102,10 @@ void async_example()
 | 
			
		||||
    size_t q_size = 4096; //queue size must be power of 2
 | 
			
		||||
    spdlog::set_async_mode(q_size);
 | 
			
		||||
    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)
 | 
			
		||||
        async_file->info("Async message #{}", i);
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -54,7 +54,6 @@ inline spdlog::async_logger::async_logger(const std::string& logger_name,
 | 
			
		||||
 | 
			
		||||
inline void spdlog::async_logger::flush()
 | 
			
		||||
{
 | 
			
		||||
 | 
			
		||||
    _async_log_helper->flush();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@@ -76,6 +75,8 @@ inline void spdlog::async_logger::_sink_it(details::log_msg& msg)
 | 
			
		||||
    try
 | 
			
		||||
    {
 | 
			
		||||
        _async_log_helper->log(msg);
 | 
			
		||||
		if (_should_flush_on(msg))
 | 
			
		||||
			flush();
 | 
			
		||||
    }
 | 
			
		||||
    catch (const std::exception &ex)
 | 
			
		||||
    {
 | 
			
		||||
 
 | 
			
		||||
@@ -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)
 | 
			
		||||
{
 | 
			
		||||
 | 
			
		||||
    _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)
 | 
			
		||||
    if(_should_flush_on(msg))
 | 
			
		||||
        flush();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@@ -282,3 +280,9 @@ inline void spdlog::logger::_default_err_handler(const std::string &msg)
 | 
			
		||||
    sinks::stderr_sink_mt::instance()->log(err_msg);
 | 
			
		||||
    _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);
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -65,6 +65,7 @@ public:
 | 
			
		||||
 | 
			
		||||
    // automatically call flush() if message level >= log_level
 | 
			
		||||
    void flush_on(level::level_enum log_level);
 | 
			
		||||
 | 
			
		||||
    virtual void flush();
 | 
			
		||||
 | 
			
		||||
protected:
 | 
			
		||||
@@ -75,6 +76,9 @@ protected:
 | 
			
		||||
    // 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);
 | 
			
		||||
 | 
			
		||||
	// return true if the given message level should trigger a flush
 | 
			
		||||
	bool _should_flush_on(const details::log_msg&);
 | 
			
		||||
	
 | 
			
		||||
    const std::string _name;
 | 
			
		||||
    std::vector<sink_ptr> _sinks;
 | 
			
		||||
    formatter_ptr _formatter;
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user