Skip to content

Commit

Permalink
Formatter for Langulus::Size
Browse files Browse the repository at this point in the history
  • Loading branch information
Epixu committed Jan 22, 2024
1 parent fe761b2 commit 9264d2e
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions source/Logger.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -307,4 +307,27 @@ namespace fmt
}
};

///
/// Extend FMT to be capable of logging byte sizes
///
template<>
struct fmt::formatter<::Langulus::Size> {
template<typename ParseContext>
constexpr auto parse(ParseContext& ctx) {
return ctx.begin();
}

template<typename FormatContext>
auto format(const ::Langulus::Size& bs, FormatContext& ctx) {
double f;
if (bs < 1'000LL) f = static_cast<float>(bs);
else if (bs < 1'000'000LL) f = bs * 1. / 1000LL;
else if (bs < 1'000'000'000LL) f = bs * 1. / 1000'000LL;
else if (bs < 1'000'000'000'000LL) f = bs * 1. / 1000'000'000LL;
else if (bs < 1'000'000'000'000'000LL) f = bs * 1. / 1000'000'000'000LL;
else f = bs * 1. / 1000'000'000'000'000LL;
return format_to(ctx.out(), "{:.2f} {}", f, bs.GetSuffix());
}
};

} // namespace fmt

0 comments on commit 9264d2e

Please sign in to comment.