filesize iomanip

This commit is contained in:
mrbesen 2022-10-26 22:14:47 +02:00
parent 9a43d50969
commit 13559b181e
Signed by: MrBesen
GPG Key ID: 596B2350DCD67504
3 changed files with 40 additions and 0 deletions

19
Log.cpp
View File

@ -271,4 +271,23 @@ void setColoredOutput(bool enabled) {
dynamic_cast<ConsoleOutput&>(*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

14
Log.h
View File

@ -105,6 +105,20 @@ extern std::vector<Entry::MetaFunction> 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;

View File

@ -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;