From: Michael Tremer Date: Thu, 31 Aug 2023 04:41:23 +0000 (+0000) Subject: build: Increase precision of the logging timestamp X-Git-Tag: 0.9.29~35 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=3afded28f1ce6d9d0acbe92d81977c3c1f1aa34c;p=pakfire.git build: Increase precision of the logging timestamp Signed-off-by: Michael Tremer --- diff --git a/src/libpakfire/build.c b/src/libpakfire/build.c index 194a8a638..e4e5b6949 100644 --- a/src/libpakfire/build.c +++ b/src/libpakfire/build.c @@ -78,7 +78,7 @@ struct pakfire_build { char target[PATH_MAX]; // Times - time_t time_start; + struct timespec time_start; // cgroup struct pakfire_cgroup* cgroup; @@ -116,15 +116,16 @@ static int pakfire_build_has_flag(struct pakfire_build* build, int flag) { return build->flags & flag; } -static time_t pakfire_build_duration(struct pakfire_build* build) { - // What time is it now? - time_t now = time(NULL); +static double pakfire_build_duration(struct pakfire_build* build) { + struct timespec now; + int r; - // Return any errors - if (now < 0) - return now; + // What time is it now? + r = clock_gettime(CLOCK_MONOTONIC, &now); + if (r < 0) + return r; - return now - build->time_start; + return pakfire_timespec_delta(&now, &build->time_start); } static int __pakfire_build_setup_repo(struct pakfire* pakfire, @@ -1393,6 +1394,23 @@ ERROR: return r; } +static int pakfire_build_log_strftime( + char* buffer, const size_t length, const double t) { + const unsigned int h = (unsigned int)t / 3600; + const unsigned int m = (unsigned int)t % 3600 / 60; + const unsigned int s = (unsigned int)t % 60; + const unsigned int ms = (unsigned int)(t * 1000.0) % 1000; + + if (h) + return __pakfire_string_format(buffer, length, "%02d:%02d:%02d.%04d", h, m, s, ms); + + else if (m) + return __pakfire_string_format(buffer, length, " %02d:%02d.%04d", m, s, ms); + + else + return __pakfire_string_format(buffer, length, " %02d.%04d", s, ms); +} + static int pakfire_build_log_callback(struct pakfire* pakfire, void* data, int priority, const char* line, size_t length) { struct pakfire_build* build = data; @@ -1400,12 +1418,12 @@ static int pakfire_build_log_callback(struct pakfire* pakfire, int r; // Get the runtime of the build - const time_t t = pakfire_build_duration(build); + const double t = pakfire_build_duration(build); if (t < 0) return t; // Format the timestamp - r = pakfire_string_format(buffer, "%8ld", t); + r = pakfire_build_log_strftime(buffer, sizeof(buffer), t); if (r < 0) return r; @@ -1541,16 +1559,14 @@ static int pakfire_build_setup_repo(struct pakfire_build* build) { } static int pakfire_build_set_time_start(struct pakfire_build* build) { - const time_t now = time(NULL); + int r; - if (now < 0) { + // Fetch current time + r = clock_gettime(CLOCK_MONOTONIC, &build->time_start); + if (r < 0) ERROR(build->pakfire, "Could not fetch start time: %m\n"); - return 1; - } - - build->time_start = now; - return 0; + return r; } PAKFIRE_EXPORT int pakfire_build_create(struct pakfire_build** build, diff --git a/src/libpakfire/include/pakfire/util.h b/src/libpakfire/include/pakfire/util.h index e5d32acd0..184f1558d 100644 --- a/src/libpakfire/include/pakfire/util.h +++ b/src/libpakfire/include/pakfire/util.h @@ -134,6 +134,17 @@ int pakfire_b64decode(struct pakfire* pakfire, void** output, size_t* length, // Copy int pakfire_copy(struct pakfire* pakfire, FILE* src, FILE* dst); +// Time + +static inline double pakfire_timespec_delta(struct timespec* t1, struct timespec* t2) { + // Compute delta in seconds + return ( + ((t1->tv_sec * 1000) + (t1->tv_nsec / 1000000)) + - + ((t2->tv_sec * 1000) + (t2->tv_nsec / 1000000)) + ) / 1000.0; +} + #endif #endif /* PAKFIRE_UTIL_H */