Skip to content

Instantly share code, notes, and snippets.

@nokute78
Created December 22, 2022 23:56
Show Gist options
  • Save nokute78/d85e028e06e918f8cc89b3ff9b0a575b to your computer and use it in GitHub Desktop.
Save nokute78/d85e028e06e918f8cc89b3ff9b0a575b to your computer and use it in GitHub Desktop.
sd_journal_open_directory example
/* SPDX-License-Identifier: MIT-0 */
/* Based https://www.freedesktop.org/software/systemd/man/sd_journal_next.html */
#include <errno.h>
#include <stdio.h>
#include <systemd/sd-journal.h>
int main(int argc, char *argv[]) {
int r;
sd_journal *j;
if (argc < 2) {
fprintf(stderr, "arg error\n");
return 1;
}
r = sd_journal_open_directory(&j, argv[1], SD_JOURNAL_SYSTEM);
if (r < 0) {
errno = -r;
fprintf(stderr, "Failed to open journal: %m\n");
return 1;
}
SD_JOURNAL_FOREACH(j) {
const char *d;
size_t l;
r = sd_journal_get_data(j, "MESSAGE", (const void **)&d, &l);
if (r < 0) {
errno = -r;
fprintf(stderr, "Failed to read message field: %m\n");
continue;
}
printf("%.*s\n", (int) l, d);
}
sd_journal_close(j);
return 0;
}
@nokute78
Copy link
Author

gcc -lsystemd sd_journal_open_directory_example.c

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment