Added async_nonblocking factory
This commit is contained in:
		@@ -21,9 +21,11 @@
 | 
				
			|||||||
#include <memory>
 | 
					#include <memory>
 | 
				
			||||||
namespace spdlog {
 | 
					namespace spdlog {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// async logger factory - creates async loggers backed with thread pool.
 | 
					// async logger factory - creates async loggers backed with thread pool.
 | 
				
			||||||
// if a global thread pool doesn't already exist, create it with default queue size of 8192 items and single thread.
 | 
					// if a global thread pool doesn't already exist, create it with default queue size of 8192 items and single thread.
 | 
				
			||||||
struct async_factory
 | 
					template<async_overflow_policy OverflowPolicy = async_overflow_policy::block>
 | 
				
			||||||
 | 
					struct async_factory_impl
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
    template<typename Sink, typename... SinkArgs>
 | 
					    template<typename Sink, typename... SinkArgs>
 | 
				
			||||||
    static std::shared_ptr<async_logger> create(const std::string &logger_name, SinkArgs &&... args)
 | 
					    static std::shared_ptr<async_logger> create(const std::string &logger_name, SinkArgs &&... args)
 | 
				
			||||||
@@ -39,18 +41,29 @@ struct async_factory
 | 
				
			|||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        auto sink = std::make_shared<Sink>(std::forward<SinkArgs>(args)...);
 | 
					        auto sink = std::make_shared<Sink>(std::forward<SinkArgs>(args)...);
 | 
				
			||||||
        auto new_logger = std::make_shared<async_logger>(logger_name, std::move(sink), std::move(tp), async_overflow_policy::block);
 | 
					        auto new_logger = std::make_shared<async_logger>(logger_name, std::move(sink), std::move(tp), OverflowPolicy);
 | 
				
			||||||
        registry::instance().register_and_init(new_logger);
 | 
					        registry::instance().register_and_init(new_logger);
 | 
				
			||||||
        return new_logger;
 | 
					        return new_logger;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					using async_factory = async_factory_impl<async_overflow_policy::block>;
 | 
				
			||||||
 | 
					using async_factory_nonblock = async_factory_impl < async_overflow_policy::overrun_oldest>;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
template<typename Sink, typename... SinkArgs>
 | 
					template<typename Sink, typename... SinkArgs>
 | 
				
			||||||
inline std::shared_ptr<spdlog::logger> create_async(const std::string &logger_name, SinkArgs &&... sink_args)
 | 
					inline std::shared_ptr<spdlog::logger> create_async(const std::string &logger_name, SinkArgs &&... sink_args)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
    return async_factory::create<Sink>(logger_name, std::forward<SinkArgs>(sink_args)...);
 | 
					    return async_factory::create<Sink>(logger_name, std::forward<SinkArgs>(sink_args)...);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					template<typename Sink, typename... SinkArgs>
 | 
				
			||||||
 | 
					inline std::shared_ptr<spdlog::logger> create_async_nb(const std::string &logger_name, SinkArgs &&... sink_args)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    return async_factory<spdlog::async_overflow_policy::overrun_oldest>::create<Sink>(logger_name, std::forward<SinkArgs>(sink_args)...);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					    
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// set global thread pool.
 | 
					// set global thread pool.
 | 
				
			||||||
inline void init_thread_pool(size_t q_size, size_t thread_count)
 | 
					inline void init_thread_pool(size_t q_size, size_t thread_count)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -23,6 +23,14 @@
 | 
				
			|||||||
#include <string>
 | 
					#include <string>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
namespace spdlog {
 | 
					namespace spdlog {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// Async overflow policy - block by default.
 | 
				
			||||||
 | 
					enum class async_overflow_policy
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    block,         // Block until message can be enqueued
 | 
				
			||||||
 | 
					    overrun_oldest // Discard oldest message in the queue if full when trying to add new item.
 | 
				
			||||||
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
namespace details {
 | 
					namespace details {
 | 
				
			||||||
class thread_pool;
 | 
					class thread_pool;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -121,14 +121,7 @@ inline spdlog::level::level_enum from_str(const std::string &name)
 | 
				
			|||||||
using level_hasher = std::hash<int>;
 | 
					using level_hasher = std::hash<int>;
 | 
				
			||||||
} // namespace level
 | 
					} // namespace level
 | 
				
			||||||
 | 
					
 | 
				
			||||||
//
 | 
					
 | 
				
			||||||
// Async overflow policy - block by default.
 | 
					 | 
				
			||||||
//
 | 
					 | 
				
			||||||
enum class async_overflow_policy
 | 
					 | 
				
			||||||
{
 | 
					 | 
				
			||||||
    block,         // Block until message can be enqueued
 | 
					 | 
				
			||||||
    overrun_oldest // Discard oldest message in the queue if full when trying to add new item.
 | 
					 | 
				
			||||||
};
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
//
 | 
					//
 | 
				
			||||||
// Pattern time - specific time getting to use for pattern_formatter.
 | 
					// Pattern time - specific time getting to use for pattern_formatter.
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user