From 13559b181eec2fb72e203ff6c0c8c73df381dbd0 Mon Sep 17 00:00:00 2001 From: mrbesen Date: Wed, 26 Oct 2022 22:14:47 +0200 Subject: [PATCH] filesize iomanip --- Log.cpp | 19 +++++++++++++++++++ Log.h | 14 ++++++++++++++ test.cpp | 7 +++++++ 3 files changed, 40 insertions(+) diff --git a/Log.cpp b/Log.cpp index 32cd703..963a947 100644 --- a/Log.cpp +++ b/Log.cpp @@ -271,4 +271,23 @@ void setColoredOutput(bool enabled) { dynamic_cast(*outputs.at(0)).setColoredOutput(enabled); // has to exist } +std::ostream& operator<<(std::ostream& str, const FileSize& fs) { + static const char PREFIX[] {' ', 'K', 'M', 'G', 'T', 'P', 'E'}; + static const uint_fast8_t PREFIXCOUNT = 7; + static const uint_fast32_t FACTOR = 1000; + static const uint_fast32_t COMMA = 100; // nach komma stellen + + uint64_t cpy = fs.fs * COMMA; + uint_fast8_t prefix = 0; + while(cpy > (FACTOR * 3 * COMMA) && prefix < PREFIXCOUNT) { + cpy /= FACTOR; + ++prefix; + } + + str << (cpy / (float) COMMA); + if(prefix > 0) + str << PREFIX[prefix]; + return str << "B"; +} + } // namespace Log \ No newline at end of file diff --git a/Log.h b/Log.h index 3072eea..c68c191 100644 --- a/Log.h +++ b/Log.h @@ -105,6 +105,20 @@ extern std::vector entryMetaFunctions; std::ostream& defaultEntryMetaTime(std::ostream&, const Entry&); std::ostream& defaultEntryMetaLevel(std::ostream&, const Entry&); +// custom io-manip +// FileSize is a iomanip to convert a filesize (fs) into a human readable format +class FileSize { +public: + explicit FileSize(uint64_t fs) : fs(fs) {} + FileSize(const FileSize&) = delete; + FileSize& operator=(const FileSize&) = delete; +private: + uint64_t fs; + friend std::ostream& operator<<(std::ostream& str, const FileSize& fs); +}; + +std::ostream& operator<<(std::ostream& str, const FileSize& fs); + // copy Level values in Log namespace constexpr Level off = Level::off; constexpr Level fatal = Level::fatal; diff --git a/test.cpp b/test.cpp index 5dacfde..12a0dd3 100644 --- a/test.cpp +++ b/test.cpp @@ -83,6 +83,13 @@ int main() { Log::error << "error msg @Lvl fatal; " << 42 << "; " << 3.14159; Log::fatal << "fatal msg @Lvl fatal; " << 42 << "; " << 3.14159; + Log::setConsoleLogLevel(Log::Level::trace); + std::cout << "=== Log FileSize ===" << std::endl; + Log::info << 1234 << " " << Log::FileSize(1234); + Log::info << 123456 << " " << Log::FileSize(123456); + Log::info << 85241 << " " << Log::FileSize(85241); + Log::info << 9876543210 << " " << Log::FileSize(9876543210); + Log::stop(); return 0;