backtracer-inl.h 2.0 KB

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