mirror of https://github.com/gabime/spdlog.git
Removed backtrace support
parent
3420dda687
commit
34cf7fc8f1
@ -1,41 +0,0 @@
|
|||||||
// Copyright(c) 2015-present, Gabi Melman & spdlog contributors.
|
|
||||||
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
|
|
||||||
|
|
||||||
#pragma once
|
|
||||||
|
|
||||||
#include <spdlog/details/log_msg_buffer.h>
|
|
||||||
#include <spdlog/details/circular_q.h>
|
|
||||||
|
|
||||||
#include <atomic>
|
|
||||||
#include <mutex>
|
|
||||||
#include <functional>
|
|
||||||
|
|
||||||
// Store log messages in circular buffer.
|
|
||||||
// Useful for storing debug data in case of error/warning happens.
|
|
||||||
|
|
||||||
namespace spdlog {
|
|
||||||
namespace details {
|
|
||||||
class SPDLOG_API backtracer
|
|
||||||
{
|
|
||||||
mutable std::mutex mutex_;
|
|
||||||
std::atomic<bool> enabled_{false};
|
|
||||||
circular_q<log_msg_buffer> messages_;
|
|
||||||
|
|
||||||
public:
|
|
||||||
backtracer() = default;
|
|
||||||
backtracer(const backtracer &other);
|
|
||||||
|
|
||||||
backtracer(backtracer &&other) noexcept;
|
|
||||||
backtracer &operator=(backtracer other);
|
|
||||||
|
|
||||||
void enable(size_t size);
|
|
||||||
void disable();
|
|
||||||
bool enabled() const;
|
|
||||||
void push_back(const log_msg &msg);
|
|
||||||
|
|
||||||
// pop all items in the q and apply the given fun on each of them.
|
|
||||||
void foreach_pop(std::function<void(const details::log_msg &)> fun);
|
|
||||||
};
|
|
||||||
|
|
||||||
} // namespace details
|
|
||||||
} // namespace spdlog
|
|
@ -1,66 +0,0 @@
|
|||||||
// Copyright(c) 2015-present, Gabi Melman & spdlog contributors.
|
|
||||||
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
|
|
||||||
|
|
||||||
#include <spdlog/details/backtracer.h>
|
|
||||||
|
|
||||||
namespace spdlog {
|
|
||||||
namespace details {
|
|
||||||
SPDLOG_INLINE backtracer::backtracer(const backtracer &other)
|
|
||||||
{
|
|
||||||
std::lock_guard<std::mutex> lock(other.mutex_);
|
|
||||||
enabled_ = other.enabled();
|
|
||||||
messages_ = other.messages_;
|
|
||||||
}
|
|
||||||
|
|
||||||
SPDLOG_INLINE backtracer::backtracer(backtracer &&other) noexcept
|
|
||||||
{
|
|
||||||
std::lock_guard<std::mutex> lock(other.mutex_);
|
|
||||||
enabled_ = other.enabled();
|
|
||||||
messages_ = std::move(other.messages_);
|
|
||||||
}
|
|
||||||
|
|
||||||
SPDLOG_INLINE backtracer &backtracer::operator=(backtracer other)
|
|
||||||
{
|
|
||||||
std::lock_guard<std::mutex> lock(mutex_);
|
|
||||||
enabled_ = other.enabled();
|
|
||||||
messages_ = std::move(other.messages_);
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
|
|
||||||
SPDLOG_INLINE void backtracer::enable(size_t size)
|
|
||||||
{
|
|
||||||
std::lock_guard<std::mutex> lock{mutex_};
|
|
||||||
enabled_.store(true, std::memory_order_relaxed);
|
|
||||||
messages_ = circular_q<log_msg_buffer>{size};
|
|
||||||
}
|
|
||||||
|
|
||||||
SPDLOG_INLINE void backtracer::disable()
|
|
||||||
{
|
|
||||||
std::lock_guard<std::mutex> lock{mutex_};
|
|
||||||
enabled_.store(false, std::memory_order_relaxed);
|
|
||||||
}
|
|
||||||
|
|
||||||
SPDLOG_INLINE bool backtracer::enabled() const
|
|
||||||
{
|
|
||||||
return enabled_.load(std::memory_order_relaxed);
|
|
||||||
}
|
|
||||||
|
|
||||||
SPDLOG_INLINE void backtracer::push_back(const log_msg &msg)
|
|
||||||
{
|
|
||||||
std::lock_guard<std::mutex> lock{mutex_};
|
|
||||||
messages_.push_back(log_msg_buffer{msg});
|
|
||||||
}
|
|
||||||
|
|
||||||
// pop all items in the q and apply the given fun on each of them.
|
|
||||||
SPDLOG_INLINE void backtracer::foreach_pop(std::function<void(const details::log_msg &)> fun)
|
|
||||||
{
|
|
||||||
std::lock_guard<std::mutex> lock{mutex_};
|
|
||||||
while (!messages_.empty())
|
|
||||||
{
|
|
||||||
auto &front_msg = messages_.front();
|
|
||||||
fun(front_msg);
|
|
||||||
messages_.pop_front();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} // namespace details
|
|
||||||
} // namespace spdlog
|
|
@ -1,65 +0,0 @@
|
|||||||
#include "includes.h"
|
|
||||||
#include "test_sink.h"
|
|
||||||
#include "spdlog/async.h"
|
|
||||||
|
|
||||||
TEST_CASE("bactrace1", "[bactrace]")
|
|
||||||
{
|
|
||||||
|
|
||||||
using spdlog::sinks::test_sink_st;
|
|
||||||
auto test_sink = std::make_shared<test_sink_st>();
|
|
||||||
size_t backtrace_size = 5;
|
|
||||||
|
|
||||||
spdlog::logger logger("test-backtrace", test_sink);
|
|
||||||
logger.set_pattern("%v");
|
|
||||||
logger.enable_backtrace(backtrace_size);
|
|
||||||
|
|
||||||
logger.info("info message");
|
|
||||||
for (int i = 0; i < 100; i++)
|
|
||||||
logger.debug("debug message {}", i);
|
|
||||||
|
|
||||||
REQUIRE(test_sink->lines().size() == 1);
|
|
||||||
REQUIRE(test_sink->lines()[0] == "info message");
|
|
||||||
|
|
||||||
logger.dump_backtrace();
|
|
||||||
REQUIRE(test_sink->lines().size() == backtrace_size + 3);
|
|
||||||
REQUIRE(test_sink->lines()[1] == "****************** Backtrace Start ******************");
|
|
||||||
REQUIRE(test_sink->lines()[2] == "debug message 95");
|
|
||||||
REQUIRE(test_sink->lines()[3] == "debug message 96");
|
|
||||||
REQUIRE(test_sink->lines()[4] == "debug message 97");
|
|
||||||
REQUIRE(test_sink->lines()[5] == "debug message 98");
|
|
||||||
REQUIRE(test_sink->lines()[6] == "debug message 99");
|
|
||||||
REQUIRE(test_sink->lines()[7] == "****************** Backtrace End ********************");
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST_CASE("bactrace-async", "[bactrace]")
|
|
||||||
{
|
|
||||||
using spdlog::sinks::test_sink_mt;
|
|
||||||
auto test_sink = std::make_shared<test_sink_mt>();
|
|
||||||
using spdlog::details::os::sleep_for_millis;
|
|
||||||
|
|
||||||
size_t backtrace_size = 5;
|
|
||||||
|
|
||||||
spdlog::init_thread_pool(120, 1);
|
|
||||||
auto logger = std::make_shared<spdlog::async_logger>("test-bactrace-async", test_sink, spdlog::thread_pool());
|
|
||||||
logger->set_pattern("%v");
|
|
||||||
logger->enable_backtrace(backtrace_size);
|
|
||||||
|
|
||||||
logger->info("info message");
|
|
||||||
for (int i = 0; i < 100; i++)
|
|
||||||
logger->debug("debug message {}", i);
|
|
||||||
|
|
||||||
sleep_for_millis(10);
|
|
||||||
REQUIRE(test_sink->lines().size() == 1);
|
|
||||||
REQUIRE(test_sink->lines()[0] == "info message");
|
|
||||||
|
|
||||||
logger->dump_backtrace();
|
|
||||||
sleep_for_millis(100); // give time for the async dump to complete
|
|
||||||
REQUIRE(test_sink->lines().size() == backtrace_size + 3);
|
|
||||||
REQUIRE(test_sink->lines()[1] == "****************** Backtrace Start ******************");
|
|
||||||
REQUIRE(test_sink->lines()[2] == "debug message 95");
|
|
||||||
REQUIRE(test_sink->lines()[3] == "debug message 96");
|
|
||||||
REQUIRE(test_sink->lines()[4] == "debug message 97");
|
|
||||||
REQUIRE(test_sink->lines()[5] == "debug message 98");
|
|
||||||
REQUIRE(test_sink->lines()[6] == "debug message 99");
|
|
||||||
REQUIRE(test_sink->lines()[7] == "****************** Backtrace End ********************");
|
|
||||||
}
|
|
Loading…
Reference in New Issue