Created
December 22, 2022 23:56
-
-
Save nokute78/d85e028e06e918f8cc89b3ff9b0a575b to your computer and use it in GitHub Desktop.
sd_journal_open_directory example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
gcc -lsystemd sd_journal_open_directory_example.c