From dc17315950b00c384bcfe9b360093c34b5afb28a Mon Sep 17 00:00:00 2001 From: Michael Tremer Date: Sat, 30 Sep 2023 11:34:09 +0000 Subject: [PATCH] build: Refactor the logger We now have the option to directly write something directly into the build logger. Signed-off-by: Michael Tremer --- src/cli/lib/build.c | 27 +++++++-- src/libpakfire/build.c | 81 ++++++++++++++++---------- src/libpakfire/include/pakfire/build.h | 3 +- 3 files changed, 75 insertions(+), 36 deletions(-) diff --git a/src/cli/lib/build.c b/src/cli/lib/build.c index eb2e20f59..875657038 100644 --- a/src/cli/lib/build.c +++ b/src/cli/lib/build.c @@ -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; } diff --git a/src/libpakfire/build.c b/src/libpakfire/build.c index cdbb11154..493b89aac 100644 --- a/src/libpakfire/build.c +++ b/src/libpakfire/build.c @@ -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)) { diff --git a/src/libpakfire/include/pakfire/build.h b/src/libpakfire/include/pakfire/build.h index af4b4a2ef..858b75829 100644 --- a/src/libpakfire/include/pakfire/build.h +++ b/src/libpakfire/include/pakfire/build.h @@ -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); -- 2.47.2