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/log_msg_buffer.h> |
7 | #include <spdlog/details/circular_q.h> |
8 | |
9 | #include <atomic> |
10 | #include <mutex> |
11 | #include <functional> |
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 | { |
20 | mutable std::mutex mutex_; |
21 | std::atomic<bool> enabled_{false}; |
22 | circular_q<log_msg_buffer> messages_; |
23 | |
24 | public: |
25 | backtracer() = default; |
26 | backtracer(const backtracer &other); |
27 | |
28 | backtracer(backtracer &&other) SPDLOG_NOEXCEPT; |
29 | backtracer &operator=(backtracer other); |
30 | |
31 | void enable(size_t size); |
32 | void disable(); |
33 | bool enabled() const; |
34 | void push_back(const log_msg &msg); |
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 | |