1// Copyright(c) 2015-present, Gabi Melman & spdlog contributors.
2// Distributed under the MIT License (http://opensource.org/licenses/MIT)
3
4#pragma once
5
6#ifndef SPDLOG_HEADER_ONLY
7 #include <spdlog/details/backtracer.h>
8#endif
9namespace spdlog {
10namespace details {
11SPDLOG_INLINE backtracer::backtracer(const backtracer &other) {
12 std::lock_guard<std::mutex> lock(other.mutex_);
13 enabled_ = other.enabled();
14 messages_ = other.messages_;
15}
16
17SPDLOG_INLINE backtracer::backtracer(backtracer &&other) SPDLOG_NOEXCEPT {
18 std::lock_guard<std::mutex> lock(other.mutex_);
19 enabled_ = other.enabled();
20 messages_ = std::move(other.messages_);
21}
22
23SPDLOG_INLINE backtracer &backtracer::operator=(backtracer other) {
24 std::lock_guard<std::mutex> lock(mutex_);
25 enabled_ = other.enabled();
26 messages_ = std::move(other.messages_);
27 return *this;
28}
29
30SPDLOG_INLINE void backtracer::enable(size_t size) {
31 std::lock_guard<std::mutex> lock{mutex_};
32 enabled_.store(i: true, m: std::memory_order_relaxed);
33 messages_ = circular_q<log_msg_buffer>{size};
34}
35
36SPDLOG_INLINE void backtracer::disable() {
37 std::lock_guard<std::mutex> lock{mutex_};
38 enabled_.store(i: false, m: std::memory_order_relaxed);
39}
40
41SPDLOG_INLINE bool backtracer::enabled() const { return enabled_.load(m: std::memory_order_relaxed); }
42
43SPDLOG_INLINE void backtracer::push_back(const log_msg &msg) {
44 std::lock_guard<std::mutex> lock{mutex_};
45 messages_.push_back(item: log_msg_buffer{msg});
46}
47
48SPDLOG_INLINE bool backtracer::empty() const {
49 std::lock_guard<std::mutex> lock{mutex_};
50 return messages_.empty();
51}
52
53// pop all items in the q and apply the given fun on each of them.
54SPDLOG_INLINE void backtracer::foreach_pop(std::function<void(const details::log_msg &)> fun) {
55 std::lock_guard<std::mutex> lock{mutex_};
56 while (!messages_.empty()) {
57 auto &front_msg = messages_.front();
58 fun(front_msg);
59 messages_.pop_front();
60 }
61}
62} // namespace details
63} // namespace spdlog
64