diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 2cb18a4a3..5456a4fde 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -50,6 +50,7 @@ set(SPDLOG_UTESTS_SOURCES test_circular_q.cpp test_ringbuffer_sink.cpp test_source_location.cpp + test_no_source_location.cpp test_log_level.cpp test_include_sinks.cpp) diff --git a/tests/test_no_source_location.cpp b/tests/test_no_source_location.cpp new file mode 100644 index 000000000..19b652a56 --- /dev/null +++ b/tests/test_no_source_location.cpp @@ -0,0 +1,22 @@ +#ifndef SPDLOG_NO_SOURCE_LOC +#define SPDLOG_NO_SOURCE_LOC +#endif + +#include "includes.h" +#include "test_sink.h" + +// no source location should appear in the log message +TEST_CASE("test_no_source_location", "[source_location]") { + auto test_sink = std::make_shared(); + auto logger = std::make_shared("test", test_sink); + logger->set_pattern("%s:%#:%! %v"); + + // test with no source location with parameters + SPDLOG_LOGGER_CALL(logger, spdlog::level::info, "Hello {}", "no source location"); + REQUIRE(test_sink->lines().size() == 1); + REQUIRE(test_sink->lines()[0] == ":: Hello no source location"); + // test with no source location without parameters + SPDLOG_LOGGER_CALL(logger, spdlog::level::info, "Hello"); + REQUIRE(test_sink->lines().size() == 2); + REQUIRE(test_sink->lines()[1] == ":: Hello"); +} diff --git a/tests/test_sink.h b/tests/test_sink.h index 827034d92..fd787bd0b 100644 --- a/tests/test_sink.h +++ b/tests/test_sink.h @@ -12,6 +12,7 @@ #include "spdlog/details/null_mutex.h" #include "spdlog/fmt/fmt.h" #include "spdlog/sinks/base_sink.h" +#include "spdlog/details/os.h" namespace spdlog { namespace sinks { diff --git a/tests/test_source_location.cpp b/tests/test_source_location.cpp index 1bd0da42e..04b3e6d70 100644 --- a/tests/test_source_location.cpp +++ b/tests/test_source_location.cpp @@ -1,21 +1,25 @@ -#ifndef SPDLOG_NO_SOURCE_LOC +#ifdef SPDLOG_NO_SOURCE_LOC +#undef SPDLOG_NO_SOURCE_LOC +#endif #include "includes.h" -#include "spdlog/sinks/ostream_sink.h" #include "test_sink.h" -using spdlog::details::os::default_eol; - +// test with source location TEST_CASE("test_source_location", "[source_location]") { - std::ostringstream oss; - auto oss_sink = std::make_shared(oss); - auto oss_logger = std::make_shared("oss", oss_sink); - //spdlog::logger oss_logger("oss", oss_sink); - oss_logger->set_pattern("%s:%# %v"); - - SPDLOG_LOGGER_CALL(oss_logger, spdlog::level::info, "Hello {}", "source location"); - REQUIRE(oss.str() == std::string("test_source_location.cpp:17 Hello source location") + default_eol); + auto test_sink = std::make_shared(); + auto logger = std::make_shared("test", test_sink); + logger->set_pattern("%s:%# %v"); + // test with source location with parameters + SPDLOG_LOGGER_CALL(logger, spdlog::level::info, "Hello {}", "source location"); + REQUIRE(test_sink->lines().size() == 1); + REQUIRE(test_sink->lines()[0] == "test_source_location.cpp:14 Hello source location"); + // test with source location without parameters + SPDLOG_LOGGER_CALL(logger, spdlog::level::info, "Hello"); + REQUIRE(test_sink->lines().size() == 2); + REQUIRE(test_sink->lines()[1] == "test_source_location.cpp:18 Hello"); } -#endif + +//REQUIRE(oss.str() == std::string("test_source_location.cpp:20 Hello source location") + default_eol);