From 9804306f1389a4cfa95848a49d7ccbbc1862dcab Mon Sep 17 00:00:00 2001 From: Danny Robson Date: Sun, 19 Nov 2023 09:38:07 +1000 Subject: [PATCH] log: use format_string to pass format specifiers --- log/log.hpp | 13 +++---- log/packet.hpp | 9 ++--- log/scoped.cpp | 2 +- tap.hpp | 88 ++++++++++++++++++++++++---------------------- test/list/sort.cpp | 4 +-- 5 files changed, 59 insertions(+), 57 deletions(-) diff --git a/log/log.hpp b/log/log.hpp index 94bb6f77..4dcc1be9 100644 --- a/log/log.hpp +++ b/log/log.hpp @@ -28,15 +28,12 @@ namespace cruft::log { void write (level_t, std::string_view msg); - //------------------------------------------------------------------------- - template - void - write (level_t l, FormatT fmt, ArgsT &&...args) - { + template + void write (level_t _level, fmt::format_string fmt, ArgsT&&... args) { default_sink ().write ( packet ( - l, - std::forward (fmt), + _level, + std::forward> (fmt), std::forward (args)... ) ); @@ -58,7 +55,7 @@ namespace cruft::log { do { \ ::cruft::log::write ( \ (LEVEL), \ - FMT_STRING(FMT) \ + (FMT) \ __VA_OPT__(,) \ __VA_ARGS__ \ ); \ diff --git a/log/packet.hpp b/log/packet.hpp index 311a8522..e63885bd 100644 --- a/log/packet.hpp +++ b/log/packet.hpp @@ -36,15 +36,16 @@ namespace cruft::log { ) { ; } - template - packet ( + + template + constexpr packet ( level_t _level, - FormatT _format, + fmt::format_string &&_format, ArgsT &&..._args ) : packet ( _level, fmt::format ( - std::forward (_format), + std::forward> (_format), std::forward (_args)... ) ) diff --git a/log/scoped.cpp b/log/scoped.cpp index c196de3e..854ff155 100644 --- a/log/scoped.cpp +++ b/log/scoped.cpp @@ -50,7 +50,7 @@ cruft::log::scoped_timer::~scoped_timer () write ( m_level, - FMT_STRING("{:f}s, {:s}"), + "{:f}s, {:s}", float (duration) / 1'000'000'000.f, m_message ); diff --git a/tap.hpp b/tap.hpp index 0e282bdc..d7d9b72e 100644 --- a/tap.hpp +++ b/tap.hpp @@ -48,13 +48,13 @@ namespace cruft::TAP { //--------------------------------------------------------------------- - template + template bool - expect (const bool test, const char (&format)[N], Args&&... args) + expect (const bool test, fmt::format_string format, Args&&... args) { m_output << (test ? "ok " : "not ok ") << ++m_size << " - "; - fmt::vprint (m_output, format, fmt::make_format_args (std::forward (args)...)); + fmt::print (m_output, format, std::forward (args)...); m_output << std::endl; if (!test) @@ -64,12 +64,12 @@ namespace cruft::TAP { //--------------------------------------------------------------------- - template + template decltype(auto) - expect (const std::function &test, Args&&...args, const char (&fmt)[N]) + expect (const std::function &test, Args&&...args, fmt::format_string fmt) { try { - return expect (test (std::forward (args)...), fmt); + return expect (test (std::forward (args)...), std::move (fmt)); } catch (...) { return expect (false, fmt); } @@ -77,36 +77,40 @@ namespace cruft::TAP { /////////////////////////////////////////////////////////////////////// - template + template decltype(auto) - expect_valid (ValueT &&value, char const (&fmt)[N], ArgsT &&...args) + expect_valid (ValueT &&value, fmt::format_string fmt, ArgsT &&...args) { return expect ( debug::is_valid ( std::forward (value) ), - fmt, + std::move (fmt), std::forward (args)... ); } /////////////////////////////////////////////////////////////////////// - template + template decltype(auto) - expect_mod (ValueA &&a, ValueB &&b, const char (&fmt)[N], Args &&...args) + expect_mod (ValueA &&a, ValueB &&b, fmt::format_string fmt, Args &&...args) { - return expect (a % b == 0, fmt, std::forward (args)...); + return expect ( + a % b == 0, + std::move (fmt), + std::forward (args)... + ); } /////////////////////////////////////////////////////////////////////// - template + template decltype(auto) - expect_eq (const T &a, const U &b, const char (&fmt)[N], Args&&...args) + expect_eq (const T &a, const U &b, fmt::format_string fmt, Args&&...args) { #if 1 - return expect (almost_equal (a, b), fmt, std::forward (args)...); + return expect (almost_equal (a, b), std::move (fmt), std::forward (args)...); #else if (almost_equal (a, b)) return expect (true, fmt, std::forward (args)...); @@ -117,63 +121,63 @@ namespace cruft::TAP { //--------------------------------------------------------------------- - template + template decltype(auto) - expect_neq (const T &a, const U &b, const char (&fmt)[N], Args&&...args) + expect_neq (const T &a, const U &b, fmt::format_string fmt, Args&&...args) { - return expect (!almost_equal (a, b), fmt, std::forward (args)...); + return expect (!almost_equal (a, b), std::move (fmt), std::forward (args)...); } /////////////////////////////////////////////////////////////////////// - template + template decltype(auto) - expect_gt (const ValueA &a, const ValueB &b, const char (&fmt)[N], Args&&...args) + expect_gt (const ValueA &a, const ValueB &b, fmt::format_string fmt, Args&&...args) { - return expect (a > b, fmt, std::forward (args)...); + return expect (a > b, std::move (fmt), std::forward (args)...); } //--------------------------------------------------------------------- - template + template decltype(auto) - expect_ge (const T &a, const U &b, const char (&fmt)[N], Args&&...args) + expect_ge (const T &a, const U &b, fmt::format_string fmt, Args&&...args) { - return expect (a >= b, fmt, std::forward (args)...); + return expect (a >= b, std::move (fmt), std::forward (args)...); } //--------------------------------------------------------------------- - template + template decltype(auto) - expect_lt (const T &a, const U &b, const char (&fmt)[N], Args&&...args) + expect_lt (const T &a, const U &b, fmt::format_string fmt, Args&&...args) { - return expect (a < b, fmt, std::forward (args)...); + return expect (a < b, std::move (fmt), std::forward (args)...); } //--------------------------------------------------------------------- - template + template decltype(auto) - expect_le (const T &a, const U &b, const char (&fmt)[N], Args&&...args) + expect_le (const T &a, const U &b, fmt::format_string fmt, Args&&...args) { - return expect (a <= b, fmt, std::forward (args)...); + return expect (a <= b, std::move (fmt), std::forward (args)...); } /////////////////////////////////////////////////////////////////////// - template + template decltype(auto) - expect_nan (const T &t, const char (&fmt)[N], Args&&...args) + expect_nan (const T &t, fmt::format_string fmt, Args&&...args) { - return expect (std::isnan (t), fmt, std::forward (args)...); + return expect (std::isnan (t), std::move (fmt), std::forward (args)...); } /////////////////////////////////////////////////////////////////////// - template + template decltype(auto) - expect_nothrow (T &&t, const char (&fmt)[N], Args&&...args) + expect_nothrow (T &&t, fmt::format_string fmt, Args&&...args) { bool success = true; @@ -183,14 +187,14 @@ namespace cruft::TAP { success = false; } - return expect (success, fmt, std::forward (args)...); + return expect (success, std::move (fmt), std::forward (args)...); } //--------------------------------------------------------------------- - template + template decltype(auto) - expect_throw (T &&t, const char (&fmt)[N], Args&&...args) + expect_throw (T &&t, fmt::format_string fmt, Args&&...args) { bool success = false; @@ -202,14 +206,14 @@ namespace cruft::TAP { success = false; } - return expect (success, fmt, std::forward (args)...); + return expect (success, std::move (fmt), std::forward (args)...); } /////////////////////////////////////////////////////////////////////// - template + template decltype(auto) - fail (const char (&fmt)[N], Args &&...args) + fail (fmt::format_string fmt, Args &&...args) { return expect (false, fmt, std::forward (args)...); } @@ -245,7 +249,7 @@ namespace cruft::TAP { } catch (std::exception const &err) { tap.fail ("no exceptions: {:s}", err.what ()); } catch (cruft::error const &err) { - tap.fail ("no exceptions: {:s}", err); + tap.fail ("no exceptions: {}", err); } catch (...) { tap.fail ("no exceptions"); } diff --git a/test/list/sort.cpp b/test/list/sort.cpp index c1c1387c..b09a2a5f 100644 --- a/test/list/sort.cpp +++ b/test/list/sort.cpp @@ -18,12 +18,12 @@ /////////////////////////////////////////////////////////////////////////////// -template +template static void test_sort ( cruft::TAP::logger &tap, std::vector &&data, - char const (&fmt)[N], + fmt::format_string fmt, ArgsT&&...args ) { // Pre-allocate the array so that we don't get caught by pointer