| 1 |
/* SPDX-License-Identifier: MIT-0 */ |
| 2 |
|
| 3 |
#include <errno.h> |
| 4 |
#include <stdio.h> |
| 5 |
#include <systemd/sd-journal.h> |
| 6 |
|
| 7 |
int main(int argc, char *argv[]) { |
| 8 |
int r; |
| 9 |
sd_journal *j; |
| 10 |
|
| 11 |
r = sd_journal_open(&j, SD_JOURNAL_LOCAL_ONLY); |
| 12 |
if (r < 0) { |
| 13 |
fprintf(stderr, "Failed to open journal: %s\n", strerror(-r)); |
| 14 |
return 1; |
| 15 |
} |
| 16 |
|
| 17 |
for (;;) { |
| 18 |
const void *d; |
| 19 |
size_t l; |
| 20 |
r = sd_journal_next(j); |
| 21 |
if (r < 0) { |
| 22 |
fprintf(stderr, "Failed to iterate to next entry: %s\n", strerror(-r)); |
| 23 |
break; |
| 24 |
} |
| 25 |
if (r == 0) { |
| 26 |
/* Reached the end, let's wait for changes, and try again */ |
| 27 |
r = sd_journal_wait(j, (uint64_t) -1); |
| 28 |
if (r < 0) { |
| 29 |
fprintf(stderr, "Failed to wait for changes: %s\n", strerror(-r)); |
| 30 |
break; |
| 31 |
} |
| 32 |
continue; |
| 33 |
} |
| 34 |
r = sd_journal_get_data(j, "MESSAGE", &d, &l); |
| 35 |
if (r < 0) { |
| 36 |
fprintf(stderr, "Failed to read message field: %s\n", strerror(-r)); |
| 37 |
continue; |
| 38 |
} |
| 39 |
printf("%.*s\n", (int) l, (const char*) d); |
| 40 |
} |
| 41 |
|
| 42 |
sd_journal_close(j); |
| 43 |
return 0; |
| 44 |
} |