test_custom_callbacks.cpp 1.2 KB

1234567891011121314151617181920212223242526272829303132333435
  1. /*
  2. * This content is released under the MIT License as specified in
  3. * https://raw.githubusercontent.com/gabime/spdlog/master/LICENSE
  4. */
  5. #include "includes.h"
  6. #include "test_sink.h"
  7. #include "spdlog/sinks/callback_sink.h"
  8. #include "spdlog/async.h"
  9. #include "spdlog/common.h"
  10. TEST_CASE("custom_callback_logger", "[custom_callback_logger]") {
  11. std::vector<std::string> lines;
  12. spdlog::pattern_formatter formatter;
  13. auto callback_logger =
  14. std::make_shared<spdlog::sinks::callback_sink_st>([&](const spdlog::details::log_msg &msg) {
  15. spdlog::memory_buf_t formatted;
  16. formatter.format(msg, formatted);
  17. auto eol_len = strlen(spdlog::details::os::default_eol);
  18. lines.emplace_back(formatted.begin(), formatted.end() - eol_len);
  19. });
  20. std::shared_ptr<spdlog::sinks::test_sink_st> test_sink(new spdlog::sinks::test_sink_st);
  21. spdlog::logger logger("test-callback", {callback_logger, test_sink});
  22. logger.info("test message 1");
  23. logger.info("test message 2");
  24. logger.info("test message 3");
  25. std::vector<std::string> ref_lines = test_sink->lines();
  26. REQUIRE(lines[0] == ref_lines[0]);
  27. REQUIRE(lines[1] == ref_lines[1]);
  28. REQUIRE(lines[2] == ref_lines[2]);
  29. spdlog::drop_all();
  30. }