log: use format_string to pass format specifiers

This commit is contained in:
Danny Robson 2023-11-19 09:38:07 +10:00
parent 6f17da5e59
commit 9804306f13
5 changed files with 59 additions and 57 deletions

View File

@ -28,15 +28,12 @@ namespace cruft::log {
void write (level_t, std::string_view msg);
//-------------------------------------------------------------------------
template <typename FormatT, typename ...ArgsT>
void
write (level_t l, FormatT fmt, ArgsT &&...args)
{
template <typename... ArgsT>
void write (level_t _level, fmt::format_string<ArgsT...> fmt, ArgsT&&... args) {
default_sink ().write (
packet (
l,
std::forward<FormatT> (fmt),
_level,
std::forward<fmt::format_string<ArgsT...>> (fmt),
std::forward<ArgsT> (args)...
)
);
@ -58,7 +55,7 @@ namespace cruft::log {
do { \
::cruft::log::write ( \
(LEVEL), \
FMT_STRING(FMT) \
(FMT) \
__VA_OPT__(,) \
__VA_ARGS__ \
); \

View File

@ -36,15 +36,16 @@ namespace cruft::log {
)
{ ; }
template <typename FormatT, typename ...ArgsT>
packet (
template <typename ...ArgsT>
constexpr packet (
level_t _level,
FormatT _format,
fmt::format_string<ArgsT...> &&_format,
ArgsT &&..._args
) : packet (
_level,
fmt::format (
std::forward<FormatT> (_format),
std::forward<fmt::format_string<ArgsT...>> (_format),
std::forward<ArgsT> (_args)...
)
)

View File

@ -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
);

88
tap.hpp
View File

@ -48,13 +48,13 @@ namespace cruft::TAP {
//---------------------------------------------------------------------
template <typename ...Args, size_t N>
template <typename ...Args>
bool
expect (const bool test, const char (&format)[N], Args&&... args)
expect (const bool test, fmt::format_string<Args...> format, Args&&... args)
{
m_output << (test ? "ok " : "not ok ") << ++m_size
<< " - ";
fmt::vprint (m_output, format, fmt::make_format_args (std::forward<Args> (args)...));
fmt::print (m_output, format, std::forward<Args> (args)...);
m_output << std::endl;
if (!test)
@ -64,12 +64,12 @@ namespace cruft::TAP {
//---------------------------------------------------------------------
template <typename ...Args, size_t N>
template <typename ...Args>
decltype(auto)
expect (const std::function<bool(Args...)> &test, Args&&...args, const char (&fmt)[N])
expect (const std::function<bool(Args...)> &test, Args&&...args, fmt::format_string<Args...> fmt)
{
try {
return expect (test (std::forward<Args> (args)...), fmt);
return expect (test (std::forward<Args> (args)...), std::move (fmt));
} catch (...) {
return expect (false, fmt);
}
@ -77,36 +77,40 @@ namespace cruft::TAP {
///////////////////////////////////////////////////////////////////////
template <typename ValueT, size_t N, typename ...ArgsT>
template <typename ValueT, typename ...ArgsT>
decltype(auto)
expect_valid (ValueT &&value, char const (&fmt)[N], ArgsT &&...args)
expect_valid (ValueT &&value, fmt::format_string<ArgsT...> fmt, ArgsT &&...args)
{
return expect (
debug::is_valid (
std::forward<ValueT> (value)
),
fmt,
std::move (fmt),
std::forward<ArgsT> (args)...
);
}
///////////////////////////////////////////////////////////////////////
template <typename ValueA, typename ValueB, std::size_t N, typename ...Args>
template <typename ValueA, typename ValueB, typename ...Args>
decltype(auto)
expect_mod (ValueA &&a, ValueB &&b, const char (&fmt)[N], Args &&...args)
expect_mod (ValueA &&a, ValueB &&b, fmt::format_string<Args...> fmt, Args &&...args)
{
return expect (a % b == 0, fmt, std::forward<Args> (args)...);
return expect (
a % b == 0,
std::move (fmt),
std::forward<Args> (args)...
);
}
///////////////////////////////////////////////////////////////////////
template <typename T, typename U, typename ...Args, size_t N>
template <typename T, typename U, typename ...Args>
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<Args...> fmt, Args&&...args)
{
#if 1
return expect (almost_equal (a, b), fmt, std::forward<Args> (args)...);
return expect (almost_equal (a, b), std::move (fmt), std::forward<Args> (args)...);
#else
if (almost_equal (a, b))
return expect (true, fmt, std::forward<Args> (args)...);
@ -117,63 +121,63 @@ namespace cruft::TAP {
//---------------------------------------------------------------------
template <typename T, typename U, typename ...Args, size_t N>
template <typename T, typename U, typename ...Args>
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<Args...> fmt, Args&&...args)
{
return expect (!almost_equal (a, b), fmt, std::forward<Args> (args)...);
return expect (!almost_equal (a, b), std::move (fmt), std::forward<Args> (args)...);
}
///////////////////////////////////////////////////////////////////////
template <typename ValueA, typename ValueB, typename ...Args, size_t N>
template <typename ValueA, typename ValueB, typename ...Args>
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<Args...> fmt, Args&&...args)
{
return expect (a > b, fmt, std::forward<Args> (args)...);
return expect (a > b, std::move (fmt), std::forward<Args> (args)...);
}
//---------------------------------------------------------------------
template <typename T, typename U, typename ...Args, size_t N>
template <typename T, typename U, typename ...Args>
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<Args...> fmt, Args&&...args)
{
return expect (a >= b, fmt, std::forward<Args> (args)...);
return expect (a >= b, std::move (fmt), std::forward<Args> (args)...);
}
//---------------------------------------------------------------------
template <typename T, typename U, typename ...Args, size_t N>
template <typename T, typename U, typename ...Args>
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<Args...> fmt, Args&&...args)
{
return expect (a < b, fmt, std::forward<Args> (args)...);
return expect (a < b, std::move (fmt), std::forward<Args> (args)...);
}
//---------------------------------------------------------------------
template <typename T, typename U, typename ...Args, size_t N>
template <typename T, typename U, typename ...Args>
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<Args...> fmt, Args&&...args)
{
return expect (a <= b, fmt, std::forward<Args> (args)...);
return expect (a <= b, std::move (fmt), std::forward<Args> (args)...);
}
///////////////////////////////////////////////////////////////////////
template <typename T, typename ...Args, size_t N>
template <typename T, typename ...Args>
decltype(auto)
expect_nan (const T &t, const char (&fmt)[N], Args&&...args)
expect_nan (const T &t, fmt::format_string<Args...> fmt, Args&&...args)
{
return expect (std::isnan (t), fmt, std::forward<Args> (args)...);
return expect (std::isnan (t), std::move (fmt), std::forward<Args> (args)...);
}
///////////////////////////////////////////////////////////////////////
template <typename T, typename ...Args, size_t N>
template <typename T, typename ...Args>
decltype(auto)
expect_nothrow (T &&t, const char (&fmt)[N], Args&&...args)
expect_nothrow (T &&t, fmt::format_string<Args...> fmt, Args&&...args)
{
bool success = true;
@ -183,14 +187,14 @@ namespace cruft::TAP {
success = false;
}
return expect (success, fmt, std::forward<Args> (args)...);
return expect (success, std::move (fmt), std::forward<Args> (args)...);
}
//---------------------------------------------------------------------
template <typename E, typename T, typename ...Args, size_t N>
template <typename E, typename T, typename ...Args>
decltype(auto)
expect_throw (T &&t, const char (&fmt)[N], Args&&...args)
expect_throw (T &&t, fmt::format_string<Args...> fmt, Args&&...args)
{
bool success = false;
@ -202,14 +206,14 @@ namespace cruft::TAP {
success = false;
}
return expect (success, fmt, std::forward<Args> (args)...);
return expect (success, std::move (fmt), std::forward<Args> (args)...);
}
///////////////////////////////////////////////////////////////////////
template <size_t N, typename ...Args>
template <typename ...Args>
decltype(auto)
fail (const char (&fmt)[N], Args &&...args)
fail (fmt::format_string<Args...> fmt, Args &&...args)
{
return expect (false, fmt, std::forward<Args> (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");
}

View File

@ -18,12 +18,12 @@
///////////////////////////////////////////////////////////////////////////////
template <std::size_t N, typename ...ArgsT>
template <typename ...ArgsT>
static void
test_sort (
cruft::TAP::logger &tap,
std::vector<int> &&data,
char const (&fmt)[N],
fmt::format_string<ArgsT...> fmt,
ArgsT&&...args
) {
// Pre-allocate the array so that we don't get caught by pointer