[Libguestfs] [PATCH nbdkit v3 1/5] filters: stats: Add maybe() helper

Nir Soffer nirsof at gmail.com
Wed Dec 4 22:34:43 UTC 2019


Returns non-null string or "(n/a)".
---
 filters/stats/stats.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)

diff --git a/filters/stats/stats.c b/filters/stats/stats.c
index 6e48017..44f3379 100644
--- a/filters/stats/stats.c
+++ b/filters/stats/stats.c
@@ -94,6 +94,12 @@ humansize (uint64_t bytes)
   return ret;
 }
 
+static inline const char *
+maybe (char *s)
+{
+  return s ? s : "(n/a)";
+}
+
 static void
 print_stat (const stat *st, int64_t usecs)
 {
@@ -104,8 +110,7 @@ print_stat (const stat *st, int64_t usecs)
       humansize (st->bytes / (usecs / 1000000.0)) : NULL;
 
     fprintf (fp, "%s: %" PRIu64 " ops, %s, %s/s\n",
-             st->name, st->ops,
-             size ? size : "(n/a)", rate ? rate : "(n/a)");
+             st->name, st->ops, maybe (size), maybe (rate));
 
     free (size);
     free (rate);
-- 
2.21.0





More information about the Libguestfs mailing list