spdlog/include/spdlog/async.h

85 lines
3.0 KiB
C
Raw Normal View History

2019-06-03 17:09:16 -04:00
// Copyright(c) 2015-present, Gabi Melman & spdlog contributors.
2018-04-13 21:11:03 -04:00
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
#pragma once
//
2018-04-28 17:52:56 -04:00
// Async logging using global thread pool
// All loggers created here share same global thread pool.
2018-07-21 16:48:07 -04:00
// Each log message is pushed to a queue along withe a shared pointer to the
// logger.
// If a logger deleted while having pending messages in the queue, it's actual
// destruction will defer
2018-04-28 18:36:45 -04:00
// until all its messages are processed by the thread pool.
2018-07-21 16:48:07 -04:00
// This is because each message in the queue holds a shared_ptr to the
// originating logger.
2018-04-13 21:11:03 -04:00
2018-04-28 18:31:09 -04:00
#include "spdlog/async_logger.h"
#include "spdlog/details/registry.h"
#include "spdlog/details/thread_pool.h"
2018-04-13 21:11:03 -04:00
#include <memory>
2018-07-26 14:09:40 -04:00
#include <mutex>
2018-04-13 21:11:03 -04:00
namespace spdlog {
namespace details {
static const size_t default_async_q_size = 8192;
}
2018-07-20 16:20:48 -04:00
2018-04-19 18:50:09 -04:00
// async logger factory - creates async loggers backed with thread pool.
2018-07-21 16:48:07 -04:00
// if a global thread pool doesn't already exist, create it with default queue
// size of 8192 items and single thread.
2018-07-20 16:20:48 -04:00
template<async_overflow_policy OverflowPolicy = async_overflow_policy::block>
struct async_factory_impl
2018-04-13 21:11:03 -04:00
{
template<typename Sink, typename... SinkArgs>
2018-10-16 07:39:29 -04:00
static std::shared_ptr<async_logger> create(std::string logger_name, SinkArgs &&... args)
2018-04-13 21:11:03 -04:00
{
2018-07-26 14:09:40 -04:00
auto &registry_inst = details::registry::instance();
2018-07-26 14:13:19 -04:00
// create global thread pool if not already exists..
std::lock_guard<std::recursive_mutex> tp_lock(registry_inst.tp_mutex());
2018-07-26 14:09:40 -04:00
auto tp = registry_inst.get_tp();
if (tp == nullptr)
{
tp = std::make_shared<details::thread_pool>(details::default_async_q_size, 1);
registry_inst.set_tp(tp);
}
2018-07-24 15:59:34 -04:00
2018-07-26 14:09:40 -04:00
auto sink = std::make_shared<Sink>(std::forward<SinkArgs>(args)...);
2018-10-16 07:39:29 -04:00
auto new_logger = std::make_shared<async_logger>(std::move(logger_name), std::move(sink), std::move(tp), OverflowPolicy);
registry_inst.initialize_logger(new_logger);
2018-04-13 21:11:03 -04:00
return new_logger;
2018-04-20 06:20:19 -04:00
}
2018-04-13 21:11:03 -04:00
};
2018-07-20 16:20:48 -04:00
using async_factory = async_factory_impl<async_overflow_policy::block>;
2018-07-21 16:48:07 -04:00
using async_factory_nonblock = async_factory_impl<async_overflow_policy::overrun_oldest>;
2018-07-20 16:20:48 -04:00
2018-04-13 21:11:03 -04:00
template<typename Sink, typename... SinkArgs>
2018-10-16 07:39:29 -04:00
inline std::shared_ptr<spdlog::logger> create_async(std::string logger_name, SinkArgs &&... sink_args)
2018-04-13 21:11:03 -04:00
{
2018-10-16 07:39:29 -04:00
return async_factory::create<Sink>(std::move(logger_name), std::forward<SinkArgs>(sink_args)...);
2018-04-13 21:11:03 -04:00
}
2018-07-20 16:20:48 -04:00
template<typename Sink, typename... SinkArgs>
2018-10-16 07:39:29 -04:00
inline std::shared_ptr<spdlog::logger> create_async_nb(std::string logger_name, SinkArgs &&... sink_args)
2018-07-20 16:20:48 -04:00
{
2018-10-16 07:39:29 -04:00
return async_factory_nonblock::create<Sink>(std::move(logger_name), std::forward<SinkArgs>(sink_args)...);
2018-07-20 16:20:48 -04:00
}
2018-05-25 11:28:29 -04:00
// set global thread pool.
2018-04-13 21:11:03 -04:00
inline void init_thread_pool(size_t q_size, size_t thread_count)
2018-07-24 17:06:10 -04:00
{
2018-07-24 15:59:34 -04:00
auto tp = std::make_shared<details::thread_pool>(q_size, thread_count);
2018-07-24 17:06:10 -04:00
details::registry::instance().set_tp(std::move(tp));
2018-04-13 21:11:03 -04:00
}
2018-05-26 20:14:55 -04:00
// get the global thread pool.
inline std::shared_ptr<spdlog::details::thread_pool> thread_pool()
{
2018-07-24 15:59:34 -04:00
return details::registry::instance().get_tp();
2018-05-26 20:14:55 -04:00
}
2018-04-13 21:11:03 -04:00
} // namespace spdlog