Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

async_logger_impl.h 2.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. //
  2. // Copyright(c) 2015 Gabi Melman.
  3. // Distributed under the MIT License (http://opensource.org/licenses/MIT)
  4. //
  5. #pragma once
  6. // Async Logger implementation
  7. // Use an async_sink (queue per logger) to perform the logging in a worker thread
  8. #include <spdlog/details/async_log_helper.h>
  9. #include <spdlog/async_logger.h>
  10. #include <string>
  11. #include <functional>
  12. #include <chrono>
  13. #include <memory>
  14. template<class It>
  15. inline spdlog::async_logger::async_logger(const std::string& logger_name,
  16. const It& begin,
  17. const It& end,
  18. size_t queue_size,
  19. const async_overflow_policy overflow_policy,
  20. const std::function<void()>& worker_warmup_cb,
  21. const std::chrono::milliseconds& flush_interval_ms,
  22. const std::function<void()>& worker_teardown_cb) :
  23. logger(logger_name, begin, end),
  24. _async_log_helper(new details::async_log_helper(_formatter, _sinks, queue_size, _err_handler, overflow_policy, worker_warmup_cb, flush_interval_ms, worker_teardown_cb))
  25. {
  26. }
  27. inline spdlog::async_logger::async_logger(const std::string& logger_name,
  28. sinks_init_list sinks,
  29. size_t queue_size,
  30. const async_overflow_policy overflow_policy,
  31. const std::function<void()>& worker_warmup_cb,
  32. const std::chrono::milliseconds& flush_interval_ms,
  33. const std::function<void()>& worker_teardown_cb) :
  34. async_logger(logger_name, sinks.begin(), sinks.end(), queue_size, overflow_policy, worker_warmup_cb, flush_interval_ms, worker_teardown_cb) {}
  35. inline spdlog::async_logger::async_logger(const std::string& logger_name,
  36. sink_ptr single_sink,
  37. size_t queue_size,
  38. const async_overflow_policy overflow_policy,
  39. const std::function<void()>& worker_warmup_cb,
  40. const std::chrono::milliseconds& flush_interval_ms,
  41. const std::function<void()>& worker_teardown_cb) :
  42. async_logger(logger_name,
  43. {
  44. single_sink
  45. }, queue_size, overflow_policy, worker_warmup_cb, flush_interval_ms, worker_teardown_cb) {}
  46. inline void spdlog::async_logger::flush()
  47. {
  48. _async_log_helper->flush(true);
  49. }
  50. inline void spdlog::async_logger::_set_formatter(spdlog::formatter_ptr msg_formatter)
  51. {
  52. _formatter = msg_formatter;
  53. _async_log_helper->set_formatter(_formatter);
  54. }
  55. inline void spdlog::async_logger::_set_pattern(const std::string& pattern)
  56. {
  57. _formatter = std::make_shared<pattern_formatter>(pattern);
  58. _async_log_helper->set_formatter(_formatter);
  59. }
  60. inline void spdlog::async_logger::_sink_it(details::log_msg& msg)
  61. {
  62. try
  63. {
  64. _async_log_helper->log(msg);
  65. if (_should_flush_on(msg))
  66. _async_log_helper->flush(false); // do async flush
  67. }
  68. catch (const std::exception &ex)
  69. {
  70. _err_handler(ex.what());
  71. }
  72. catch (...)
  73. {
  74. _err_handler("Unknown exception");
  75. }
  76. }