From bde5939b2f1eb5c04c1ab429a79fa799c3bfee28 Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Mon, 26 Feb 2024 18:46:21 +0100 Subject: implement printing fractions for corrade's Debug{} --- compat/debug.cpp | 16 ++++++++++++++++ compat/debug.hpp | 12 ++++++++++++ src/chunk-region.cpp | 5 ++--- 3 files changed, 30 insertions(+), 3 deletions(-) diff --git a/compat/debug.cpp b/compat/debug.cpp index 0a77d298..0c7a6fd1 100644 --- a/compat/debug.cpp +++ b/compat/debug.cpp @@ -1,11 +1,16 @@ #include "debug.hpp" #include "compat/strerror.hpp" #include +#include #include // Error{} << "error" << colon() << "can't open file" << colon() << quoted("foo") << error_string(EINVAL); // ===> "error: can't open file 'foo': Invalid argument" +#ifdef __clang__ +#pragma clang diagnostic ignored "-Wformat-nonliteral" +#endif + namespace floormat::detail::corrade_debug { Debug& operator<<(Debug& dbg, Colon box) @@ -46,6 +51,15 @@ Debug& quoted_end(Debug& dbg, Debug::Flags flags, char c) template struct Quoted; +Debug& operator<<(Debug& dbg, Fraction f) +{ + char fmt[8], buf[56]; + std::snprintf(fmt, sizeof fmt, "%%.%hhuf", f.decimal_points); + std::snprintf(buf, sizeof buf, fmt, (double)f.value); + dbg << buf; + return dbg; +} + } // namespace floormat::detail::corrade_debug namespace floormat { @@ -56,4 +70,6 @@ Colon colon(char c) { return Colon{c}; } ErrorString error_string(int error) { return { error }; } ErrorString error_string() { return { errno }; } +Fraction fraction(float value, uint8_t decimal_points) { return Fraction { value, decimal_points }; } + } // namespace floormat diff --git a/compat/debug.hpp b/compat/debug.hpp index cb36e612..1fdd9a7e 100644 --- a/compat/debug.hpp +++ b/compat/debug.hpp @@ -40,6 +40,16 @@ template Debug& operator<<(Debug& dbg, Quoted box) return quoted_end(dbg, flags, box.c); } +// ***** float ***** + +struct Fraction +{ + float value; + uint8_t decimal_points; +}; + +Debug& operator<<(Debug& dbg, Fraction frac); + } // namespace floormat::detail::corrade_debug // ***** api ***** @@ -52,6 +62,8 @@ floormat::detail::corrade_debug::Colon colon(char c = ':'); floormat::detail::corrade_debug::ErrorString error_string(int error); floormat::detail::corrade_debug::ErrorString error_string(); +floormat::detail::corrade_debug::Fraction fraction(float value, uint8_t decimal_points = 1); + template auto quoted(T&& value, char c = '\'') { diff --git a/src/chunk-region.cpp b/src/chunk-region.cpp index 0006285d..5b15b7e2 100644 --- a/src/chunk-region.cpp +++ b/src/chunk-region.cpp @@ -3,6 +3,7 @@ #include "world.hpp" #include "collision.hpp" #include "object.hpp" +#include "compat/debug.hpp" #include "compat/function2.hpp" #include #include @@ -183,9 +184,7 @@ auto chunk::make_pass_region(const pred& f, bool debug) -> pass_region if (debug) [[unlikely]] { const auto time = timeline.currentFrameTime(); - char buf[32]; - std::snprintf(buf, sizeof buf, "%.3f", 1e3*(double)time); - DBG_nospace << "region: generating for " << _coord << " took " << buf << " ms"; + DBG_nospace << "region: generating for " << _coord << " took " << fraction(1e3f*time, 3) << " ms"; } return ret; -- cgit v1.2.3