]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
common: add bytes2str pretty print function
authorPatrick Donnelly <pdonnell@redhat.com>
Sun, 30 Jul 2017 23:09:32 +0000 (16:09 -0700)
committerPatrick Donnelly <pdonnell@redhat.com>
Tue, 12 Sep 2017 22:46:23 +0000 (15:46 -0700)
Signed-off-by: Patrick Donnelly <pdonnell@redhat.com>
src/common/util.cc
src/include/util.h

index 762fe374bb578723671f667c1cefae1b815e760d..4f31b932faa4385127036f7180cb6ef2a5197882 100644 (file)
 #include <sys/mount.h>
 #endif
 
+#include <string>
+
+#include <stdio.h>
+
 int64_t unit_to_bytesize(string val, ostream *pss)
 {
   if (val.empty()) {
@@ -302,3 +306,15 @@ string cleanbin(string &str)
     result = "Base64:" + result;
   return result;
 }
+
+std::string bytes2str(uint64_t count) {
+  static char s[][2] = {"\0", "k", "M", "G", "T", "P", "E", "\0"};
+  int i = 0;
+  while (count >= 1024 && *s[i+1]) {
+    count >>= 10;
+    i++;
+  }
+  char str[128];
+  snprintf(str, sizeof str, "%" PRIu64 "%sB", count, s[i]);
+  return std::string(str);
+}
index b152c1130df9455684fcd4a82285bbfc705fb873..3de4c3d3ec1e3ed3e58616877af2325ccfd8cc93 100644 (file)
@@ -19,6 +19,8 @@
 
 int64_t unit_to_bytesize(string val, ostream *pss);
 
+std::string bytes2str(uint64_t count);
+
 struct ceph_data_stats
 {
   uint64_t byte_total;