#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 int system_journal_open(JournalFile **f) {
- int r;
- char *fn;
- sd_id128_t machine;
- char ids[33];
+static bool arg_follow = true;
- assert(f);
-
- r = sd_id128_get_machine(&machine);
- if (r < 0)
- return r;
-
- fn = join("/var/log/journal/", sd_id128_to_string(machine, ids), "/system.journal", NULL);
- if (!fn)
- return -ENOMEM;
+int main(int argc, char *argv[]) {
+ int r, i, fd;
+ sd_journal *j = NULL;
- r = journal_file_open(fn, O_RDONLY, 0640, NULL, f);
- free(fn);
+ log_set_max_level(LOG_DEBUG);
+ log_set_target(LOG_TARGET_CONSOLE);
- if (r >= 0)
- return r;
+ log_parse_environment();
+ log_open();
- if (r < 0 && r != -ENOENT) {
- log_error("Failed to open system journal: %s", strerror(-r));
- return r;
+ r = sd_journal_open(&j);
+ if (r < 0) {
+ log_error("Failed to open journal: %s", strerror(-r));
+ goto finish;
}
- fn = join("/run/log/journal/", ids, "/system.journal", NULL);
- if (!fn)
- return -ENOMEM;
+ for (i = 1; i < argc; i++) {
+ r = sd_journal_add_match(j, argv[i], strlen(argv[i]));
+ if (r < 0) {
+ log_error("Failed to add match: %s", strerror(-r));
+ goto finish;
+ }
+ }
- r = journal_file_open(fn, O_RDONLY, 0640, NULL, f);
- free(fn);
+ fd = sd_journal_get_fd(j);
+ if (fd < 0) {
+ log_error("Failed to get wakeup fd: %s", strerror(-fd));
+ goto finish;
+ }
+ r = sd_journal_seek_head(j);
if (r < 0) {
- log_error("Failed to open system journal: %s", strerror(-r));
- return r;
+ log_error("Failed to seek to head: %s", strerror(-r));
+ goto finish;
}
- return r;
-}
+ for (;;) {
+ struct pollfd pollfd;
-int main(int argc, char *argv[]) {
- int r;
- JournalFile *f;
- Object *o = NULL;
+ while (sd_journal_next(j) > 0) {
+ const void *data;
+ size_t length;
+ char *cursor;
+ uint64_t realtime = 0, monotonic = 0;
- log_parse_environment();
- log_open();
+ r = sd_journal_get_cursor(j, &cursor);
+ if (r < 0) {
+ log_error("Failed to get cursor: %s", strerror(-r));
+ goto finish;
+ }
- r = system_journal_open(&f);
- if (r < 0) {
- log_error("Failed to open journal: %s", strerror(-r));
- return EXIT_FAILURE;
- }
+ printf("entry: %s\n", cursor);
+ free(cursor);
- for (;;) {
- uint64_t offset;
- uint64_t n, i;
+ 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);
- r = journal_file_next_entry(f, o, &o, &offset);
- if (r < 0) {
- log_error("Failed to read journal: %s", strerror(-r));
- goto finish;
+ SD_JOURNAL_FOREACH_DATA(j, data, length)
+ printf("\t%.*s\n", (int) length, (const char*) data);
}
- if (r == 0)
+ if (!arg_follow)
break;
- printf("entry: %llu\n", (unsigned long long) le64toh(o->entry.seqnum));
+ zero(pollfd);
+ pollfd.fd = fd;
+ pollfd.events = POLLIN;
- n = journal_file_entry_n_items(o);
- for (i = 0; i < n; i++) {
- uint64_t p, l;
-
- p = le64toh(o->entry.items[i].object_offset);
- r = journal_file_move_to_object(f, p, OBJECT_DATA, &o);
- if (r < 0) {
- log_error("Failed to move to data: %s", strerror(-r));
- goto finish;
- }
+ if (poll(&pollfd, 1, -1) < 0) {
+ if (errno == EINTR)
+ break;
- l = o->object.size - offsetof(Object, data.payload);
- printf("\t[%.*s]\n", (int) l, o->data.payload);
+ log_error("poll(): %m");
+ r = -errno;
+ goto finish;
+ }
- 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;
- }
+ 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;
}