aboutsummaryrefslogtreecommitdiff
path: root/src/common
diff options
context:
space:
mode:
authorrbrunner7 <rbrunner@dreamshare.ch>2019-03-21 11:03:24 +0100
committerrbrunner7 <rbrunner@dreamshare.ch>2019-03-24 16:58:57 +0100
commitc23ea7962debe4e66fd3c0e7719117dcb7966d1f (patch)
tree4627040783eac198cbcdde56e47a8e16c3a16b77 /src/common
parentMerge pull request #5201 (diff)
downloadmonero-c23ea7962debe4e66fd3c0e7719117dcb7966d1f.tar.xz
New interactive daemon command 'print_net_stats': Global traffic stats
Diffstat (limited to 'src/common')
-rw-r--r--src/common/util.cpp36
-rw-r--r--src/common/util.h2
2 files changed, 38 insertions, 0 deletions
diff --git a/src/common/util.cpp b/src/common/util.cpp
index 80b8a9e81..728efc294 100644
--- a/src/common/util.cpp
+++ b/src/common/util.cpp
@@ -80,6 +80,7 @@ using namespace epee;
#include <boost/filesystem.hpp>
#include <boost/algorithm/string.hpp>
#include <boost/asio.hpp>
+#include <boost/format.hpp>
#include <openssl/sha.h>
#undef MONERO_DEFAULT_LOG_CATEGORY
@@ -1063,4 +1064,39 @@ std::string get_nix_version_display_string()
strftime(buffer, sizeof(buffer), "%Y-%m-%d %H:%M:%S", &tm);
return std::string(buffer);
}
+
+ std::string get_human_readable_bytes(uint64_t bytes)
+ {
+ // Use 1024 for "kilo", 1024*1024 for "mega" and so on instead of the more modern and standard-conforming
+ // 1000, 1000*1000 and so on, to be consistent with other Monero code that also uses base 2 units
+ struct byte_map
+ {
+ const char* const format;
+ const std::uint64_t bytes;
+ };
+
+ static constexpr const byte_map sizes[] =
+ {
+ {"%.0f B", 1024},
+ {"%.2f KB", 1024 * 1024},
+ {"%.2f MB", std::uint64_t(1024) * 1024 * 1024},
+ {"%.2f GB", std::uint64_t(1024) * 1024 * 1024 * 1024},
+ {"%.2f TB", std::uint64_t(1024) * 1024 * 1024 * 1024 * 1024}
+ };
+
+ struct bytes_less
+ {
+ bool operator()(const byte_map& lhs, const byte_map& rhs) const noexcept
+ {
+ return lhs.bytes < rhs.bytes;
+ }
+ };
+
+ const auto size = std::upper_bound(
+ std::begin(sizes), std::end(sizes) - 1, byte_map{"", bytes}, bytes_less{}
+ );
+ const std::uint64_t divisor = size->bytes / 1024;
+ return (boost::format(size->format) % (double(bytes) / divisor)).str();
+ }
+
}
diff --git a/src/common/util.h b/src/common/util.h
index ef2305bf4..77a5a9af6 100644
--- a/src/common/util.h
+++ b/src/common/util.h
@@ -244,4 +244,6 @@ namespace tools
void closefrom(int fd);
std::string get_human_readable_timestamp(uint64_t ts);
+
+ std::string get_human_readable_bytes(uint64_t bytes);
}