Skip to content

Commit d1e687b

Browse files
committed
Use boost::chrono::thread_clock for better portability
1 parent 41c975f commit d1e687b

File tree

2 files changed

+10
-12
lines changed

2 files changed

+10
-12
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ else()
160160
find_package(fmt 10.0 REQUIRED CONFIG PATHS "${CMAKE_CURRENT_BINARY_DIR}/fmtlib-install" NO_DEFAULT_PATH)
161161
endif()
162162

163-
list(APPEND DWARFS_BOOST_MODULES date_time filesystem program_options system)
163+
list(APPEND DWARFS_BOOST_MODULES chrono date_time filesystem program_options system)
164164

165165
if(WITH_PYTHON)
166166
# TODO: would be nicer to be able to support a range of python versions

include/dwarfs/logger.h

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@
3838
#include <type_traits>
3939
#include <utility>
4040

41+
#include <boost/chrono/thread_clock.hpp>
42+
4143
#include "dwarfs/error.h"
4244
#include "dwarfs/util.h"
4345

@@ -131,6 +133,8 @@ class level_logger {
131133

132134
class timed_level_logger {
133135
public:
136+
using thread_clock = boost::chrono::thread_clock;
137+
134138
timed_level_logger(logger& lgr, logger::level_type level,
135139
char const* file = nullptr, int line = 0,
136140
bool with_cpu = false)
@@ -142,7 +146,7 @@ class timed_level_logger {
142146
, line_(line) {
143147
oss_.imbue(lgr_.locale());
144148
if (with_cpu) {
145-
::clock_gettime(CLOCK_THREAD_CPUTIME_ID, &cpu_start_time_);
149+
cpu_start_time_ = thread_clock::now();
146150
}
147151
}
148152

@@ -154,11 +158,9 @@ class timed_level_logger {
154158
std::chrono::high_resolution_clock::now() - start_time_;
155159
oss_ << " [" << time_with_unit(sec.count());
156160
if (with_cpu_) {
157-
struct ::timespec cpu_end_time;
158-
::clock_gettime(CLOCK_THREAD_CPUTIME_ID, &cpu_end_time);
159-
auto cpu_time = timespec_to_double(cpu_end_time) -
160-
timespec_to_double(cpu_start_time_);
161-
oss_ << ", " << time_with_unit(cpu_time) << " CPU";
161+
boost::chrono::duration<double> cpu_time_sec =
162+
thread_clock::now() - cpu_start_time_;
163+
oss_ << ", " << time_with_unit(cpu_time_sec.count()) << " CPU";
162164
}
163165
oss_ << "]";
164166
lgr_.write(level_, oss_.str(), file_, line_);
@@ -173,15 +175,11 @@ class timed_level_logger {
173175
}
174176

175177
private:
176-
static double timespec_to_double(struct ::timespec const& ts) {
177-
return ts.tv_sec + 1e-9 * ts.tv_nsec;
178-
}
179-
180178
logger& lgr_;
181179
std::ostringstream oss_;
182180
logger::level_type const level_;
183181
std::chrono::time_point<std::chrono::high_resolution_clock> start_time_;
184-
struct ::timespec cpu_start_time_;
182+
thread_clock::time_point cpu_start_time_;
185183
bool output_{false};
186184
bool const with_cpu_;
187185
char const* const file_;

0 commit comments

Comments
 (0)