mirror of
https://github.com/gabime/spdlog.git
synced 2025-01-13 01:10:26 +08:00
Added bactrace tests
This commit is contained in:
parent
75adf9e75e
commit
5c1e44a93d
@ -23,7 +23,8 @@ set(SPDLOG_UTESTS_SOURCES
|
|||||||
test_sink.h
|
test_sink.h
|
||||||
test_fmt_helper.cpp
|
test_fmt_helper.cpp
|
||||||
test_stdout_api.cpp
|
test_stdout_api.cpp
|
||||||
test_dup_filter.cpp)
|
test_dup_filter.cpp
|
||||||
|
test_backtrace.cpp)
|
||||||
|
|
||||||
if(NOT SPDLOG_NO_EXCEPTIONS)
|
if(NOT SPDLOG_NO_EXCEPTIONS)
|
||||||
list(APPEND SPDLOG_UTESTS_SOURCES test_errors.cpp)
|
list(APPEND SPDLOG_UTESTS_SOURCES test_errors.cpp)
|
||||||
|
36
tests/test_backtrace.cpp
Normal file
36
tests/test_backtrace.cpp
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
#include "includes.h"
|
||||||
|
#include "test_sink.h"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
TEST_CASE("bactrace1", "[bactrace]")
|
||||||
|
{
|
||||||
|
|
||||||
|
using spdlog::sinks::test_sink_st;
|
||||||
|
auto test_sink = std::make_shared<test_sink_st>();
|
||||||
|
int backtrace_size = 5;
|
||||||
|
|
||||||
|
spdlog::logger logger("test", 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() == 8);
|
||||||
|
REQUIRE(test_sink->lines()[1] == "****************** Backtrace Start ******************");
|
||||||
|
|
||||||
|
for(int i = 0; i < backtrace_size; i++)
|
||||||
|
{
|
||||||
|
REQUIRE(test_sink->lines()[i+2] == fmt::format("debug message {}", 100 - backtrace_size + i));
|
||||||
|
}
|
||||||
|
REQUIRE(test_sink->lines()[7] == "****************** Backtrace End ********************");
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -18,6 +18,7 @@ namespace sinks {
|
|||||||
template<class Mutex>
|
template<class Mutex>
|
||||||
class test_sink : public base_sink<Mutex>
|
class test_sink : public base_sink<Mutex>
|
||||||
{
|
{
|
||||||
|
const size_t lines_to_save = 100;
|
||||||
public:
|
public:
|
||||||
size_t msg_counter()
|
size_t msg_counter()
|
||||||
{
|
{
|
||||||
@ -33,23 +34,29 @@ public:
|
|||||||
|
|
||||||
void set_delay(std::chrono::milliseconds delay)
|
void set_delay(std::chrono::milliseconds delay)
|
||||||
{
|
{
|
||||||
|
std::lock_guard<Mutex> lock(base_sink<Mutex>::mutex_);
|
||||||
delay_ = delay;
|
delay_ = delay;
|
||||||
}
|
}
|
||||||
|
|
||||||
// return last output without the eol
|
// return last output without the eol
|
||||||
std::string last_output()
|
std::vector<std::string> lines()
|
||||||
{
|
{
|
||||||
std::lock_guard<Mutex> lock(base_sink<Mutex>::mutex_);
|
std::lock_guard<Mutex> lock(base_sink<Mutex>::mutex_);
|
||||||
auto eol_len = strlen(spdlog::details::os::default_eol);
|
return lines_;
|
||||||
return std::string(last_output_.begin(), last_output_.end() - eol_len);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
void sink_it_(const details::log_msg &msg) override
|
void sink_it_(const details::log_msg &msg) override
|
||||||
{
|
{
|
||||||
fmt::memory_buffer formatted;
|
fmt::memory_buffer formatted;
|
||||||
base_sink<Mutex>::formatter_->format(msg, formatted);
|
base_sink<Mutex>::formatter_->format(msg, formatted);
|
||||||
last_output_.assign(formatted.begin(), formatted.end());
|
// save the line without the eol
|
||||||
|
auto eol_len = strlen(details::os::default_eol);
|
||||||
|
if(lines_.size() < lines_to_save)
|
||||||
|
{
|
||||||
|
lines_.emplace_back(formatted.begin(), formatted.end()-eol_len);
|
||||||
|
}
|
||||||
msg_counter_++;
|
msg_counter_++;
|
||||||
std::this_thread::sleep_for(delay_);
|
std::this_thread::sleep_for(delay_);
|
||||||
}
|
}
|
||||||
@ -62,7 +69,7 @@ protected:
|
|||||||
size_t msg_counter_{0};
|
size_t msg_counter_{0};
|
||||||
size_t flush_counter_{0};
|
size_t flush_counter_{0};
|
||||||
std::chrono::milliseconds delay_{std::chrono::milliseconds::zero()};
|
std::chrono::milliseconds delay_{std::chrono::milliseconds::zero()};
|
||||||
std::string last_output_;
|
std::vector<std::string> lines_;
|
||||||
};
|
};
|
||||||
|
|
||||||
using test_sink_mt = test_sink<std::mutex>;
|
using test_sink_mt = test_sink<std::mutex>;
|
||||||
|
Loading…
Reference in New Issue
Block a user