]> git.ipfire.org Git - pakfire.git/commitdiff
job: Create a log file and write the entire job output to it
authorMichael Tremer <michael.tremer@ipfire.org>
Wed, 29 Jan 2025 16:49:03 +0000 (16:49 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Wed, 29 Jan 2025 16:51:07 +0000 (16:51 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/pakfire/job.c

index 4d0151fa893253a5d72426f97fd76491f103e8cf..dffdcba43251ff33c5bc8b7d5114734870a74b2c 100644 (file)
@@ -38,6 +38,7 @@
 #include <pakfire/ctx.h>
 #include <pakfire/daemon.h>
 #include <pakfire/job.h>
+#include <pakfire/log_file.h>
 #include <pakfire/log_buffer.h>
 #include <pakfire/log_stream.h>
 #include <pakfire/logging.h>
@@ -83,6 +84,9 @@ struct pakfire_job {
 
        // Logging
        struct {
+               // File
+               struct pakfire_log_file* file;
+
                // Buffer
                struct pakfire_log_buffer* buffer;
 
@@ -301,6 +305,8 @@ ERROR:
 static int pakfire_job_finished(struct pakfire_job* job, int status) {
        struct pakfire_xfer* xfer = NULL;
        char job_id[UUID_STR_LEN];
+       const char* path = NULL;
+       char* logfile = NULL;
        int r;
 
        // Format the job ID as string
@@ -317,6 +323,21 @@ static int pakfire_job_finished(struct pakfire_job* job, int status) {
                }
        }
 
+       // Fetch the path of the log file
+       path = pakfire_log_file_path(job->log.file);
+       if (!path) {
+               ERROR(job->ctx, "Log file has no path\n");
+               r = -EINVAL;
+               goto ERROR;
+       }
+
+       // Upload the log file
+       r = pakfire_buildservice_upload(job->service, path, NULL, &logfile);
+       if (r < 0) {
+               ERROR(job->ctx, "Could not upload the log file: %s\n", strerror(-r));
+               goto ERROR;
+       }
+
        // Create a new xfer
        r = pakfire_job_xfer_create(&xfer, job, "/api/v1/jobs/%s/finished", job_id);
        if (r < 0)
@@ -332,14 +353,12 @@ static int pakfire_job_finished(struct pakfire_job* job, int status) {
        if (r < 0)
                goto ERROR;
 
-#if 0
        // Logfile
        if (logfile) {
                r = pakfire_xfer_add_param(xfer, "logfile", "%s", logfile);
                if (r)
                        goto ERROR;
        }
-#endif
 
        // Packages
        for (char** upload = job->uploads; *upload; upload++) {
@@ -566,7 +585,6 @@ static void pakfire_job_log(void* data, int priority, const char* file,
                int line, const char* fn, const char* format, va_list args) {
        struct pakfire_job* job = data;
        char* buffer = NULL;
-       int r;
 
        // Format the line
        const ssize_t length = vasprintf(&buffer, format, args);
@@ -590,6 +608,18 @@ static void pakfire_job_log(void* data, int priority, const char* file,
                        break;
        }
 
+       // Send everything but debug messages to the log file
+       if (likely(job->log.file)) {
+               switch (priority) {
+                       case LOG_DEBUG:
+                               break;
+
+                       default:
+                               pakfire_log_file_write(job->log.file, buffer, length);
+                               break;
+               }
+       }
+
        // Pass everything on to syslog
        pakfire_log_syslog(NULL, priority, file, line, fn, format, args);
 }
@@ -632,6 +662,13 @@ static int pakfire_job_child(struct pakfire_job* job) {
        // Setup logging
        pakfire_ctx_set_log_callback(ctx, pakfire_job_log, job);
 
+       // Open a new log file
+       r = pakfire_log_file_create(&job->log.file, ctx, PAKFIRE_LOG_FILE_COMPRESS);
+       if (r < 0) {
+               ERROR(ctx, "Could not open log file: %s\n", strerror(-r));
+               goto ERROR;
+       }
+
        // Disable the ccache
        if (!(job->flags & PAKFIRE_JOB_CCACHE))
                build_flags |= PAKFIRE_BUILD_DISABLE_CCACHE;
@@ -660,12 +697,21 @@ static int pakfire_job_child(struct pakfire_job* job) {
        // Run the build
        r = pakfire_build_exec(build, job->pkg, pakfire_job_result, job);
 
+       // Close the log file
+       r = pakfire_log_file_close(job->log.file);
+       if (r < 0) {
+               ERROR(ctx, "Could not close the log file: %s\n", strerror(-r));
+               goto ERROR;
+       }
+
        // Signal that the job has finished
        r = pakfire_job_finished(job, r);
        if (r < 0)
                goto ERROR;
 
 ERROR:
+       if (job->log.file)
+               pakfire_log_file_unref(job->log.file);
        if (build)
                pakfire_build_unref(build);
        if (ctx)