backtracer-inl.h 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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/details/backtracer.h>
  6. #endif
  7. namespace spdlog {
  8. namespace details {
  9. SPDLOG_INLINE backtracer::backtracer(const backtracer &other)
  10. {
  11. std::lock_guard<std::mutex> lock(other.mutex_);
  12. enabled_ = other.enabled();
  13. messages_ = other.messages_;
  14. }
  15. SPDLOG_INLINE backtracer::backtracer(backtracer &&other) SPDLOG_NOEXCEPT
  16. {
  17. std::lock_guard<std::mutex> lock(other.mutex_);
  18. enabled_ = other.enabled();
  19. messages_ = std::move(other.messages_);
  20. }
  21. SPDLOG_INLINE backtracer &backtracer::operator=(backtracer other)
  22. {
  23. std::lock_guard<std::mutex> lock(mutex_);
  24. enabled_ = other.enabled();
  25. messages_ = std::move(other.messages_);
  26. return *this;
  27. }
  28. SPDLOG_INLINE void backtracer::enable(size_t size)
  29. {
  30. std::lock_guard<std::mutex> lock{mutex_};
  31. enabled_.store(true, std::memory_order_relaxed);
  32. messages_ = circular_q<log_msg_buffer>{size};
  33. }
  34. SPDLOG_INLINE void backtracer::disable()
  35. {
  36. std::lock_guard<std::mutex> lock{mutex_};
  37. enabled_.store(false, std::memory_order_relaxed);
  38. }
  39. SPDLOG_INLINE bool backtracer::enabled() const
  40. {
  41. return enabled_.load(std::memory_order_relaxed);
  42. }
  43. SPDLOG_INLINE void backtracer::push_back(const log_msg &msg)
  44. {
  45. std::lock_guard<std::mutex> lock{mutex_};
  46. messages_.push_back(log_msg_buffer{msg});
  47. }
  48. // pop all items in the q and apply the given fun on each of them.
  49. SPDLOG_INLINE void backtracer::foreach_pop(std::function<void(const details::log_msg &)> fun)
  50. {
  51. std::lock_guard<std::mutex> lock{mutex_};
  52. while (!messages_.empty())
  53. {
  54. auto &front_msg = messages_.front();
  55. fun(front_msg);
  56. messages_.pop_front();
  57. }
  58. }
  59. } // namespace details
  60. } // namespace spdlog