backtracer.h 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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/circular_q.h>
  5. #include <spdlog/details/log_msg_buffer.h>
  6. #include <atomic>
  7. #include <functional>
  8. #include <mutex>
  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. mutable std::mutex mutex_;
  15. std::atomic<bool> enabled_{false};
  16. circular_q<log_msg_buffer> messages_;
  17. public:
  18. backtracer() = default;
  19. backtracer(const backtracer &other);
  20. backtracer(backtracer &&other) SPDLOG_NOEXCEPT;
  21. backtracer &operator=(backtracer other);
  22. void enable(size_t size);
  23. void disable();
  24. bool enabled() const;
  25. void push_back(const log_msg &msg);
  26. bool empty() const;
  27. // pop all items in the q and apply the given fun on each of them.
  28. void foreach_pop(std::function<void(const details::log_msg &)> fun);
  29. };
  30. } // namespace details
  31. } // namespace spdlog
  32. #ifdef SPDLOG_HEADER_ONLY
  33. #include "backtracer-inl.h"
  34. #endif