]> git.ipfire.org Git - pakfire.git/commitdiff
build: Refactor the logger
authorMichael Tremer <michael.tremer@ipfire.org>
Sat, 30 Sep 2023 11:34:09 +0000 (11:34 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Sat, 30 Sep 2023 11:34:09 +0000 (11:34 +0000)
We now have the option to directly write something directly into the
build logger.

Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/cli/lib/build.c
src/libpakfire/build.c
src/libpakfire/include/pakfire/build.h

index eb2e20f595534e00016b198fd993eb5980896f4d..87565703864e0f28e6d4ff5bb45baa891762799d 100644 (file)
@@ -118,18 +118,37 @@ static int parse_argv(struct config* config, int argc, char* argv[]) {
        return 0;
 }
 
-static int log_callback(struct pakfire* pakfire, void* data,
-               int priority, const char* message, size_t length) {
+static int log_callback(struct pakfire_build* build, void* data,
+               int priority, int error, const char* file, int line, const char* function, const char* format, ...) {
+       char* buffer = NULL;
+       va_list args;
+       int r;
+
+       // Format the message
+       va_start(args, format);
+       r = vasprintf(&buffer, format, args);
+       va_end(args);
+
+       // Fail if we could not format the message
+       if (r < 0)
+               return r;
+
        switch (priority) {
+               // Highlight any error messages
                case LOG_ERR:
-                       fprintf(stderr, "%s%.*s%s", color_highlight(), (int)length, message, color_reset());
+                       fprintf(stderr, "%s%s%s", color_highlight(), buffer, color_reset());
                        break;
 
+               // Print the rest to stdout
                default:
-                       fprintf(stdout, "%.*s", (int)length, message);
+                       fprintf(stdout, "%s", buffer);
                        break;
        }
 
+       // Cleanup
+       if (buffer)
+               free(buffer);
+
        return 0;
 }
 
index cdbb1115450b81bf8668870e12e64b13be3b3814..493b89aac4619ac07cf13ab89c849df8de89a627 100644 (file)
@@ -119,6 +119,23 @@ struct pakfire_build {
        "\n" \
        "exit 0\n"
 
+/*
+       Convenience macro to call the logger callback
+*/
+#define pakfire_build_log(build, priority, r, file, line, function, format, arg...) \
+       build->callbacks.log(build, build->callbacks.log_data, \
+                       priority, r, file, line, function, format, ## arg)
+
+#define BUILD_LOG_ERRNO(build, priority, r, arg...) \
+       pakfire_build_log(build, priority, r, __FILE__, __LINE__, __FUNCTION__, ## arg)
+#define BUILD_LOG(build, priority, arg...) BUILD_LOG_ERRNO(build, priority, 0, ## arg)
+
+#define BUILD_INFO_ERRNO(build, r, arg...) BUILD_LOG_ERRNO(build, LOG_INFO, r, ## arg)
+#define BUILD_ERROR_ERRNO(build, r, arg...) BUILD_LOG_ERRNO(build, LOG_ERR, r, ## arg)
+
+#define BUILD_INFO(build, arg...) BUILD_INFO_ERRNO(build, 0, ## arg)
+#define BUILD_ERROR(build, arg...) BUILD_ERROR_ERRNO(build, 0, ## arg)
+
 static int pakfire_build_has_flag(struct pakfire_build* build, int flag) {
        return build->flags & flag;
 }
@@ -1401,55 +1418,57 @@ ERROR:
        return r;
 }
 
-static int pakfire_build_default_log_callback(struct pakfire* pakfire,
-               void* data, int priority, const char* message, size_t length) {
-       // Pass the message to the upstream logger
-       pakfire_log_condition(pakfire, priority, 0, "%.*s", (int)length, message);
+static int pakfire_build_default_log_callback(struct pakfire_build* build, void* data,
+               int priority, int error, const char* file, int line, const char* function, const char* format, ...) {
+       char* buffer = NULL;
+       va_list args;
+       int r;
 
-       return 0;
-}
+       // Only pass messages to the pakfire logger that are wanted
+       if (pakfire_log_get_priority(build->pakfire) < priority)
+               return 0;
 
-static int pakfire_build_log_prepend_timestamp(
-               char** buffer, const double t, const char* message, const int l) {
-       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;
+       // Format the message
+       va_start(args, format);
+       r = vasprintf(&buffer, format, args);
+       va_end(args);
 
-       if (h)
-               return asprintf(buffer, "[%02d:%02d:%02d.%04d] %.*s", h, m, s, ms, l, message);
+       // Fail if we could not format the string
+       if (r < 0)
+               return r;
 
-       else if (m)
-               return asprintf(buffer, "[   %02d:%02d.%04d] %.*s", m, s, ms, l, message);
+       // Send the message to the upstream logger
+       pakfire_log(build->pakfire, priority, error, file, line, function, "%s", buffer);
 
-       else
-               return asprintf(buffer, "[      %02d.%04d] %.*s", s, ms, l, message);
+       // Cleanup
+       if (buffer)
+               free(buffer);
+
+       return 0;
 }
 
 static int pakfire_build_jail_log_callback(struct pakfire* pakfire,
                void* data, int priority, const char* line, size_t length) {
        struct pakfire_build* build = data;
-       char* buffer = NULL;
-       int r;
 
        // Get the runtime of the build
        const double t = pakfire_build_duration(build);
        if (t < 0)
                return t;
 
-       // Format the timestamp
-       r = pakfire_build_log_prepend_timestamp(&buffer, t, line, length);
-       if (r < 0)
-               return r;
+       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;
 
-       // Run the log callback
-       r = build->callbacks.log(pakfire, build->callbacks.log_data, priority, buffer, r);
+       if (h)
+               return BUILD_LOG(build, priority, "[%02d:%02d:%02d.%04d] %.*s", h, m, s, ms, length, line);
 
-       // Cleanup
-       if (buffer)
-               free(buffer);
+       else if (m)
+               return BUILD_LOG(build, priority, "[   %02d:%02d.%04d] %.*s", m, s, ms, length, line);
 
-       return r;
+       else
+               return BUILD_LOG(build, priority, "[      %02d.%04d] %.*s", s, ms, length, line);
 }
 
 /*
@@ -2138,7 +2157,7 @@ PAKFIRE_EXPORT int pakfire_build_exec(struct pakfire_build* build, const char* p
 
        const char* nevra = pakfire_package_get_string(package, PAKFIRE_PKG_NEVRA);
 
-       INFO(build->pakfire, "Building %s...\n", nevra);
+       BUILD_INFO(build, "Building %s...\n", nevra);
 
        // Check if this package can be build in this environment
        if (!pakfire_package_supports_build_arch(package, arch)) {
index af4b4a2ef84176f6db9324dcb68509f5c1b475f8..858b7582945a62574ebbe07578ab259e5aafa94d 100644 (file)
@@ -33,7 +33,8 @@ enum pakfire_build_flags {
 };
 
 typedef int (*pakfire_build_log_callback)
-       (struct pakfire* pakfire, void* data, int priority, const char* message, size_t length);
+       (struct pakfire_build* build, void* data, int priority, int r,
+               const char* file, int line, const char* function, const char* format, ...);
 
 int pakfire_build_create(struct pakfire_build** build,
        struct pakfire* pakfire, const char* id, int flags);