]> git.ipfire.org Git - pakfire.git/commitdiff
log buffer: Add a function to dump everything
authorMichael Tremer <michael.tremer@ipfire.org>
Sun, 26 Jan 2025 16:51:03 +0000 (16:51 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Sun, 26 Jan 2025 16:51:03 +0000 (16:51 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/pakfire/log_buffer.c
src/pakfire/log_buffer.h

index 0e592f52f490f0f888919e393ec062a8b8fcfad0..4da52eabbfa032bdd06b3fc1bf565017b5ac8f5a 100644 (file)
@@ -188,3 +188,43 @@ int pakfire_log_buffer_dequeue(struct pakfire_log_buffer* buffer, int* priority,
 
        return 0;
 }
+
+int pakfire_log_buffer_dump(struct pakfire_log_buffer* buffer, char** result, size_t* length) {
+       struct pakfire_log_line* line = NULL;
+       char* s = NULL;
+       size_t l = 0;
+       int r;
+
+       // Check inputs
+       if (!result || !length)
+               return -EINVAL;
+
+       // Compute the total length of the buffer that we will need
+       STAILQ_FOREACH(line, &buffer->lines, nodes) {
+               l += line->length;
+       }
+
+       // Nothing to do if the buffer is empty
+       if (!l)
+               return 0;
+
+       // Allocate a buffer that can fit the entire content
+       s = malloc(l);
+       if (!s)
+               return -errno;
+
+       char* p = s;
+
+       // Copy the entire content into the buffer
+       STAILQ_FOREACH(line, &buffer->lines, nodes) {
+               memcpy(p, line->line, line->length);
+
+               p += line->length;
+       }
+
+       // Return the buffer
+       *result = s;
+       *length = l;
+
+       return 0;
+}
index 3dc865eab842d4b98845fc782b560ed4a2abfcf0..9727a79a1e7aeb2f3d36cc11825527dce1312ac7 100644 (file)
@@ -36,4 +36,6 @@ struct pakfire_log_buffer* pakfire_log_buffer_unref(struct pakfire_log_buffer* b
 int pakfire_log_buffer_enqueue(struct pakfire_log_buffer* buffer, int priority, const char* line, ssize_t length);
 int pakfire_log_buffer_dequeue(struct pakfire_log_buffer* buffer, int* priority, char** line, size_t* length);
 
+int pakfire_log_buffer_dump(struct pakfire_log_buffer* buffer, char** result, size_t* length);
+
 #endif /* PAKFIRE_LOG_BUFFER_H */