From 82977c7224f85f7eb0ce5fb30088550fcbee24ef Mon Sep 17 00:00:00 2001 From: Marcus Holland-Moritz Date: Sun, 23 Jul 2023 19:57:06 +0200 Subject: [PATCH] Just use the global locale (fixes gh #156) After all the effort of setting the global locale in `safe_main`, we might just as well use it. Also makes the code a lot simpler. --- include/dwarfs/console_writer.h | 4 ---- include/dwarfs/logger.h | 11 +---------- src/dwarfs/console_writer.cpp | 4 ---- src/dwarfs/logger.cpp | 2 -- src/dwarfs/metadata_v2.cpp | 8 ++------ 5 files changed, 3 insertions(+), 26 deletions(-) diff --git a/include/dwarfs/console_writer.h b/include/dwarfs/console_writer.h index 72117afe..23704ee3 100644 --- a/include/dwarfs/console_writer.h +++ b/include/dwarfs/console_writer.h @@ -24,7 +24,6 @@ #include #include #include -#include #include #include @@ -49,13 +48,10 @@ class console_writer : public logger { void update(const progress& p, bool last); - std::locale const& locale() const override { return locale_; } - private: void rewind(); std::ostream& os_; - std::locale locale_; std::mutex mx_; std::atomic threshold_; std::string statebuf_; diff --git a/include/dwarfs/logger.h b/include/dwarfs/logger.h index af71d943..af50a289 100644 --- a/include/dwarfs/logger.h +++ b/include/dwarfs/logger.h @@ -27,7 +27,6 @@ #include #include #include -#include #include #include #include @@ -59,8 +58,6 @@ class logger { virtual void write(level_type level, const std::string& output, char const* file, int line) = 0; - virtual std::locale const& locale() const = 0; - const std::string& policy_name() const { return policy_name_; } template @@ -88,14 +85,11 @@ class stream_logger : public logger { void write(level_type level, const std::string& output, char const* file, int line) override; - std::locale const& locale() const override { return locale_; } - void set_threshold(level_type threshold); void set_with_context(bool with_context) { with_context_ = with_context; } private: std::ostream& os_; - std::locale locale_; std::mutex mx_; std::atomic threshold_; bool const color_; @@ -109,9 +103,7 @@ class level_logger { : lgr_(lgr) , level_(level) , file_(file) - , line_(line) { - oss_.imbue(lgr_.locale()); - } + , line_(line) {} level_logger(level_logger const&) = delete; @@ -144,7 +136,6 @@ class timed_level_logger { , with_cpu_(with_cpu) , file_(file) , line_(line) { - oss_.imbue(lgr_.locale()); if (with_cpu) { cpu_start_time_ = thread_clock::now(); } diff --git a/src/dwarfs/console_writer.cpp b/src/dwarfs/console_writer.cpp index e3f3d29e..d8fdb264 100644 --- a/src/dwarfs/console_writer.cpp +++ b/src/dwarfs/console_writer.cpp @@ -21,7 +21,6 @@ #include #include -#include #include #include @@ -82,7 +81,6 @@ console_writer::console_writer(std::ostream& os, progress_mode pg_mode, size_t width, level_type threshold, display_mode mode, bool with_context) : os_(os) - , locale_{""} , threshold_(threshold) , frac_(0.0) , pg_mode_(pg_mode) @@ -183,8 +181,6 @@ void console_writer::update(const progress& p, bool last) { std::ostringstream oss; - oss.imbue(locale_); - bool fancy = pg_mode_ == ASCII || pg_mode_ == UNICODE; if (last || fancy) { diff --git a/src/dwarfs/logger.cpp b/src/dwarfs/logger.cpp index 26a8fac6..23ed7899 100644 --- a/src/dwarfs/logger.cpp +++ b/src/dwarfs/logger.cpp @@ -22,7 +22,6 @@ #include #include #include -#include #include #include @@ -64,7 +63,6 @@ logger::level_type logger::parse_level(std::string_view level) { stream_logger::stream_logger(std::ostream& os, level_type threshold, bool with_context) : os_(os) - , locale_{""} , color_(stream_is_fancy_terminal(os)) , with_context_(with_context) { set_threshold(threshold); diff --git a/src/dwarfs/metadata_v2.cpp b/src/dwarfs/metadata_v2.cpp index 670ee6d7..4f9e9bef 100644 --- a/src/dwarfs/metadata_v2.cpp +++ b/src/dwarfs/metadata_v2.cpp @@ -140,7 +140,6 @@ void analyze_frozen(std::ostream& os, MappedFrozen const& meta, size_t total_size, int detail) { using namespace ::apache::thrift::frozen; - std::locale loc(""); std::ostringstream oss; stream_logger lgr(oss); @@ -157,8 +156,7 @@ void analyze_frozen(std::ostream& os, #endif auto fmt_size = [&](auto const& name, size_t count, size_t size) { - return fmt::format(loc, - "{0:>14" DWARFS_FMT_L "} {1:.<20}{2:.>16" DWARFS_FMT_L + return fmt::format("{0:>14" DWARFS_FMT_L "} {1:.<20}{2:.>16" DWARFS_FMT_L "} bytes {3:5.1f}% {4:5.1f} bytes/item\n", count, name, size, 100.0 * size / total_size, count > 0 ? static_cast(size) / count : 0.0); @@ -167,7 +165,6 @@ void analyze_frozen(std::ostream& os, auto fmt_detail = [&](auto const& name, size_t count, size_t size, std::string num) { return fmt::format( - loc, " {0:<20}{1:>16" DWARFS_FMT_L "} bytes {2:>6} " "{3:5.1f} bytes/item\n", name, size, num, count > 0 ? static_cast(size) / count : 0.0); @@ -276,8 +273,7 @@ void analyze_frozen(std::ostream& os, }); os << "metadata memory usage:\n"; - os << fmt::format(loc, - " {0:.<20}{1:.>16" DWARFS_FMT_L + os << fmt::format(" {0:.<20}{1:.>16" DWARFS_FMT_L "} bytes {2:6.1f} bytes/inode\n", "total metadata", total_size, static_cast(total_size) / meta.inodes().size());