async.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. // Copyright(c) 2015-present, Gabi Melman & spdlog contributors.
  2. // Distributed under the MIT License (http://opensource.org/licenses/MIT)
  3. #pragma once
  4. //
  5. // Async logging using global thread pool
  6. // All loggers created here share same global thread pool.
  7. // Each log message is pushed to a queue along with a shared pointer to the
  8. // logger.
  9. // If a logger deleted while having pending messages in the queue, it's actual
  10. // destruction will defer
  11. // until all its messages are processed by the thread pool.
  12. // This is because each message in the queue holds a shared_ptr to the
  13. // originating logger.
  14. #include <spdlog/async_logger.h>
  15. #include <spdlog/details/registry.h>
  16. #include <spdlog/details/thread_pool.h>
  17. #include <functional>
  18. #include <memory>
  19. #include <mutex>
  20. namespace spdlog {
  21. namespace details {
  22. static const size_t default_async_q_size = 8192;
  23. }
  24. // async logger factory - creates async loggers backed with thread pool.
  25. // if a global thread pool doesn't already exist, create it with default queue
  26. // size of 8192 items and single thread.
  27. template <async_overflow_policy OverflowPolicy = async_overflow_policy::block>
  28. struct async_factory_impl {
  29. template <typename Sink, typename... SinkArgs>
  30. static std::shared_ptr<async_logger> create(std::string logger_name, SinkArgs &&...args) {
  31. auto &registry_inst = details::registry::instance();
  32. // create global thread pool if not already exists..
  33. auto &mutex = registry_inst.tp_mutex();
  34. std::lock_guard<std::recursive_mutex> tp_lock(mutex);
  35. auto tp = registry_inst.get_tp();
  36. if (tp == nullptr) {
  37. tp = std::make_shared<details::thread_pool>(details::default_async_q_size, 1U);
  38. registry_inst.set_tp(tp);
  39. }
  40. auto sink = std::make_shared<Sink>(std::forward<SinkArgs>(args)...);
  41. auto new_logger = std::make_shared<async_logger>(std::move(logger_name), std::move(sink),
  42. std::move(tp), OverflowPolicy);
  43. registry_inst.initialize_logger(new_logger);
  44. return new_logger;
  45. }
  46. };
  47. using async_factory = async_factory_impl<async_overflow_policy::block>;
  48. using async_factory_nonblock = async_factory_impl<async_overflow_policy::overrun_oldest>;
  49. template <typename Sink, typename... SinkArgs>
  50. inline std::shared_ptr<spdlog::logger> create_async(std::string logger_name,
  51. SinkArgs &&...sink_args) {
  52. return async_factory::create<Sink>(std::move(logger_name),
  53. std::forward<SinkArgs>(sink_args)...);
  54. }
  55. template <typename Sink, typename... SinkArgs>
  56. inline std::shared_ptr<spdlog::logger> create_async_nb(std::string logger_name,
  57. SinkArgs &&...sink_args) {
  58. return async_factory_nonblock::create<Sink>(std::move(logger_name),
  59. std::forward<SinkArgs>(sink_args)...);
  60. }
  61. // set global thread pool.
  62. inline void init_thread_pool(size_t q_size,
  63. size_t thread_count,
  64. std::function<void()> on_thread_start,
  65. std::function<void()> on_thread_stop) {
  66. auto tp = std::make_shared<details::thread_pool>(q_size, thread_count, on_thread_start,
  67. on_thread_stop);
  68. details::registry::instance().set_tp(std::move(tp));
  69. }
  70. inline void init_thread_pool(size_t q_size,
  71. size_t thread_count,
  72. std::function<void()> on_thread_start) {
  73. init_thread_pool(q_size, thread_count, on_thread_start, [] {});
  74. }
  75. inline void init_thread_pool(size_t q_size, size_t thread_count) {
  76. init_thread_pool(
  77. q_size, thread_count, [] {}, [] {});
  78. }
  79. // get the global thread pool.
  80. inline std::shared_ptr<spdlog::details::thread_pool> thread_pool() {
  81. return details::registry::instance().get_tp();
  82. }
  83. } // namespace spdlog