From bfdadc55b2b56218dbc7fdd2eb35009735ad9142 Mon Sep 17 00:00:00 2001 From: Norman Feske Date: Thu, 16 Feb 2017 17:24:44 +0100 Subject: [PATCH] base: Add Number_of_bytes::print method This method attempts to print the number with a K/M/G unit if possible and thereby increases the human readability of generated output. --- repos/base/include/util/string.h | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/repos/base/include/util/string.h b/repos/base/include/util/string.h index b5d69ba411..6242347d19 100644 --- a/repos/base/include/util/string.h +++ b/repos/base/include/util/string.h @@ -51,6 +51,18 @@ class Genode::Number_of_bytes * Convert number of bytes to 'size_t' value */ operator size_t() const { return _n; } + + void print(Output &output) const + { + using Genode::print; + + enum { KB = 1024UL, MB = KB*1024UL, GB = MB*1024UL }; + + if (_n % GB == 0) print(output, _n/GB, "G"); + else if (_n % MB == 0) print(output, _n/MB, "M"); + else if (_n % KB == 0) print(output, _n/KB, "K"); + else print(output, _n); + } };