You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

async_logger.h 2.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. //
  2. // Copyright(c) 2015 Gabi Melman.
  3. // Distributed under the MIT License (http://opensource.org/licenses/MIT)
  4. //
  5. #pragma once
  6. // Very fast asynchronous logger (millions of logs per second on an average desktop)
  7. // Uses pre allocated lockfree queue for maximum throughput even under large number of threads.
  8. // Creates a single back thread to pop messages from the queue and log them.
  9. //
  10. // Upon each log write the logger:
  11. // 1. Checks if its log level is enough to log the message
  12. // 2. Push a new copy of the message to a queue (or block the caller until space is available in the queue)
  13. // 3. will throw spdlog_ex upon log exceptions
  14. // Upon destruction, logs all remaining messages in the queue before destructing..
  15. #include <spdlog/common.h>
  16. #include <spdlog/logger.h>
  17. #include <chrono>
  18. #include <functional>
  19. #include <string>
  20. #include <memory>
  21. namespace spdlog
  22. {
  23. namespace details
  24. {
  25. class async_log_helper;
  26. }
  27. class async_logger :public logger
  28. {
  29. public:
  30. template<class It>
  31. async_logger(const std::string& name,
  32. const It& begin,
  33. const It& end,
  34. size_t queue_size,
  35. const async_overflow_policy overflow_policy = async_overflow_policy::block_retry,
  36. const std::function<void()>& worker_warmup_cb = nullptr,
  37. const std::chrono::milliseconds& flush_interval_ms = std::chrono::milliseconds::zero(),
  38. const std::function<void()>& worker_teardown_cb = nullptr);
  39. async_logger(const std::string& logger_name,
  40. sinks_init_list sinks,
  41. size_t queue_size,
  42. const async_overflow_policy overflow_policy = async_overflow_policy::block_retry,
  43. const std::function<void()>& worker_warmup_cb = nullptr,
  44. const std::chrono::milliseconds& flush_interval_ms = std::chrono::milliseconds::zero(),
  45. const std::function<void()>& worker_teardown_cb = nullptr);
  46. async_logger(const std::string& logger_name,
  47. sink_ptr single_sink,
  48. size_t queue_size,
  49. const async_overflow_policy overflow_policy = async_overflow_policy::block_retry,
  50. const std::function<void()>& worker_warmup_cb = nullptr,
  51. const std::chrono::milliseconds& flush_interval_ms = std::chrono::milliseconds::zero(),
  52. const std::function<void()>& worker_teardown_cb = nullptr);
  53. //Wait for the queue to be empty, and flush synchronously
  54. //Warning: this can potentialy last forever as we wait it to complete
  55. void flush() override;
  56. protected:
  57. void _sink_it(details::log_msg& msg) override;
  58. void _set_formatter(spdlog::formatter_ptr msg_formatter) override;
  59. void _set_pattern(const std::string& pattern) override;
  60. private:
  61. std::unique_ptr<details::async_log_helper> _async_log_helper;
  62. };
  63. }
  64. #include <spdlog/details/async_logger_impl.h>