From b59a48c2b65717336900977ff7374f90cca68ac4 Mon Sep 17 00:00:00 2001 From: Marcus Holland-Moritz Date: Wed, 26 Oct 2022 12:40:57 +0200 Subject: [PATCH] Replace use of boost posix_time with {fmt} Only issue is that in order to properly format fractional seconds, we need a bleeding edge version of {fmt}. --- CMakeLists.txt | 4 ++-- CMakeLists.txt.fmtlib | 2 +- include/dwarfs/logger.h | 1 + src/dwarfs/console_writer.cpp | 8 ++------ src/dwarfs/logger.cpp | 13 ++++++++----- 5 files changed, 14 insertions(+), 14 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index e0dae7ff..ae8b96c7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -120,7 +120,7 @@ if(WITH_MAN_PAGES) endif() if(NOT STATIC_BUILD_DO_NOT_USE) - find_package(fmt 8 CONFIG) + find_package(fmt 9.1 CONFIG) endif() if(STATIC_BUILD_DO_NOT_USE OR NOT fmt_FOUND) @@ -141,7 +141,7 @@ if(STATIC_BUILD_DO_NOT_USE OR NOT fmt_FOUND) endif() set(CMAKE_PREFIX_PATH "${CMAKE_CURRENT_BINARY_DIR}/fmtlib-install;${CMAKE_PREFIX_PATH}") include_directories(BEFORE "${CMAKE_CURRENT_BINARY_DIR}/fmtlib-install/include") - find_package(fmt 8 REQUIRED CONFIG PATHS "${CMAKE_CURRENT_BINARY_DIR}/fmtlib-install" NO_DEFAULT_PATH) + find_package(fmt 9.1 REQUIRED CONFIG PATHS "${CMAKE_CURRENT_BINARY_DIR}/fmtlib-install" NO_DEFAULT_PATH) endif() list(APPEND DWARFS_BOOST_MODULES date_time filesystem program_options system) diff --git a/CMakeLists.txt.fmtlib b/CMakeLists.txt.fmtlib index 9de9c648..c64ee244 100644 --- a/CMakeLists.txt.fmtlib +++ b/CMakeLists.txt.fmtlib @@ -8,7 +8,7 @@ ExternalProject_Add( fmtlib PREFIX ${CMAKE_CURRENT_BINARY_DIR}/fmtlib GIT_REPOSITORY https://github.com/fmtlib/fmt.git - GIT_TAG 8.1.1 + GIT_TAG 80f8d34427d40ec5e7ce3b10ededc46bd4bd5759 CMAKE_ARGS -DCMAKE_CXX_COMPILER_LAUNCHER=${CMAKE_CXX_COMPILER_LAUNCHER} -DCMAKE_INSTALL_PREFIX:PATH=${CMAKE_CURRENT_BINARY_DIR}/fmtlib-install -DCMAKE_BUILD_TYPE=Release diff --git a/include/dwarfs/logger.h b/include/dwarfs/logger.h index 1a57c2f4..dea3145e 100644 --- a/include/dwarfs/logger.h +++ b/include/dwarfs/logger.h @@ -412,5 +412,6 @@ std::shared_ptr make_shared_logging_object(logger& lgr, Args&&... args) { } std::string get_logger_context(char const* path, int line); +std::string get_current_time_string(); } // namespace dwarfs diff --git a/src/dwarfs/console_writer.cpp b/src/dwarfs/console_writer.cpp index 9845e31a..75dca741 100644 --- a/src/dwarfs/console_writer.cpp +++ b/src/dwarfs/console_writer.cpp @@ -24,8 +24,6 @@ #include #include -#include - #include #include @@ -71,8 +69,6 @@ console_writer::console_writer(std::ostream& os, progress_mode pg_mode, , color_(stream_is_fancy_terminal(os)) , with_context_(with_context) , debug_progress_(is_debug_progress()) { - os_.imbue(std::locale(os_.getloc(), - new boost::posix_time::time_facet("%H:%M:%S.%f"))); if (threshold > level_type::INFO) { set_policy(); } else { @@ -96,7 +92,7 @@ void console_writer::rewind() { void console_writer::write(level_type level, const std::string& output, char const* file, int line) { if (level <= threshold_) { - auto t = boost::posix_time::microsec_clock::local_time(); + auto t = get_current_time_string(); const char* prefix = ""; const char* suffix = ""; @@ -248,7 +244,7 @@ void console_writer::update(const progress& p, bool last) { fmt::format(" ==> {0:.0f}% done, {1} blocks/{2} written", 100 * frac_, p.blocks_written, size_with_unit(p.compressed_size)); if (tmp != statebuf_) { - auto t = boost::posix_time::microsec_clock::local_time(); + auto t = get_current_time_string(); statebuf_ = tmp; std::lock_guard lock(mx_); os_ << "- " << t << statebuf_ << "\n"; diff --git a/src/dwarfs/logger.cpp b/src/dwarfs/logger.cpp index 028ede7a..829c3c0b 100644 --- a/src/dwarfs/logger.cpp +++ b/src/dwarfs/logger.cpp @@ -25,8 +25,6 @@ #include #include -#include - #include #ifndef NDEBUG @@ -36,6 +34,7 @@ #define DWARFS_SYMBOLIZE 0 #endif +#include #include #include "dwarfs/logger.h" @@ -67,15 +66,13 @@ stream_logger::stream_logger(std::ostream& os, level_type threshold, : os_(os) , color_(stream_is_fancy_terminal(os)) , with_context_(with_context) { - os_.imbue(std::locale(os_.getloc(), - new boost::posix_time::time_facet("%H:%M:%S.%f"))); set_threshold(threshold); } void stream_logger::write(level_type level, const std::string& output, char const* file, int line) { if (level <= threshold_) { - auto t = boost::posix_time::microsec_clock::local_time(); + auto t = get_current_time_string(); const char* prefix = ""; const char* suffix = ""; @@ -149,4 +146,10 @@ std::string get_logger_context(char const* path, int line) { return fmt::format("[{0}:{1}] ", base ? base + 1 : path, line); } +std::string get_current_time_string() { + using namespace std::chrono; + auto now = floor(system_clock::now()); + return fmt::format("{:%H:%M:%S}", now); +} + } // namespace dwarfs