]> git.ipfire.org Git - pakfire.git/commitdiff
build: Increase precision of the logging timestamp
authorMichael Tremer <michael.tremer@ipfire.org>
Thu, 31 Aug 2023 04:41:23 +0000 (04:41 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Thu, 31 Aug 2023 04:41:23 +0000 (04:41 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/libpakfire/build.c
src/libpakfire/include/pakfire/util.h

index 194a8a638cb9888b6f3230a54ee5764920cfd392..e4e5b6949307c267af85b495407bfe30aafeebc0 100644 (file)
@@ -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,
index e5d32acd0e124834ce610f97809274a584635249..184f1558d0bbed6ca0763ebde4ddf59a63052d20 100644 (file)
@@ -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 */