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