backtracer.h 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. // Copyright(c) 2015-present, Gabi Melman & spdlog contributors.
  2. // Distributed under the MIT License (http://opensource.org/licenses/MIT)
  3. #pragma once
  4. #include <spdlog/details/log_msg_buffer.h>
  5. #include <spdlog/details/circular_q.h>
  6. #include <atomic>
  7. #include <mutex>
  8. #include <functional>
  9. // Store log messages in circular buffer.
  10. // Useful for storing debug data in case of error/warning happens.
  11. namespace spdlog {
  12. namespace details {
  13. class SPDLOG_API backtracer
  14. {
  15. mutable std::mutex mutex_;
  16. std::atomic<bool> enabled_{false};
  17. circular_q<log_msg_buffer> messages_;
  18. public:
  19. backtracer() = default;
  20. backtracer(const backtracer &other);
  21. backtracer(backtracer &&other) SPDLOG_NOEXCEPT;
  22. backtracer &operator=(backtracer other);
  23. void enable(size_t size);
  24. void disable();
  25. bool enabled() const;
  26. void push_back(const log_msg &msg);
  27. bool empty() const;
  28. // pop all items in the q and apply the given fun on each of them.
  29. void foreach_pop(std::function<void(const details::log_msg &)> fun);
  30. };
  31. } // namespace details
  32. } // namespace spdlog
  33. #ifdef SPDLOG_HEADER_ONLY
  34. # include "backtracer-inl.h"
  35. #endif