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.
This commit is contained in:
Marcus Holland-Moritz 2023-07-23 19:57:06 +02:00
parent ef02024dc5
commit 82977c7224
5 changed files with 3 additions and 26 deletions

View File

@ -24,7 +24,6 @@
#include <atomic>
#include <cstddef>
#include <iosfwd>
#include <locale>
#include <mutex>
#include <string>
@ -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<level_type> threshold_;
std::string statebuf_;

View File

@ -27,7 +27,6 @@
#include <cstddef>
#include <ctime>
#include <iostream>
#include <locale>
#include <memory>
#include <mutex>
#include <sstream>
@ -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 <class Policy>
@ -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<level_type> 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();
}

View File

@ -21,7 +21,6 @@
#include <cstdlib>
#include <cstring>
#include <locale>
#include <sstream>
#include <folly/Conv.h>
@ -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) {

View File

@ -22,7 +22,6 @@
#include <cstdlib>
#include <cstring>
#include <iterator>
#include <locale>
#include <stdexcept>
#include <folly/Conv.h>
@ -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);

View File

@ -140,7 +140,6 @@ void analyze_frozen(std::ostream& os,
MappedFrozen<thrift::metadata::metadata> 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<double>(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<double>(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<double>(total_size) / meta.inodes().size());