Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

94 linhas
3.3KB

  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 withe 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 <memory>
  18. #include <mutex>
  19. #include <functional>
  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. {
  30. template<typename Sink, typename... SinkArgs>
  31. static std::shared_ptr<async_logger> create(std::string logger_name, SinkArgs &&... args)
  32. {
  33. auto &registry_inst = details::registry::instance();
  34. // create global thread pool if not already exists..
  35. auto &mutex = registry_inst.tp_mutex();
  36. std::lock_guard<std::recursive_mutex> tp_lock(mutex);
  37. auto tp = registry_inst.get_tp();
  38. if (tp == nullptr)
  39. {
  40. tp = std::make_shared<details::thread_pool>(details::default_async_q_size, 1);
  41. registry_inst.set_tp(tp);
  42. }
  43. auto sink = std::make_shared<Sink>(std::forward<SinkArgs>(args)...);
  44. auto new_logger = std::make_shared<async_logger>(std::move(logger_name), std::move(sink), std::move(tp), OverflowPolicy);
  45. registry_inst.initialize_logger(new_logger);
  46. return new_logger;
  47. }
  48. };
  49. using async_factory = async_factory_impl<async_overflow_policy::block>;
  50. using async_factory_nonblock = async_factory_impl<async_overflow_policy::overrun_oldest>;
  51. template<typename Sink, typename... SinkArgs>
  52. inline std::shared_ptr<spdlog::logger> create_async(std::string logger_name, SinkArgs &&... sink_args)
  53. {
  54. return async_factory::create<Sink>(std::move(logger_name), std::forward<SinkArgs>(sink_args)...);
  55. }
  56. template<typename Sink, typename... SinkArgs>
  57. inline std::shared_ptr<spdlog::logger> create_async_nb(std::string logger_name, SinkArgs &&... sink_args)
  58. {
  59. return async_factory_nonblock::create<Sink>(std::move(logger_name), std::forward<SinkArgs>(sink_args)...);
  60. }
  61. // set global thread pool.
  62. inline void init_thread_pool(size_t q_size, size_t thread_count, std::function<void()> on_thread_start)
  63. {
  64. auto tp = std::make_shared<details::thread_pool>(q_size, thread_count, on_thread_start);
  65. details::registry::instance().set_tp(std::move(tp));
  66. }
  67. // set global thread pool.
  68. inline void init_thread_pool(size_t q_size, size_t thread_count)
  69. {
  70. init_thread_pool(q_size, thread_count, [] {});
  71. }
  72. // get the global thread pool.
  73. inline std::shared_ptr<spdlog::details::thread_pool> thread_pool()
  74. {
  75. return details::registry::instance().get_tp();
  76. }
  77. } // namespace spdlog