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
132134class 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