async_logger-inl.h 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. // Copyright(c) 2015-present, Gabi Melman & spdlog contributors.
  2. // Distributed under the MIT License (http://opensource.org/licenses/MIT)
  3. #pragma once
  4. #ifndef SPDLOG_HEADER_ONLY
  5. #include <spdlog/async_logger.h>
  6. #endif
  7. #include <spdlog/details/thread_pool.h>
  8. #include <spdlog/sinks/sink.h>
  9. #include <memory>
  10. #include <string>
  11. SPDLOG_INLINE spdlog::async_logger::async_logger(std::string logger_name,
  12. sinks_init_list sinks_list,
  13. std::weak_ptr<details::thread_pool> tp,
  14. async_overflow_policy overflow_policy)
  15. : async_logger(std::move(logger_name),
  16. sinks_list.begin(),
  17. sinks_list.end(),
  18. std::move(tp),
  19. overflow_policy) {}
  20. SPDLOG_INLINE spdlog::async_logger::async_logger(std::string logger_name,
  21. sink_ptr single_sink,
  22. std::weak_ptr<details::thread_pool> tp,
  23. async_overflow_policy overflow_policy)
  24. : async_logger(
  25. std::move(logger_name), {std::move(single_sink)}, std::move(tp), overflow_policy) {}
  26. // send the log message to the thread pool
  27. SPDLOG_INLINE void spdlog::async_logger::sink_it_(const details::log_msg &msg){
  28. SPDLOG_TRY{if (auto pool_ptr = thread_pool_.lock()){
  29. pool_ptr->post_log(shared_from_this(), msg, overflow_policy_);
  30. }
  31. else {
  32. throw_spdlog_ex("async log: thread pool doesn't exist anymore");
  33. }
  34. }
  35. SPDLOG_LOGGER_CATCH(msg.source)
  36. }
  37. // send flush request to the thread pool
  38. SPDLOG_INLINE void spdlog::async_logger::flush_(){SPDLOG_TRY{auto pool_ptr = thread_pool_.lock();
  39. if (!pool_ptr) {
  40. throw_spdlog_ex("async flush: thread pool doesn't exist anymore");
  41. }
  42. std::future<void> future = pool_ptr->post_flush(shared_from_this(), overflow_policy_);
  43. // Wait for the flush operation to complete.
  44. // This might throw exception if the flush message get dropped because of overflow.
  45. future.get();
  46. }
  47. SPDLOG_LOGGER_CATCH(source_loc())
  48. }
  49. //
  50. // backend functions - called from the thread pool to do the actual job
  51. //
  52. SPDLOG_INLINE void spdlog::async_logger::backend_sink_it_(const details::log_msg &msg) {
  53. for (auto &sink : sinks_) {
  54. if (sink->should_log(msg.level)) {
  55. SPDLOG_TRY { sink->log(msg); }
  56. SPDLOG_LOGGER_CATCH(msg.source)
  57. }
  58. }
  59. if (should_flush_(msg)) {
  60. backend_flush_();
  61. }
  62. }
  63. SPDLOG_INLINE void spdlog::async_logger::backend_flush_() {
  64. for (auto &sink : sinks_) {
  65. SPDLOG_TRY { sink->flush(); }
  66. SPDLOG_LOGGER_CATCH(source_loc())
  67. }
  68. }
  69. SPDLOG_INLINE std::shared_ptr<spdlog::logger> spdlog::async_logger::clone(std::string new_name) {
  70. auto cloned = std::make_shared<spdlog::async_logger>(*this);
  71. cloned->name_ = std::move(new_name);
  72. return cloned;
  73. }