Switch to vformat_to

Drive-by: reduce the amount of occurences of #ifdef SPDLOG_USE_STD_FORMAT
This commit is contained in:
Charles Milette
2022-04-21 21:46:58 -04:00
parent b3ce5ed379
commit ebeb3707b1
10 changed files with 66 additions and 166 deletions

View File

@@ -5,8 +5,20 @@
#ifdef SPDLOG_USE_STD_FORMAT
using filename_memory_buf_t = std::basic_string<spdlog::filename_t::value_type>;
std::string filename_buf_to_utf8string(const filename_memory_buf_t &w)
{
spdlog::memory_buf_t buf;
spdlog::details::os::wstr_to_utf8buf(w, buf);
return spdlog::details::fmt_helper::to_string(buf);
}
#else
using filename_memory_buf_t = fmt::basic_memory_buffer<spdlog::filename_t::value_type, 250>;
std::string filename_buf_to_utf8string(const filename_memory_buf_t &w)
{
return spdlog::details::fmt_helper::to_string(w);
}
#endif
TEST_CASE("daily_logger with dateonly calculator", "[daily_logger]")
@@ -30,23 +42,7 @@ TEST_CASE("daily_logger with dateonly calculator", "[daily_logger]")
}
logger->flush();
#ifdef SPDLOG_WCHAR_FILENAMES
spdlog::memory_buf_t buf;
# ifdef SPDLOG_USE_STD_FORMAT
spdlog::details::os::wstr_to_utf8buf(w, buf);
auto &filename = buf;
# else
spdlog::details::os::wstr_to_utf8buf(fmt::to_string(w), buf);
auto filename = fmt::to_string(buf);
# endif
#else
# ifdef SPDLOG_USE_STD_FORMAT
auto &filename = w;
# else
auto filename = fmt::to_string(w);
# endif
#endif
require_message_count(filename, 10);
require_message_count(filename_buf_to_utf8string(w), 10);
}
struct custom_daily_file_name_calculator
@@ -56,11 +52,8 @@ struct custom_daily_file_name_calculator
filename_memory_buf_t w;
spdlog::fmt_lib::format_to(std::back_inserter(w), SPDLOG_FILENAME_T("{}{:04d}{:02d}{:02d}"), basename, now_tm.tm_year + 1900,
now_tm.tm_mon + 1, now_tm.tm_mday);
#ifdef SPDLOG_USE_STD_FORMAT
return w;
#else
return fmt::to_string(w);
#endif
return spdlog::details::fmt_helper::to_string(w);
}
};
@@ -85,23 +78,7 @@ TEST_CASE("daily_logger with custom calculator", "[daily_logger]")
logger->flush();
#ifdef SPDLOG_WCHAR_FILENAMES
spdlog::memory_buf_t buf;
# ifdef SPDLOG_USE_STD_FORMAT
spdlog::details::os::wstr_to_utf8buf(w, buf);
auto &filename = buf;
# else
spdlog::details::os::wstr_to_utf8buf(fmt::to_string(w), buf);
auto filename = fmt::to_string(buf);
# endif
#else
# ifdef SPDLOG_USE_STD_FORMAT
auto &filename = w;
# else
auto filename = fmt::to_string(w);
# endif
#endif
require_message_count(filename, 10);
require_message_count(filename_buf_to_utf8string(w), 10);
}
/*

View File

@@ -29,11 +29,7 @@ TEST_CASE("default_error_handler", "[errors]]")
auto logger = spdlog::create<spdlog::sinks::basic_file_sink_mt>("test-error", filename, true);
logger->set_pattern("%v");
#ifdef SPDLOG_USE_STD_FORMAT
logger->info("Test message {} {}", 1);
#else
logger->info(fmt::runtime("Test message {} {}"), 1);
#endif
logger->info(SPDLOG_FMT_RUNTIME("Test message {} {}"), 1);
logger->info("Test message {}", 2);
logger->flush();
@@ -53,11 +49,7 @@ TEST_CASE("custom_error_handler", "[errors]]")
logger->set_error_handler([=](const std::string &) { throw custom_ex(); });
logger->info("Good message #1");
#ifdef SPDLOG_USE_STD_FORMAT
REQUIRE_THROWS_AS(logger->info("Bad format msg {} {}", "xxx"), custom_ex);
#else
REQUIRE_THROWS_AS(logger->info(fmt::runtime("Bad format msg {} {}"), "xxx"), custom_ex);
#endif
REQUIRE_THROWS_AS(logger->info(SPDLOG_FMT_RUNTIME("Bad format msg {} {}"), "xxx"), custom_ex);
logger->info("Good message #2");
require_message_count(SIMPLE_LOG, 2);
}
@@ -96,11 +88,7 @@ TEST_CASE("async_error_handler", "[errors]]")
ofs << err_msg;
});
logger->info("Good message #1");
#ifdef SPDLOG_USE_STD_FORMAT
logger->info("Bad format msg {} {}", "xxx");
#else
logger->info(fmt::runtime("Bad format msg {} {}"), "xxx");
#endif
logger->info(SPDLOG_FMT_RUNTIME("Bad format msg {} {}"), "xxx");
logger->info("Good message #2");
spdlog::drop("logger"); // force logger to drain the queue and shutdown
}

View File

@@ -3,17 +3,14 @@
#include "spdlog/details/fmt_helper.h"
using spdlog::memory_buf_t;
using spdlog::details::fmt_helper::to_string_view;
void test_pad2(int n, const char *expected)
{
memory_buf_t buf;
spdlog::details::fmt_helper::pad2(n, buf);
#ifdef SPDLOG_USE_STD_FORMAT
REQUIRE(buf == expected);
#else
REQUIRE(fmt::to_string(buf) == expected);
#endif
REQUIRE(to_string_view(buf) == expected);
}
void test_pad3(uint32_t n, const char *expected)
@@ -21,11 +18,7 @@ void test_pad3(uint32_t n, const char *expected)
memory_buf_t buf;
spdlog::details::fmt_helper::pad3(n, buf);
#ifdef SPDLOG_USE_STD_FORMAT
REQUIRE(buf == expected);
#else
REQUIRE(fmt::to_string(buf) == expected);
#endif
REQUIRE(to_string_view(buf) == expected);
}
void test_pad6(std::size_t n, const char *expected)
@@ -33,11 +26,7 @@ void test_pad6(std::size_t n, const char *expected)
memory_buf_t buf;
spdlog::details::fmt_helper::pad6(n, buf);
#ifdef SPDLOG_USE_STD_FORMAT
REQUIRE(buf == expected);
#else
REQUIRE(fmt::to_string(buf) == expected);
#endif
REQUIRE(to_string_view(buf) == expected);
}
void test_pad9(std::size_t n, const char *expected)
@@ -45,11 +34,7 @@ void test_pad9(std::size_t n, const char *expected)
memory_buf_t buf;
spdlog::details::fmt_helper::pad9(n, buf);
#ifdef SPDLOG_USE_STD_FORMAT
REQUIRE(buf == expected);
#else
REQUIRE(fmt::to_string(buf) == expected);
#endif
REQUIRE(to_string_view(buf) == expected);
}
TEST_CASE("pad2", "[fmt_helper]")

View File

@@ -2,6 +2,7 @@
#include "test_sink.h"
using spdlog::memory_buf_t;
using spdlog::details::fmt_helper::to_string_view;
// log to str and return it
template<typename... Args>
@@ -273,11 +274,7 @@ TEST_CASE("clone-default-formatter", "[pattern_formatter]")
formatter_1->format(msg, formatted_1);
formatter_2->format(msg, formatted_2);
#ifdef SPDLOG_USE_STD_FORMAT
REQUIRE(formatted_1 == formatted_2);
#else
REQUIRE(fmt::to_string(formatted_1) == fmt::to_string(formatted_2));
#endif
REQUIRE(to_string_view(formatted_1) == to_string_view(formatted_2));
}
TEST_CASE("clone-default-formatter2", "[pattern_formatter]")
@@ -292,11 +289,7 @@ TEST_CASE("clone-default-formatter2", "[pattern_formatter]")
formatter_1->format(msg, formatted_1);
formatter_2->format(msg, formatted_2);
#ifdef SPDLOG_USE_STD_FORMAT
REQUIRE(formatted_1 == formatted_2);
#else
REQUIRE(fmt::to_string(formatted_1) == fmt::to_string(formatted_2));
#endif
REQUIRE(to_string_view(formatted_1) == to_string_view(formatted_2));
}
TEST_CASE("clone-formatter", "[pattern_formatter]")
@@ -311,11 +304,7 @@ TEST_CASE("clone-formatter", "[pattern_formatter]")
formatter_1->format(msg, formatted_1);
formatter_2->format(msg, formatted_2);
#ifdef SPDLOG_USE_STD_FORMAT
REQUIRE(formatted_1 == formatted_2);
#else
REQUIRE(fmt::to_string(formatted_1) == fmt::to_string(formatted_2));
#endif
REQUIRE(to_string_view(formatted_1) == to_string_view(formatted_2));
}
TEST_CASE("clone-formatter-2", "[pattern_formatter]")
@@ -331,11 +320,7 @@ TEST_CASE("clone-formatter-2", "[pattern_formatter]")
formatter_1->format(msg, formatted_1);
formatter_2->format(msg, formatted_2);
#ifdef SPDLOG_USE_STD_FORMAT
REQUIRE(formatted_1 == formatted_2);
#else
REQUIRE(fmt::to_string(formatted_1) == fmt::to_string(formatted_2));
#endif
REQUIRE(to_string_view(formatted_1) == to_string_view(formatted_2));
}
class custom_test_flag : public spdlog::custom_flag_formatter
@@ -382,13 +367,8 @@ TEST_CASE("clone-custom_formatter", "[pattern_formatter]")
auto expected = spdlog::fmt_lib::format("[logger-name] [custom_output] some message{}", spdlog::details::os::default_eol);
#ifdef SPDLOG_USE_STD_FORMAT
REQUIRE(formatted_1 == expected);
REQUIRE(formatted_2 == expected);
#else
REQUIRE(fmt::to_string(formatted_1) == expected);
REQUIRE(fmt::to_string(formatted_2) == expected);
#endif
REQUIRE(to_string_view(formatted_1) == expected);
REQUIRE(to_string_view(formatted_2) == expected);
}
//
@@ -410,11 +390,7 @@ TEST_CASE("short filename formatter-1", "[pattern_formatter]")
spdlog::details::log_msg msg(source_loc, "logger-name", spdlog::level::info, "Hello");
formatter.format(msg, formatted);
#ifdef SPDLOG_USE_STD_FORMAT
REQUIRE(formatted == "myfile.cpp");
#else
REQUIRE(fmt::to_string(formatted) == "myfile.cpp");
#endif
REQUIRE(to_string_view(formatted) == "myfile.cpp");
}
TEST_CASE("short filename formatter-2", "[pattern_formatter]")
@@ -426,11 +402,7 @@ TEST_CASE("short filename formatter-2", "[pattern_formatter]")
spdlog::details::log_msg msg(source_loc, "logger-name", spdlog::level::info, "Hello");
formatter.format(msg, formatted);
#ifdef SPDLOG_USE_STD_FORMAT
REQUIRE(formatted == "myfile.cpp:123");
#else
REQUIRE(fmt::to_string(formatted) == "myfile.cpp:123");
#endif
REQUIRE(to_string_view(formatted) == "myfile.cpp:123");
}
TEST_CASE("short filename formatter-3", "[pattern_formatter]")
@@ -442,11 +414,7 @@ TEST_CASE("short filename formatter-3", "[pattern_formatter]")
spdlog::details::log_msg msg(source_loc, "logger-name", spdlog::level::info, "Hello");
formatter.format(msg, formatted);
#ifdef SPDLOG_USE_STD_FORMAT
REQUIRE(formatted == " Hello");
#else
REQUIRE(fmt::to_string(formatted) == " Hello");
#endif
REQUIRE(to_string_view(formatted) == " Hello");
}
TEST_CASE("full filename formatter", "[pattern_formatter]")
@@ -458,11 +426,7 @@ TEST_CASE("full filename formatter", "[pattern_formatter]")
spdlog::details::log_msg msg(source_loc, "logger-name", spdlog::level::info, "Hello");
formatter.format(msg, formatted);
#ifdef SPDLOG_USE_STD_FORMAT
REQUIRE(formatted == test_path);
#else
REQUIRE(fmt::to_string(formatted) == test_path);
#endif
REQUIRE(to_string_view(formatted) == test_path);
}
TEST_CASE("custom flags", "[pattern_formatter]")
@@ -476,11 +440,7 @@ TEST_CASE("custom flags", "[pattern_formatter]")
formatter->format(msg, formatted);
auto expected = spdlog::fmt_lib::format("[logger-name] [custom1] [custom2] some message{}", spdlog::details::os::default_eol);
#ifdef SPDLOG_USE_STD_FORMAT
REQUIRE(formatted == expected);
#else
REQUIRE(fmt::to_string(formatted) == expected);
#endif
REQUIRE(to_string_view(formatted) == expected);
}
TEST_CASE("custom flags-padding", "[pattern_formatter]")
@@ -494,11 +454,7 @@ TEST_CASE("custom flags-padding", "[pattern_formatter]")
formatter->format(msg, formatted);
auto expected = spdlog::fmt_lib::format("[logger-name] [custom1] [ custom2] some message{}", spdlog::details::os::default_eol);
#ifdef SPDLOG_USE_STD_FORMAT
REQUIRE(formatted == expected);
#else
REQUIRE(fmt::to_string(formatted) == expected);
#endif
REQUIRE(to_string_view(formatted) == expected);
}
TEST_CASE("custom flags-exception", "[pattern_formatter]")