summaryrefslogtreecommitdiffhomepage
path: root/src
diff options
context:
space:
mode:
authorStanislaw Halik <sthalik@misaki.pl>2024-03-05 21:56:35 +0100
committerStanislaw Halik <sthalik@misaki.pl>2024-03-05 21:56:35 +0100
commit036251d1fa388fc7b381643e06e65c988ba33def (patch)
tree75a3141add1dac2f7a985195e8f30635ec249252 /src
parent38d2c8eb13b5317f77697e72574983d79a992353 (diff)
make more readable converting Ns to string
Diffstat (limited to 'src')
-rw-r--r--src/timer-ns.cpp66
1 files changed, 50 insertions, 16 deletions
diff --git a/src/timer-ns.cpp b/src/timer-ns.cpp
index b467ce19..1f927f14 100644
--- a/src/timer-ns.cpp
+++ b/src/timer-ns.cpp
@@ -8,27 +8,61 @@ namespace floormat {
-Debug& operator<<(Debug& dbg, const Ns& box)
+Debug& operator<<(Debug& dbg, const Ns& val)
{
- const auto value = (float)((double)box.stamp * 1e-6);
- const auto absval = Math::abs(value);
+ const char* unit;
+ double x = val.stamp;
int precision;
- if (absval < 2)
- precision = 4;
- else if (absval < 5)
- precision = 2;
- else if (absval < 100)
+ if (val >= 10*Second)
+ {
+ unit = "s";
+ x *= 1e-9;
precision = 1;
+ }
+#if 0
+ else if (val >= 1*Second)
+ {
+ unit = "ms";
+ x *= 1e-6;
+ precision = 3;
+
+ }
+#endif
+ else if (val >= 100*Millisecond)
+ {
+ unit = "ms";
+ x *= 1e-6;
+ precision = 3;
+ }
+ else if (val >= 50*Microsecond)
+ {
+ unit = "ms";
+ x *= 1e-6;
+ precision = 4;
+ }
+ else if (val >= 1*Microsecond)
+ {
+ unit = "usec";
+ x *= 1e-3;
+ precision = 3;
+ }
+ else if (val.stamp > 1000)
+ {
+ char buf[64];
+ std::snprintf(buf, sizeof buf, "%llu_%llu ns",
+ val.stamp / 1000, val.stamp % 1000);
+ return dbg;
+ }
else
+ {
+ unit = "ns";
precision = 0;
- auto flags = dbg.flags();
- dbg << "";
- dbg.setFlags(flags | Debug::Flag::NoSpace);
- //dbg << "{";
- dbg << fraction(value, precision);
- dbg << " ms";
- //dbg << "}";
- dbg.setFlags(flags);
+ }
+ if (!precision)
+ dbg << (uint64_t)x << Debug::space << unit;
+ else
+ dbg << fraction((float)x, precision) << Debug::space << unit;
+
return dbg;
}