/* cc -o t-journal-tail t-journal-tail.c -lsystemd-journal */ #include #include #include void check (int r) { if (r < 0) { fprintf (stderr, "%s\n", strerror(-r)); exit (1); } } void checkv (const char *label, int r) { if (r < 0) { fprintf (stderr, "%s: %s\n", label, strerror(-r)); exit (1); } fprintf (stderr, "%s: %d\n", label, r); } char * dump (sd_journal *j, int count) { uint64_t last_usec = 0; char *cursor; int i = 0; while (i < count) { const void *data; size_t size; uint64_t usec; int r; check (sd_journal_get_realtime_usec (j, &usec)); check (sd_journal_get_data (j, "MESSAGE", &data, &size)); // printf ("%lld %.*s\n", usec, size, data); if (usec < last_usec) printf ("back\n"); last_usec = usec; i++; if (i >= count) break; check (r = sd_journal_next (j)); if (r == 0) { printf("%d entries, EOF\n", i); return NULL; } } check (sd_journal_get_cursor (j, &cursor)); printf("%d entries, %s\n", i, cursor); return cursor; } void match (sd_journal *j, const char *m) { check (sd_journal_add_match (j, m, strlen (m))); } void main () { sd_journal *j; check (sd_journal_open_directory (&j, "/home/mvo/work/XXX/var/log/journal", 0)); match (j, "_SYSTEMD_UNIT=NetworkManager.service"); check (sd_journal_seek_tail (j)); check (sd_journal_previous_skip (j, 10)); while (1) { char *c = dump (j, 2000); if (c == NULL) break; check (sd_journal_seek_cursor (j, c)); check (sd_journal_next (j)); check (sd_journal_test_cursor (j, c)); free (c); } }