Commit 5c1e44a9 authored by gabime's avatar gabime

Added bactrace tests

parent 75adf9e7
...@@ -23,7 +23,8 @@ set(SPDLOG_UTESTS_SOURCES ...@@ -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)
......
#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 { ...@@ -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: ...@@ -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: ...@@ -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>;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment