Support C++20 std::format as an alternative to fmtlib

This commit is contained in:
Charless Milette
2021-11-13 11:29:05 -05:00
parent ff9313e6dd
commit 44a4517e2b
34 changed files with 445 additions and 143 deletions

View File

@@ -166,7 +166,7 @@ TEST_CASE("to_file", "[async]")
require_message_count(TEST_FILENAME, messages);
auto contents = file_contents(TEST_FILENAME);
using spdlog::details::os::default_eol;
REQUIRE(ends_with(contents, fmt::format("Hello message #1023{}", default_eol)));
REQUIRE(ends_with(contents, spdlog::fmt_lib::format("Hello message #1023{}", default_eol)));
}
TEST_CASE("to_file multi-workers", "[async]")

View File

@@ -3,7 +3,11 @@
*/
#include "includes.h"
#ifdef SPDLOG_USE_STD_FORMAT
using filename_memory_buf_t = std::basic_string<spdlog::filename_t::value_type>;
#else
using filename_memory_buf_t = fmt::basic_memory_buffer<spdlog::filename_t::value_type, 250>;
#endif
TEST_CASE("daily_logger with dateonly calculator", "[daily_logger]")
{
@@ -15,7 +19,7 @@ TEST_CASE("daily_logger with dateonly calculator", "[daily_logger]")
spdlog::filename_t basename = SPDLOG_FILENAME_T("test_logs/daily_dateonly");
std::tm tm = spdlog::details::os::localtime();
filename_memory_buf_t w;
fmt::format_to(
spdlog::fmt_lib::format_to(
std::back_inserter(w), SPDLOG_FILENAME_T("{}_{:04d}-{:02d}-{:02d}"), basename, tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday);
auto logger = spdlog::create<sink_type>("logger", basename, 0, 0);
@@ -29,9 +33,17 @@ TEST_CASE("daily_logger with dateonly calculator", "[daily_logger]")
#ifdef SPDLOG_WCHAR_FILENAMES
spdlog::memory_buf_t buf;
spdlog::details::os::wstr_to_utf8buf(fmt::to_string(w), buf);
#ifdef SPDLOG_USE_STD_FORMAT
auto &filename = buf;
#else
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);
}
@@ -41,9 +53,13 @@ struct custom_daily_file_name_calculator
static spdlog::filename_t calc_filename(const spdlog::filename_t &basename, const tm &now_tm)
{
filename_memory_buf_t w;
fmt::format_to(std::back_inserter(w), SPDLOG_FILENAME_T("{}{:04d}{:02d}{:02d}"), basename, now_tm.tm_year + 1900, now_tm.tm_mon + 1,
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
}
};
@@ -57,7 +73,7 @@ TEST_CASE("daily_logger with custom calculator", "[daily_logger]")
spdlog::filename_t basename = SPDLOG_FILENAME_T("test_logs/daily_dateonly");
std::tm tm = spdlog::details::os::localtime();
filename_memory_buf_t w;
fmt::format_to(
spdlog::fmt_lib::format_to(
std::back_inserter(w), SPDLOG_FILENAME_T("{}{:04d}{:02d}{:02d}"), basename, tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday);
auto logger = spdlog::create<sink_type>("logger", basename, 0, 0);
@@ -71,9 +87,17 @@ TEST_CASE("daily_logger with custom calculator", "[daily_logger]")
#ifdef SPDLOG_WCHAR_FILENAMES
spdlog::memory_buf_t buf;
spdlog::details::os::wstr_to_utf8buf(fmt::to_string(w), buf);
#ifdef SPDLOG_USE_STD_FORMAT
auto &filename = buf;
#else
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);
}

View File

@@ -34,7 +34,7 @@ TEST_CASE("default_error_handler", "[errors]]")
logger->flush();
using spdlog::details::os::default_eol;
REQUIRE(file_contents(SIMPLE_LOG) == fmt::format("Test message 2{}", default_eol));
REQUIRE(file_contents(SIMPLE_LOG) == spdlog::fmt_lib::format("Test message 2{}", default_eol));
REQUIRE(count_lines(SIMPLE_LOG) == 1);
}

View File

@@ -10,7 +10,7 @@ using spdlog::details::file_helper;
static void write_with_helper(file_helper &helper, size_t howmany)
{
spdlog::memory_buf_t formatted;
fmt::format_to(std::back_inserter(formatted), "{}", std::string(howmany, '1'));
spdlog::fmt_lib::format_to(std::back_inserter(formatted), "{}", std::string(howmany, '1'));
helper.write(formatted);
helper.flush();
}

View File

@@ -8,28 +8,48 @@ 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(formatted == expected);
#else
REQUIRE(fmt::to_string(buf) == expected);
#endif
}
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(formatted == expected);
#else
REQUIRE(fmt::to_string(buf) == expected);
#endif
}
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(formatted == expected);
#else
REQUIRE(fmt::to_string(buf) == expected);
#endif
}
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(formatted == expected);
#else
REQUIRE(fmt::to_string(buf) == expected);
#endif
}
TEST_CASE("pad2", "[fmt_helper]")

View File

@@ -25,7 +25,7 @@ TEST_CASE("debug and trace w/o format string", "[macros]]")
logger->flush();
using spdlog::details::os::default_eol;
REQUIRE(ends_with(file_contents(TEST_FILENAME), fmt::format("Test message 2{}", default_eol)));
REQUIRE(ends_with(file_contents(TEST_FILENAME), spdlog::fmt_lib::format("Test message 2{}", default_eol)));
REQUIRE(count_lines(TEST_FILENAME) == 1);
spdlog::set_default_logger(logger);
@@ -35,7 +35,7 @@ TEST_CASE("debug and trace w/o format string", "[macros]]")
logger->flush();
require_message_count(TEST_FILENAME, 2);
REQUIRE(ends_with(file_contents(TEST_FILENAME), fmt::format("Test message 4{}", default_eol)));
REQUIRE(ends_with(file_contents(TEST_FILENAME), spdlog::fmt_lib::format("Test message 4{}", default_eol)));
}
TEST_CASE("disable param evaluation", "[macros]")

View File

@@ -64,7 +64,7 @@ TEST_CASE("color range test1", "[pattern_formatter]")
auto formatter = std::make_shared<spdlog::pattern_formatter>("%^%v%$", spdlog::pattern_time_type::local, "\n");
memory_buf_t buf;
fmt::format_to(std::back_inserter(buf), "Hello");
spdlog::fmt_lib::format_to(std::back_inserter(buf), "Hello");
memory_buf_t formatted;
std::string logger_name = "test";
spdlog::details::log_msg msg(logger_name, spdlog::level::info, spdlog::string_view_t(buf.data(), buf.size()));
@@ -273,7 +273,11 @@ 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
}
TEST_CASE("clone-default-formatter2", "[pattern_formatter]")
@@ -288,7 +292,11 @@ 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
}
TEST_CASE("clone-formatter", "[pattern_formatter]")
@@ -302,7 +310,12 @@ TEST_CASE("clone-formatter", "[pattern_formatter]")
memory_buf_t formatted_2;
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
}
TEST_CASE("clone-formatter-2", "[pattern_formatter]")
@@ -317,7 +330,12 @@ TEST_CASE("clone-formatter-2", "[pattern_formatter]")
memory_buf_t formatted_2;
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
}
class custom_test_flag : public spdlog::custom_flag_formatter
@@ -362,9 +380,15 @@ TEST_CASE("clone-custom_formatter", "[pattern_formatter]")
formatter_1->format(msg, formatted_1);
formatter_2->format(msg, formatted_2);
auto expected = fmt::format("[logger-name] [custom_output] some message{}", spdlog::details::os::default_eol);
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
}
//
@@ -385,7 +409,12 @@ TEST_CASE("short filename formatter-1", "[pattern_formatter]")
spdlog::source_loc source_loc{test_path, 123, "some_func()"};
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
}
TEST_CASE("short filename formatter-2", "[pattern_formatter]")
@@ -396,7 +425,12 @@ TEST_CASE("short filename formatter-2", "[pattern_formatter]")
spdlog::source_loc source_loc{"myfile.cpp", 123, "some_func()"};
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
}
TEST_CASE("short filename formatter-3", "[pattern_formatter]")
@@ -407,7 +441,12 @@ TEST_CASE("short filename formatter-3", "[pattern_formatter]")
spdlog::source_loc source_loc{"", 123, "some_func()"};
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
}
TEST_CASE("full filename formatter", "[pattern_formatter]")
@@ -418,7 +457,12 @@ TEST_CASE("full filename formatter", "[pattern_formatter]")
spdlog::source_loc source_loc{test_path, 123, "some_func()"};
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
}
TEST_CASE("custom flags", "[pattern_formatter]")
@@ -430,8 +474,13 @@ TEST_CASE("custom flags", "[pattern_formatter]")
spdlog::details::log_msg msg(spdlog::source_loc{}, "logger-name", spdlog::level::info, "some message");
formatter->format(msg, formatted);
auto expected = fmt::format("[logger-name] [custom1] [custom2] some message{}", spdlog::details::os::default_eol);
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
}
TEST_CASE("custom flags-padding", "[pattern_formatter]")
@@ -443,8 +492,13 @@ TEST_CASE("custom flags-padding", "[pattern_formatter]")
spdlog::details::log_msg msg(spdlog::source_loc{}, "logger-name", spdlog::level::info, "some message");
formatter->format(msg, formatted);
auto expected = fmt::format("[logger-name] [custom1] [ custom2] some message{}", spdlog::details::os::default_eol);
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
}
TEST_CASE("custom flags-exception", "[pattern_formatter]")