mirror of
https://github.com/mhx/dwarfs.git
synced 2025-09-14 14:59:52 -04:00
Use boost::chrono::thread_clock for better portability
This commit is contained in:
parent
41c975f26f
commit
d1e687b2a2
@ -160,7 +160,7 @@ else()
|
|||||||
find_package(fmt 10.0 REQUIRED CONFIG PATHS "${CMAKE_CURRENT_BINARY_DIR}/fmtlib-install" NO_DEFAULT_PATH)
|
find_package(fmt 10.0 REQUIRED CONFIG PATHS "${CMAKE_CURRENT_BINARY_DIR}/fmtlib-install" NO_DEFAULT_PATH)
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
list(APPEND DWARFS_BOOST_MODULES date_time filesystem program_options system)
|
list(APPEND DWARFS_BOOST_MODULES chrono date_time filesystem program_options system)
|
||||||
|
|
||||||
if(WITH_PYTHON)
|
if(WITH_PYTHON)
|
||||||
# TODO: would be nicer to be able to support a range of python versions
|
# TODO: would be nicer to be able to support a range of python versions
|
||||||
|
@ -38,6 +38,8 @@
|
|||||||
#include <type_traits>
|
#include <type_traits>
|
||||||
#include <utility>
|
#include <utility>
|
||||||
|
|
||||||
|
#include <boost/chrono/thread_clock.hpp>
|
||||||
|
|
||||||
#include "dwarfs/error.h"
|
#include "dwarfs/error.h"
|
||||||
#include "dwarfs/util.h"
|
#include "dwarfs/util.h"
|
||||||
|
|
||||||
@ -131,6 +133,8 @@ class level_logger {
|
|||||||
|
|
||||||
class timed_level_logger {
|
class timed_level_logger {
|
||||||
public:
|
public:
|
||||||
|
using thread_clock = boost::chrono::thread_clock;
|
||||||
|
|
||||||
timed_level_logger(logger& lgr, logger::level_type level,
|
timed_level_logger(logger& lgr, logger::level_type level,
|
||||||
char const* file = nullptr, int line = 0,
|
char const* file = nullptr, int line = 0,
|
||||||
bool with_cpu = false)
|
bool with_cpu = false)
|
||||||
@ -142,7 +146,7 @@ class timed_level_logger {
|
|||||||
, line_(line) {
|
, line_(line) {
|
||||||
oss_.imbue(lgr_.locale());
|
oss_.imbue(lgr_.locale());
|
||||||
if (with_cpu) {
|
if (with_cpu) {
|
||||||
::clock_gettime(CLOCK_THREAD_CPUTIME_ID, &cpu_start_time_);
|
cpu_start_time_ = thread_clock::now();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -154,11 +158,9 @@ class timed_level_logger {
|
|||||||
std::chrono::high_resolution_clock::now() - start_time_;
|
std::chrono::high_resolution_clock::now() - start_time_;
|
||||||
oss_ << " [" << time_with_unit(sec.count());
|
oss_ << " [" << time_with_unit(sec.count());
|
||||||
if (with_cpu_) {
|
if (with_cpu_) {
|
||||||
struct ::timespec cpu_end_time;
|
boost::chrono::duration<double> cpu_time_sec =
|
||||||
::clock_gettime(CLOCK_THREAD_CPUTIME_ID, &cpu_end_time);
|
thread_clock::now() - cpu_start_time_;
|
||||||
auto cpu_time = timespec_to_double(cpu_end_time) -
|
oss_ << ", " << time_with_unit(cpu_time_sec.count()) << " CPU";
|
||||||
timespec_to_double(cpu_start_time_);
|
|
||||||
oss_ << ", " << time_with_unit(cpu_time) << " CPU";
|
|
||||||
}
|
}
|
||||||
oss_ << "]";
|
oss_ << "]";
|
||||||
lgr_.write(level_, oss_.str(), file_, line_);
|
lgr_.write(level_, oss_.str(), file_, line_);
|
||||||
@ -173,15 +175,11 @@ class timed_level_logger {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
static double timespec_to_double(struct ::timespec const& ts) {
|
|
||||||
return ts.tv_sec + 1e-9 * ts.tv_nsec;
|
|
||||||
}
|
|
||||||
|
|
||||||
logger& lgr_;
|
logger& lgr_;
|
||||||
std::ostringstream oss_;
|
std::ostringstream oss_;
|
||||||
logger::level_type const level_;
|
logger::level_type const level_;
|
||||||
std::chrono::time_point<std::chrono::high_resolution_clock> start_time_;
|
std::chrono::time_point<std::chrono::high_resolution_clock> start_time_;
|
||||||
struct ::timespec cpu_start_time_;
|
thread_clock::time_point cpu_start_time_;
|
||||||
bool output_{false};
|
bool output_{false};
|
||||||
bool const with_cpu_;
|
bool const with_cpu_;
|
||||||
char const* const file_;
|
char const* const file_;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user