mirror of
https://github.com/gabime/spdlog.git
synced 2024-11-15 08:25:43 +08:00
Fixed #2724 by excluding bin_to_hex sink if using std::format
This commit is contained in:
parent
bed324e414
commit
7e635fca68
@ -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<my_type> : fmt::formatter<std::string>
|
||||
template<>
|
||||
struct std::formatter<my_type> : std::formatter<std::string>
|
||||
{
|
||||
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);
|
||||
}
|
||||
|
@ -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)
|
||||
|
93
tests/test_bin_to_hex.cpp
Normal file
93
tests/test_bin_to_hex.cpp
Normal file
@ -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<spdlog::sinks::ostream_sink_mt>(oss);
|
||||
spdlog::logger oss_logger("oss", oss_sink);
|
||||
|
||||
std::vector<unsigned char> 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<spdlog::sinks::ostream_sink_mt>(oss);
|
||||
spdlog::logger oss_logger("oss", oss_sink);
|
||||
|
||||
std::vector<unsigned char> 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<spdlog::sinks::ostream_sink_mt>(oss);
|
||||
spdlog::logger oss_logger("oss", oss_sink);
|
||||
|
||||
std::vector<unsigned char> 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<spdlog::sinks::ostream_sink_mt>(oss);
|
||||
spdlog::logger oss_logger("oss", oss_sink);
|
||||
|
||||
std::vector<unsigned char> 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<spdlog::sinks::ostream_sink_mt>(oss);
|
||||
spdlog::logger oss_logger("oss", oss_sink);
|
||||
|
||||
std::vector<unsigned char> 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<spdlog::sinks::ostream_sink_mt>(oss);
|
||||
spdlog::logger oss_logger("oss", oss_sink);
|
||||
|
||||
std::vector<unsigned char> 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)));
|
||||
}
|
@ -1,6 +1,5 @@
|
||||
#include "includes.h"
|
||||
#include "test_sink.h"
|
||||
#include "spdlog/fmt/bin_to_hex.h"
|
||||
|
||||
template<class T>
|
||||
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<spdlog::sinks::ostream_sink_mt>(oss);
|
||||
spdlog::logger oss_logger("oss", oss_sink);
|
||||
|
||||
std::vector<unsigned char> 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<spdlog::sinks::ostream_sink_mt>(oss);
|
||||
spdlog::logger oss_logger("oss", oss_sink);
|
||||
|
||||
std::vector<unsigned char> 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<spdlog::sinks::ostream_sink_mt>(oss);
|
||||
spdlog::logger oss_logger("oss", oss_sink);
|
||||
|
||||
std::vector<unsigned char> 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<spdlog::sinks::ostream_sink_mt>(oss);
|
||||
spdlog::logger oss_logger("oss", oss_sink);
|
||||
|
||||
std::vector<unsigned char> 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<spdlog::sinks::ostream_sink_mt>(oss);
|
||||
spdlog::logger oss_logger("oss", oss_sink);
|
||||
|
||||
std::vector<unsigned char> 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<spdlog::sinks::ostream_sink_mt>(oss);
|
||||
spdlog::logger oss_logger("oss", oss_sink);
|
||||
|
||||
std::vector<unsigned char> 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;
|
||||
|
Loading…
Reference in New Issue
Block a user