diff --git a/include/spdlog/logger-inl.h b/include/spdlog/logger-inl.h index 269932c2..fccf46ce 100644 --- a/include/spdlog/logger-inl.h +++ b/include/spdlog/logger-inl.h @@ -116,9 +116,9 @@ SPDLOG_INLINE void logger::disable_backtrace() tracer_.disable(); } -SPDLOG_INLINE void logger::dump_backtrace() +SPDLOG_INLINE void logger::dump_backtrace(bool with_message) { - dump_backtrace_(); + dump_backtrace_(with_message); } // flush functions diff --git a/include/spdlog/logger.h b/include/spdlog/logger.h index 0802a5d9..540e455d 100644 --- a/include/spdlog/logger.h +++ b/include/spdlog/logger.h @@ -329,7 +329,7 @@ public: // efficiently store all debug/trace messages in a circular buffer until needed for debugging. void enable_backtrace(size_t n_messages); void disable_backtrace(); - void dump_backtrace(); + void dump_backtrace(bool with_message=true); // flush functions void flush(); @@ -410,7 +410,7 @@ protected: void log_it_(const details::log_msg &log_msg, bool log_enabled, bool traceback_enabled); virtual void sink_it_(const details::log_msg &msg); virtual void flush_(); - void dump_backtrace_(); + void dump_backtrace_(bool with_message=true); bool should_flush_(const details::log_msg &msg); // handle errors during logging. diff --git a/tests/test_backtrace.cpp b/tests/test_backtrace.cpp index 6cf9ec55..f4d03fcb 100644 --- a/tests/test_backtrace.cpp +++ b/tests/test_backtrace.cpp @@ -31,6 +31,33 @@ TEST_CASE("bactrace1", "[bactrace]") REQUIRE(test_sink->lines()[7] == "****************** Backtrace End ********************"); } +TEST_CASE("bactrace2", "[bactrace]") +{ + + using spdlog::sinks::test_sink_st; + auto test_sink = std::make_shared(); + 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(false); + REQUIRE(test_sink->lines().size() == backtrace_size + 1); + REQUIRE(test_sink->lines()[1] == "debug message 95"); + REQUIRE(test_sink->lines()[2] == "debug message 96"); + REQUIRE(test_sink->lines()[3] == "debug message 97"); + REQUIRE(test_sink->lines()[4] == "debug message 98"); + REQUIRE(test_sink->lines()[5] == "debug message 99"); +} + TEST_CASE("bactrace-empty", "[bactrace]") { using spdlog::sinks::test_sink_st;