From: Michael Tremer Date: Wed, 29 Jan 2025 16:49:03 +0000 (+0000) Subject: job: Create a log file and write the entire job output to it X-Git-Tag: 0.9.30~295 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=da0f77f6fa07b4169fc44b19c95e8a383ae04ba7;p=pakfire.git job: Create a log file and write the entire job output to it Signed-off-by: Michael Tremer --- diff --git a/src/pakfire/job.c b/src/pakfire/job.c index 4d0151fa..dffdcba4 100644 --- a/src/pakfire/job.c +++ b/src/pakfire/job.c @@ -38,6 +38,7 @@ #include #include #include +#include #include #include #include @@ -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)