backtracer-inl.h 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. SPDLOG_INLINE bool backtracer::empty() const
  49. {
  50. std::lock_guard<std::mutex> lock{mutex_};
  51. return messages_.empty();
  52. }
  53. // pop all items in the q and apply the given fun on each of them.
  54. SPDLOG_INLINE void backtracer::foreach_pop(std::function<void(const details::log_msg &)> fun)
  55. {
  56. std::lock_guard<std::mutex> lock{mutex_};
  57. while (!messages_.empty())
  58. {
  59. auto &front_msg = messages_.front();
  60. fun(front_msg);
  61. messages_.pop_front();
  62. }
  63. }
  64. } // namespace details
  65. } // namespace spdlog