#include <fcntl.h>
#include <errno.h>
#include <stddef.h>
+#include <string.h>
+#include <stdio.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include <sys/poll.h>
-#include "journal-file.h"
+#include "sd-journal.h"
+#include "log.h"
+
+static bool arg_follow = true;
int main(int argc, char *argv[]) {
- int r;
- JournalFile *f;
- Object *o = NULL;
+ int r, i, fd;
+ sd_journal *j = NULL;
+
+ log_set_max_level(LOG_DEBUG);
+ log_set_target(LOG_TARGET_CONSOLE);
log_parse_environment();
log_open();
- r = journal_file_open("/var/log/journal/system.journal", O_RDONLY, 0644, &f);
- if (r == -ENOENT)
- r = journal_file_open("/run/log/journal/system.journal", O_RDONLY, 0644, &f);
-
+ r = sd_journal_open(&j);
if (r < 0) {
log_error("Failed to open journal: %s", strerror(-r));
- return EXIT_FAILURE;
+ goto finish;
}
- for (;;) {
- uint64_t offset;
- uint64_t n, i;
-
- r = journal_file_next_entry(f, o, &o, &offset);
+ for (i = 1; i < argc; i++) {
+ r = sd_journal_add_match(j, argv[i], strlen(argv[i]));
if (r < 0) {
- log_error("Failed to read journal: %s", strerror(-r));
+ log_error("Failed to add match: %s", strerror(-r));
goto finish;
}
+ }
- if (r == 0)
- break;
+ fd = sd_journal_get_fd(j);
+ if (fd < 0) {
+ log_error("Failed to get wakeup fd: %s", strerror(-fd));
+ goto finish;
+ }
- printf("entry: %llu\n", (unsigned long long) le64toh(o->entry.seqnum));
+ r = sd_journal_seek_head(j);
+ if (r < 0) {
+ log_error("Failed to seek to head: %s", strerror(-r));
+ goto finish;
+ }
- n = journal_file_entry_n_items(o);
- for (i = 0; i < n; i++) {
- uint64_t p, l;
+ for (;;) {
+ struct pollfd pollfd;
- p = le64toh(o->entry.items[i].object_offset);
- r = journal_file_move_to_object(f, p, OBJECT_DATA, &o);
+ while (sd_journal_next(j) > 0) {
+ const void *data;
+ size_t length;
+ char *cursor;
+ uint64_t realtime = 0, monotonic = 0;
+
+ r = sd_journal_get_cursor(j, &cursor);
if (r < 0) {
- log_error("Failed to move to data: %s", strerror(-r));
+ log_error("Failed to get cursor: %s", strerror(-r));
goto finish;
}
- l = o->object.size - offsetof(Object, data.payload);
- printf("\t[%.*s]\n", (int) l, o->data.payload);
+ printf("entry: %s\n", cursor);
+ free(cursor);
- r = journal_file_move_to_object(f, offset, OBJECT_ENTRY, &o);
- if (r < 0) {
- log_error("Failed to move back to entry: %s", strerror(-r));
- goto finish;
- }
+ sd_journal_get_realtime_usec(j, &realtime);
+ sd_journal_get_monotonic_usec(j, &monotonic, NULL);
+ printf("realtime: %llu\n"
+ "monotonic: %llu\n",
+ (unsigned long long) realtime,
+ (unsigned long long) monotonic);
+
+ SD_JOURNAL_FOREACH_DATA(j, data, length)
+ printf("\t%.*s\n", (int) length, (const char*) data);
+ }
+
+ if (!arg_follow)
+ break;
+
+ zero(pollfd);
+ pollfd.fd = fd;
+ pollfd.events = POLLIN;
+
+ if (poll(&pollfd, 1, -1) < 0) {
+ if (errno == EINTR)
+ break;
+
+ log_error("poll(): %m");
+ r = -errno;
+ goto finish;
+ }
+
+ r = sd_journal_process(j);
+ if (r < 0) {
+ log_error("Failed to process: %s", strerror(-r));
+ goto finish;
}
}
finish:
- journal_file_close(f);
+ if (j)
+ sd_journal_close(j);
- return 0;
+ return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
}