diff options
author | Zbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl> | 2022-10-10 09:18:26 +0200 |
---|---|---|
committer | Zbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl> | 2022-10-11 16:59:00 +0200 |
commit | 29c45dc4348e7db61aa80ba1657cbc2d8b1a19ee (patch) | |
tree | 82cedba74276d810467c79a990d7952b90852e8a /man/journal-iterate-foreach.c | |
parent | 0cf1a4b3a7e0b870912ec0f986aa8107309a761e (diff) | |
download | systemd-29c45dc4348e7db61aa80ba1657cbc2d8b1a19ee.tar.gz |
man: use external .c files for three examples
This way it's much easier to test that the code compiles without issues.
It's also easier to edit the code.
Indentation in one of the examples is reduced to two spaces. This is what we
use in man pages to make them fit on screen better.
Diffstat (limited to 'man/journal-iterate-foreach.c')
-rw-r--r-- | man/journal-iterate-foreach.c | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/man/journal-iterate-foreach.c b/man/journal-iterate-foreach.c new file mode 100644 index 0000000000..ae53d467ed --- /dev/null +++ b/man/journal-iterate-foreach.c @@ -0,0 +1,29 @@ +/* SPDX-License-Identifier: CC0-1.0 */ + +#include <stdio.h> +#include <string.h> +#include <systemd/sd-journal.h> + +int main(int argc, char *argv[]) { + int r; + sd_journal *j; + r = sd_journal_open(&j, SD_JOURNAL_LOCAL_ONLY); + if (r < 0) { + fprintf(stderr, "Failed to open journal: %s\n", strerror(-r)); + 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) { + fprintf(stderr, "Failed to read message field: %s\n", strerror(-r)); + continue; + } + + printf("%.*s\n", (int) l, d); + } + sd_journal_close(j); + return 0; +} |