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 | #include <spdlog/details/circular_q.h> |
7 | #include <spdlog/details/log_msg_buffer.h> |
8 | |
9 | #include <atomic> |
10 | #include <functional> |
11 | #include <mutex> |
12 | |
13 | // Store log messages in circular buffer. |
14 | // Useful for storing debug data in case of error/warning happens. |
15 | |
16 | namespace spdlog { |
17 | namespace details { |
18 | class SPDLOG_API backtracer { |
19 | mutable std::mutex mutex_; |
20 | std::atomic<bool> enabled_{false}; |
21 | circular_q<log_msg_buffer> messages_; |
22 | |
23 | public: |
24 | backtracer() = default; |
25 | backtracer(const backtracer &other); |
26 | |
27 | backtracer(backtracer &&other) SPDLOG_NOEXCEPT; |
28 | backtracer &operator=(backtracer other); |
29 | |
30 | void enable(size_t size); |
31 | void disable(); |
32 | bool enabled() const; |
33 | void push_back(const log_msg &msg); |
34 | bool empty() const; |
35 | |
36 | // pop all items in the q and apply the given fun on each of them. |
37 | void foreach_pop(std::function<void(const details::log_msg &)> fun); |
38 | }; |
39 | |
40 | } // namespace details |
41 | } // namespace spdlog |
42 | |
43 | #ifdef SPDLOG_HEADER_ONLY |
44 | #include "backtracer-inl.h" |
45 | #endif |
46 |