mirror of https://github.com/gabime/spdlog.git
commit
147bf04d08
@ -0,0 +1,58 @@
|
|||||||
|
// Copyright(c) 2015-present, Gabi Melman & spdlog contributors.
|
||||||
|
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "spdlog/common.h"
|
||||||
|
#include "spdlog/details/log_msg_buffer.h"
|
||||||
|
#include "spdlog/details/circular_q.h"
|
||||||
|
#include "spdlog/sinks/sink.h"
|
||||||
|
#include <mutex>
|
||||||
|
|
||||||
|
// Store log messages in circular buffer.
|
||||||
|
// Useful for storing debug data in case of error/warning happens.
|
||||||
|
|
||||||
|
namespace spdlog {
|
||||||
|
namespace details {
|
||||||
|
class backtracer
|
||||||
|
{
|
||||||
|
std::mutex mutex_;
|
||||||
|
size_t n_messages_;
|
||||||
|
circular_q<log_msg_buffer> messages_;
|
||||||
|
|
||||||
|
public:
|
||||||
|
explicit backtracer(size_t n_messages)
|
||||||
|
: n_messages_{n_messages}
|
||||||
|
, messages_{n_messages}
|
||||||
|
{}
|
||||||
|
|
||||||
|
backtracer(const backtracer &other)
|
||||||
|
: n_messages_{other.n_messages_}
|
||||||
|
, messages_{other.messages_}
|
||||||
|
{}
|
||||||
|
|
||||||
|
size_t n_messages() const
|
||||||
|
{
|
||||||
|
return n_messages_;
|
||||||
|
}
|
||||||
|
|
||||||
|
void add(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 give fun on each of them.
|
||||||
|
void foreach_pop(std::function<void(const details::log_msg)> fun)
|
||||||
|
{
|
||||||
|
std::lock_guard<std::mutex> lock{mutex_};
|
||||||
|
while (!messages_.empty())
|
||||||
|
{
|
||||||
|
log_msg_buffer popped;
|
||||||
|
messages_.pop_front(popped);
|
||||||
|
fun(popped);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
} // namespace details
|
||||||
|
} // namespace spdlog
|
@ -0,0 +1,60 @@
|
|||||||
|
// 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.h"
|
||||||
|
#include "spdlog/fmt/bundled/core.h"
|
||||||
|
|
||||||
|
namespace spdlog {
|
||||||
|
namespace details {
|
||||||
|
|
||||||
|
// extend log_msg with internal buffer to store its payload.
|
||||||
|
// this is needed since log_msg holds string_views that points to stack data.
|
||||||
|
|
||||||
|
class log_msg_buffer : public log_msg
|
||||||
|
{
|
||||||
|
fmt::basic_memory_buffer<char, 200> buffer;
|
||||||
|
void update_string_views()
|
||||||
|
{
|
||||||
|
logger_name = string_view_t{buffer.data(), logger_name.size()};
|
||||||
|
payload = string_view_t{logger_name.end(), payload.size()};
|
||||||
|
}
|
||||||
|
|
||||||
|
public:
|
||||||
|
log_msg_buffer() = default;
|
||||||
|
|
||||||
|
explicit log_msg_buffer(const log_msg &orig_msg)
|
||||||
|
: log_msg{orig_msg}
|
||||||
|
{
|
||||||
|
buffer.append(logger_name.begin(), logger_name.end());
|
||||||
|
buffer.append(payload.begin(), payload.end());
|
||||||
|
update_string_views();
|
||||||
|
}
|
||||||
|
|
||||||
|
log_msg_buffer(const log_msg_buffer &other)
|
||||||
|
: log_msg{other}
|
||||||
|
{
|
||||||
|
buffer.append(logger_name.begin(), logger_name.end());
|
||||||
|
buffer.append(payload.begin(), payload.end());
|
||||||
|
update_string_views();
|
||||||
|
}
|
||||||
|
|
||||||
|
log_msg_buffer(log_msg_buffer &&other)
|
||||||
|
: log_msg{std::move(other)}
|
||||||
|
, buffer{std::move(other.buffer)}
|
||||||
|
{
|
||||||
|
update_string_views();
|
||||||
|
}
|
||||||
|
|
||||||
|
log_msg_buffer &operator=(log_msg_buffer &&other)
|
||||||
|
{
|
||||||
|
log_msg::operator=(std::move(other));
|
||||||
|
buffer = std::move(other.buffer);
|
||||||
|
update_string_views();
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace details
|
||||||
|
} // namespace spdlog
|
@ -0,0 +1,65 @@
|
|||||||
|
#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