I am working with systemd journals to create a custom log processing program. I am trying to work with sd_journal APIs but I have a couple of questions:
Here is the sample code I am using:
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <systemd/sd-journal.h>
#include <systemd/sd-daemon.h>
int main(int argc, char *argv[]) {
int ret_val = 0;
int count = 0;
sd_journal *jd;
sd_journal_print(LOG_INFO, "Hello World, this is PID %lu!", (unsigned long) getpid());
do {
ret_val = sd_journal_open (&jd, SD_JOURNAL_SYSTEM | SD_JOURNAL_RUNTIME_ONLY | SD_JOURNAL_LOCAL_ONLY);
if (ret_val != 0) {
fprintf(stderr, "Failed to open journal: %s\n", strerror(-ret_val));
break;
}
printf ("Current Journal was loaded successfully!\n");
const void *d;
size_t l;
SD_JOURNAL_FOREACH_DATA (jd, d, l) {
printf("%.*s\n", (int)l, (const char*) d);
count++;
}
sd_journal_close(jd);
printf ("# of Journal entries read: %d\n", count);
} while (0);
return 0;
}
It took me a while to figure out but the problem was fairly simple. The problem was in the use of
SD_JOURNAL_RUNTIME_ONLY
while the journald
storage was specified as persistent.
To me, it was non-intuitive that persistent journals doesn't go to runtime journal buffer. So the only way to simulate the functionality of journalctl -f
was by opening the local journals and seek the tail.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With