From 7e635fca68d014934b4af8a1cf874f63989352b7 Mon Sep 17 00:00:00 2001 From: gabime Date: Sat, 8 Jul 2023 17:12:25 +0300 Subject: [PATCH] Fixed #2724 by excluding bin_to_hex sink if using std::format --- example/example.cpp | 9 +++- tests/CMakeLists.txt | 4 ++ tests/test_bin_to_hex.cpp | 93 +++++++++++++++++++++++++++++++++++++++ tests/test_misc.cpp | 91 -------------------------------------- 4 files changed, 105 insertions(+), 92 deletions(-) create mode 100644 tests/test_bin_to_hex.cpp diff --git a/example/example.cpp b/example/example.cpp index ec31baaa..d6609ed5 100644 --- a/example/example.cpp +++ b/example/example.cpp @@ -31,6 +31,7 @@ void replace_default_logger_example(); #include "spdlog/cfg/env.h" // support for loading levels from the environment variable #include "spdlog/fmt/ostr.h" // support for user defined types + int main(int, char *[]) { // Log levels can be loaded from argv/env using "SPDLOG_LEVEL" @@ -183,6 +184,7 @@ void async_example() // {:p} - don't print the position on each line start. // {:n} - don't split the output to lines. +#if !defined SPDLOG_USE_STD_FORMAT || defined(_MSC_VER) #include "spdlog/fmt/bin_to_hex.h" void binary_example() { @@ -200,6 +202,11 @@ void binary_example() // logger->info("hexdump style: {:a}", spdlog::to_hex(buf)); // logger->info("hexdump style, 20 chars per line {:a}", spdlog::to_hex(buf, 20)); } +#else +void binary_example() { + // not supported with std::format yet +} +#endif // Log a vector of numbers #ifndef SPDLOG_USE_STD_FORMAT @@ -287,7 +294,7 @@ struct fmt::formatter : fmt::formatter template<> struct std::formatter : std::formatter { - auto format(my_type my, format_context &ctx) -> decltype(ctx.out()) + auto format(my_type my, format_context &ctx) const -> decltype(ctx.out()) { return format_to(ctx.out(), "[my_type i={}]", my.i); } diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 942ec519..176578ad 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -54,6 +54,10 @@ if(systemd_FOUND) list(APPEND SPDLOG_UTESTS_SOURCES test_systemd.cpp) endif() +if(NOT SPDLOG_USE_STD_FORMAT) + list(APPEND SPDLOG_UTESTS_SOURCES test_bin_to_hex.cpp) +endif() + enable_testing() function(spdlog_prepare_test test_target spdlog_lib) diff --git a/tests/test_bin_to_hex.cpp b/tests/test_bin_to_hex.cpp new file mode 100644 index 00000000..3c50c74a --- /dev/null +++ b/tests/test_bin_to_hex.cpp @@ -0,0 +1,93 @@ +#include "includes.h" +#include "test_sink.h" +#include "spdlog/fmt/bin_to_hex.h" + +TEST_CASE("to_hex", "[to_hex]") +{ + std::ostringstream oss; + auto oss_sink = std::make_shared(oss); + spdlog::logger oss_logger("oss", oss_sink); + + std::vector v{9, 0xa, 0xb, 0xc, 0xff, 0xff}; + oss_logger.info("{}", spdlog::to_hex(v)); + + auto output = oss.str(); + REQUIRE(ends_with(output, "0000: 09 0a 0b 0c ff ff" + std::string(spdlog::details::os::default_eol))); +} + +TEST_CASE("to_hex_upper", "[to_hex]") +{ + std::ostringstream oss; + auto oss_sink = std::make_shared(oss); + spdlog::logger oss_logger("oss", oss_sink); + + std::vector v{9, 0xa, 0xb, 0xc, 0xff, 0xff}; + oss_logger.info("{:X}", spdlog::to_hex(v)); + + auto output = oss.str(); + REQUIRE(ends_with(output, "0000: 09 0A 0B 0C FF FF" + std::string(spdlog::details::os::default_eol))); +} + +TEST_CASE("to_hex_no_delimiter", "[to_hex]") +{ + std::ostringstream oss; + auto oss_sink = std::make_shared(oss); + spdlog::logger oss_logger("oss", oss_sink); + + std::vector v{9, 0xa, 0xb, 0xc, 0xff, 0xff}; + oss_logger.info("{:sX}", spdlog::to_hex(v)); + + auto output = oss.str(); + REQUIRE(ends_with(output, "0000: 090A0B0CFFFF" + std::string(spdlog::details::os::default_eol))); +} + +TEST_CASE("to_hex_show_ascii", "[to_hex]") +{ + std::ostringstream oss; + auto oss_sink = std::make_shared(oss); + spdlog::logger oss_logger("oss", oss_sink); + + std::vector v{9, 0xa, 0xb, 0x41, 0xc, 0x4b, 0xff, 0xff}; + oss_logger.info("{:Xsa}", spdlog::to_hex(v, 8)); + + REQUIRE(ends_with(oss.str(), "0000: 090A0B410C4BFFFF ...A.K.." + std::string(spdlog::details::os::default_eol))); +} + +TEST_CASE("to_hex_different_size_per_line", "[to_hex]") +{ + std::ostringstream oss; + auto oss_sink = std::make_shared(oss); + spdlog::logger oss_logger("oss", oss_sink); + + std::vector v{9, 0xa, 0xb, 0x41, 0xc, 0x4b, 0xff, 0xff}; + + oss_logger.info("{:Xsa}", spdlog::to_hex(v, 10)); + REQUIRE(ends_with(oss.str(), "0000: 090A0B410C4BFFFF ...A.K.." + std::string(spdlog::details::os::default_eol))); + + oss_logger.info("{:Xs}", spdlog::to_hex(v, 10)); + REQUIRE(ends_with(oss.str(), "0000: 090A0B410C4BFFFF" + std::string(spdlog::details::os::default_eol))); + + oss_logger.info("{:Xsa}", spdlog::to_hex(v, 6)); + REQUIRE(ends_with(oss.str(), "0000: 090A0B410C4B ...A.K" + std::string(spdlog::details::os::default_eol) + "0006: FFFF .." + + std::string(spdlog::details::os::default_eol))); + + oss_logger.info("{:Xs}", spdlog::to_hex(v, 6)); + REQUIRE(ends_with(oss.str(), "0000: 090A0B410C4B" + std::string(spdlog::details::os::default_eol) + "0006: FFFF" + + std::string(spdlog::details::os::default_eol))); +} + +TEST_CASE("to_hex_no_ascii", "[to_hex]") +{ + std::ostringstream oss; + auto oss_sink = std::make_shared(oss); + spdlog::logger oss_logger("oss", oss_sink); + + std::vector v{9, 0xa, 0xb, 0x41, 0xc, 0x4b, 0xff, 0xff}; + oss_logger.info("{:Xs}", spdlog::to_hex(v, 8)); + + REQUIRE(ends_with(oss.str(), "0000: 090A0B410C4BFFFF" + std::string(spdlog::details::os::default_eol))); + + oss_logger.info("{:Xsna}", spdlog::to_hex(v, 8)); + + REQUIRE(ends_with(oss.str(), "090A0B410C4BFFFF" + std::string(spdlog::details::os::default_eol))); +} diff --git a/tests/test_misc.cpp b/tests/test_misc.cpp index b6807154..9f3cb174 100644 --- a/tests/test_misc.cpp +++ b/tests/test_misc.cpp @@ -1,6 +1,5 @@ #include "includes.h" #include "test_sink.h" -#include "spdlog/fmt/bin_to_hex.h" template std::string log_info(const T &what, spdlog::level::level_enum logger_level = spdlog::level::info) @@ -140,96 +139,6 @@ TEST_CASE("clone async", "[clone]") spdlog::drop_all(); } -TEST_CASE("to_hex", "[to_hex]") -{ - std::ostringstream oss; - auto oss_sink = std::make_shared(oss); - spdlog::logger oss_logger("oss", oss_sink); - - std::vector v{9, 0xa, 0xb, 0xc, 0xff, 0xff}; - oss_logger.info("{}", spdlog::to_hex(v)); - - auto output = oss.str(); - REQUIRE(ends_with(output, "0000: 09 0a 0b 0c ff ff" + std::string(spdlog::details::os::default_eol))); -} - -TEST_CASE("to_hex_upper", "[to_hex]") -{ - std::ostringstream oss; - auto oss_sink = std::make_shared(oss); - spdlog::logger oss_logger("oss", oss_sink); - - std::vector v{9, 0xa, 0xb, 0xc, 0xff, 0xff}; - oss_logger.info("{:X}", spdlog::to_hex(v)); - - auto output = oss.str(); - REQUIRE(ends_with(output, "0000: 09 0A 0B 0C FF FF" + std::string(spdlog::details::os::default_eol))); -} - -TEST_CASE("to_hex_no_delimiter", "[to_hex]") -{ - std::ostringstream oss; - auto oss_sink = std::make_shared(oss); - spdlog::logger oss_logger("oss", oss_sink); - - std::vector v{9, 0xa, 0xb, 0xc, 0xff, 0xff}; - oss_logger.info("{:sX}", spdlog::to_hex(v)); - - auto output = oss.str(); - REQUIRE(ends_with(output, "0000: 090A0B0CFFFF" + std::string(spdlog::details::os::default_eol))); -} - -TEST_CASE("to_hex_show_ascii", "[to_hex]") -{ - std::ostringstream oss; - auto oss_sink = std::make_shared(oss); - spdlog::logger oss_logger("oss", oss_sink); - - std::vector v{9, 0xa, 0xb, 0x41, 0xc, 0x4b, 0xff, 0xff}; - oss_logger.info("{:Xsa}", spdlog::to_hex(v, 8)); - - REQUIRE(ends_with(oss.str(), "0000: 090A0B410C4BFFFF ...A.K.." + std::string(spdlog::details::os::default_eol))); -} - -TEST_CASE("to_hex_different_size_per_line", "[to_hex]") -{ - std::ostringstream oss; - auto oss_sink = std::make_shared(oss); - spdlog::logger oss_logger("oss", oss_sink); - - std::vector v{9, 0xa, 0xb, 0x41, 0xc, 0x4b, 0xff, 0xff}; - - oss_logger.info("{:Xsa}", spdlog::to_hex(v, 10)); - REQUIRE(ends_with(oss.str(), "0000: 090A0B410C4BFFFF ...A.K.." + std::string(spdlog::details::os::default_eol))); - - oss_logger.info("{:Xs}", spdlog::to_hex(v, 10)); - REQUIRE(ends_with(oss.str(), "0000: 090A0B410C4BFFFF" + std::string(spdlog::details::os::default_eol))); - - oss_logger.info("{:Xsa}", spdlog::to_hex(v, 6)); - REQUIRE(ends_with(oss.str(), "0000: 090A0B410C4B ...A.K" + std::string(spdlog::details::os::default_eol) + "0006: FFFF .." + - std::string(spdlog::details::os::default_eol))); - - oss_logger.info("{:Xs}", spdlog::to_hex(v, 6)); - REQUIRE(ends_with(oss.str(), "0000: 090A0B410C4B" + std::string(spdlog::details::os::default_eol) + "0006: FFFF" + - std::string(spdlog::details::os::default_eol))); -} - -TEST_CASE("to_hex_no_ascii", "[to_hex]") -{ - std::ostringstream oss; - auto oss_sink = std::make_shared(oss); - spdlog::logger oss_logger("oss", oss_sink); - - std::vector v{9, 0xa, 0xb, 0x41, 0xc, 0x4b, 0xff, 0xff}; - oss_logger.info("{:Xs}", spdlog::to_hex(v, 8)); - - REQUIRE(ends_with(oss.str(), "0000: 090A0B410C4BFFFF" + std::string(spdlog::details::os::default_eol))); - - oss_logger.info("{:Xsna}", spdlog::to_hex(v, 8)); - - REQUIRE(ends_with(oss.str(), "090A0B410C4BFFFF" + std::string(spdlog::details::os::default_eol))); -} - TEST_CASE("default logger API", "[default logger]") { std::ostringstream oss;