#include <errno.h>
#include <string.h>
#include <sys/queue.h>
+#include <sys/time.h>
#include <pakfire/ctx.h>
#include <pakfire/log_buffer.h>
struct pakfire_log_line {
STAILQ_ENTRY(pakfire_log_line) nodes;
+ struct timeval timestamp;
int priority;
char* line;
size_t length;
}
static int pakfire_log_buffer_drop(struct pakfire_log_buffer* self) {
+ struct timeval timestamp = {};
int priority = 0;
char* line = NULL;
int r;
// Dequeue one line
- r = pakfire_log_buffer_dequeue(self, &priority, &line, NULL);
+ r = pakfire_log_buffer_dequeue(self, ×tamp, &priority, &line, NULL);
if (r < 0)
return r;
if (!l)
return -errno;
+ // Store the current time
+ r = gettimeofday(&l->timestamp, NULL);
+ if (r < 0) {
+ ERROR(buffer->ctx, "Could not determine the current time: %m\n");
+ r = -errno;
+ goto ERROR;
+ }
+
// Store the priority
l->priority = priority;
return -errno;
}
-int pakfire_log_buffer_dequeue(struct pakfire_log_buffer* buffer, int* priority, char** line, size_t* length) {
+int pakfire_log_buffer_dequeue(struct pakfire_log_buffer* buffer,
+ struct timeval* timestamp, int* priority, char** line, size_t* length) {
struct pakfire_log_line* l = NULL;
- // Priority & Line must be set, length is optional
- if (!priority || !line)
+ // Timestamp, priority & line must be set, length is optional
+ if (!timestamp || !priority || !line)
return -EINVAL;
// Fetch the first line
l = STAILQ_FIRST(&buffer->lines);
if (!l) {
- // Reset all pointers
+ // Reset all values
+ (*timestamp).tv_sec = (*timestamp).tv_usec = 0;
*priority = -1;
*line = NULL;
return 0;
}
+ // Return the timestamp
+ *timestamp = l->timestamp;
+
// Return the priority
*priority = l->priority;
# #
#############################################################################*/
+#include <sys/time.h>
+
#include <pakfire/log_buffer.h>
#include "../testsuite.h"
static int test_simple(const struct test* t) {
struct pakfire_log_buffer* buffer = NULL;
+ struct timeval timestamp = {};
char* line = NULL;
size_t length = 0;
int priority = 0;
ASSERT_SUCCESS(pakfire_log_buffer_enqueue(buffer, LOG_DEBUG, "CCC", -1));
// Dequeue the first string
- ASSERT_SUCCESS(pakfire_log_buffer_dequeue(buffer, &priority, &line, &length));
+ ASSERT_SUCCESS(pakfire_log_buffer_dequeue(buffer, ×tamp, &priority, &line, &length));
ASSERT_EQUALS(priority, LOG_DEBUG);
ASSERT_STRING_EQUALS(line, "A");
free(line);
// Dequeue the second string
- ASSERT_SUCCESS(pakfire_log_buffer_dequeue(buffer, &priority, &line, &length));
+ ASSERT_SUCCESS(pakfire_log_buffer_dequeue(buffer, ×tamp, &priority, &line, &length));
ASSERT_EQUALS(priority, LOG_DEBUG);
ASSERT_STRING_EQUALS(line, "BB");
free(line);
// Dequeue the third string
- ASSERT_SUCCESS(pakfire_log_buffer_dequeue(buffer, &priority, &line, &length));
+ ASSERT_SUCCESS(pakfire_log_buffer_dequeue(buffer, ×tamp, &priority, &line, &length));
ASSERT_EQUALS(priority, LOG_DEBUG);
ASSERT_STRING_EQUALS(line, "CCC");
free(line);
// Dequeue more than we put in
- ASSERT_SUCCESS(pakfire_log_buffer_dequeue(buffer, &priority, &line, &length));
+ ASSERT_SUCCESS(pakfire_log_buffer_dequeue(buffer, ×tamp, &priority, &line, &length));
ASSERT_EQUALS(priority, -1);
ASSERT_EQUALS(line, NULL);
static int test_wrong_usage(const struct test* t) {
struct pakfire_log_buffer* buffer = NULL;
+ struct timeval timestamp = {};
int priority = -1;
char* line = NULL;
int r = EXIT_FAILURE;
ASSERT_ERROR(pakfire_log_buffer_enqueue(buffer, LOG_DEBUG, NULL, -1), EINVAL);
// Wrong dequeue
- ASSERT_ERROR(pakfire_log_buffer_dequeue(buffer, NULL, &line, NULL), EINVAL);
- ASSERT_ERROR(pakfire_log_buffer_dequeue(buffer, &priority, NULL, NULL), EINVAL);
+ ASSERT_ERROR(pakfire_log_buffer_dequeue(buffer, ×tamp, NULL, &line, NULL), EINVAL);
+ ASSERT_ERROR(pakfire_log_buffer_dequeue(buffer, ×tamp, &priority, NULL, NULL), EINVAL);
+ ASSERT_ERROR(pakfire_log_buffer_dequeue(buffer, NULL, &priority, NULL, NULL), EINVAL);
// Everything passed
r = EXIT_SUCCESS;
static int test_full(const struct test* t) {
struct pakfire_log_buffer* buffer = NULL;
+ struct timeval timestamp = {};
char* line = NULL;
size_t length = 0;
int priority = 0;
ASSERT_SUCCESS(pakfire_log_buffer_enqueue(buffer, LOG_DEBUG, "Line 3", -1));
// On dequeue we should now miss Line 1, but get Line 2 instead
- ASSERT_SUCCESS(pakfire_log_buffer_dequeue(buffer, &priority, &line, &length));
+ ASSERT_SUCCESS(pakfire_log_buffer_dequeue(buffer, ×tamp, &priority, &line, &length));
ASSERT_STRING_EQUALS(line, "Line 2");
// On the next iteration, we should get Line 3
- ASSERT_SUCCESS(pakfire_log_buffer_dequeue(buffer, &priority, &line, &length));
+ ASSERT_SUCCESS(pakfire_log_buffer_dequeue(buffer, ×tamp, &priority, &line, &length));
ASSERT_STRING_EQUALS(line, "Line 3");
// On the next iteration, the buffer should be empty
- ASSERT_SUCCESS(pakfire_log_buffer_dequeue(buffer, &priority, &line, &length));
+ ASSERT_SUCCESS(pakfire_log_buffer_dequeue(buffer, ×tamp, &priority, &line, &length));
ASSERT(line == NULL);
// Everything passed