From 29c45dc4348e7db61aa80ba1657cbc2d8b1a19ee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= Date: Mon, 10 Oct 2022 09:18:26 +0200 Subject: [PATCH] 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. --- man/journal-enumerate-fields.c | 21 +++++++++++++++++++++ man/journal-iterate-foreach.c | 29 +++++++++++++++++++++++++++++ man/journal-stream-fd.c | 28 ++++++++++++++++++++++++++++ man/sd_journal_enumerate_fields.xml | 21 +-------------------- man/sd_journal_next.xml | 29 +---------------------------- man/sd_journal_stream_fd.xml | 28 +--------------------------- 6 files changed, 81 insertions(+), 75 deletions(-) create mode 100644 man/journal-enumerate-fields.c create mode 100644 man/journal-iterate-foreach.c create mode 100644 man/journal-stream-fd.c diff --git a/man/journal-enumerate-fields.c b/man/journal-enumerate-fields.c new file mode 100644 index 00000000000..8a357854400 --- /dev/null +++ b/man/journal-enumerate-fields.c @@ -0,0 +1,21 @@ +/* SPDX-License-Identifier: CC0-1.0 */ + +#include +#include +#include + +int main(int argc, char *argv[]) { + sd_journal *j; + const char *field; + int r; + + 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_FIELD(j, field) + printf("%s\n", field); + sd_journal_close(j); + return 0; +} diff --git a/man/journal-iterate-foreach.c b/man/journal-iterate-foreach.c new file mode 100644 index 00000000000..ae53d467edd --- /dev/null +++ b/man/journal-iterate-foreach.c @@ -0,0 +1,29 @@ +/* SPDX-License-Identifier: CC0-1.0 */ + +#include +#include +#include + +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; +} diff --git a/man/journal-stream-fd.c b/man/journal-stream-fd.c new file mode 100644 index 00000000000..6bc55821892 --- /dev/null +++ b/man/journal-stream-fd.c @@ -0,0 +1,28 @@ +/* SPDX-License-Identifier: CC0-1.0 */ + +#include +#include +#include +#include +#include +#include + +int main(int argc, char *argv[]) { + int fd; + FILE *log; + fd = sd_journal_stream_fd("test", LOG_INFO, 1); + if (fd < 0) { + fprintf(stderr, "Failed to create stream fd: %s\n", strerror(-fd)); + return 1; + } + log = fdopen(fd, "w"); + if (!log) { + fprintf(stderr, "Failed to create file object: %m\n"); + close(fd); + return 1; + } + fprintf(log, "Hello World!\n"); + fprintf(log, SD_WARNING "This is a warning!\n"); + fclose(log); + return 0; +} diff --git a/man/sd_journal_enumerate_fields.xml b/man/sd_journal_enumerate_fields.xml index e074906980c..2d8055ec898 100644 --- a/man/sd_journal_enumerate_fields.xml +++ b/man/sd_journal_enumerate_fields.xml @@ -94,26 +94,7 @@ Use the SD_JOURNAL_FOREACH_FIELD() macro to iterate through all field names in use in the current journal. - #include <stdio.h> -#include <string.h> -#include <systemd/sd-journal.h> - -int main(int argc, char *argv[]) { - sd_journal *j; - const char *field; - int r; - - 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_FIELD(j, field) - printf("%s\n", field); - sd_journal_close(j); - return 0; -} - + diff --git a/man/sd_journal_next.xml b/man/sd_journal_next.xml index e0f0a049749..628abb296cb 100644 --- a/man/sd_journal_next.xml +++ b/man/sd_journal_next.xml @@ -129,34 +129,7 @@ Iterating through the journal: - #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; -} - + diff --git a/man/sd_journal_stream_fd.xml b/man/sd_journal_stream_fd.xml index af2234e77d6..40d28041211 100644 --- a/man/sd_journal_stream_fd.xml +++ b/man/sd_journal_stream_fd.xml @@ -100,33 +100,7 @@ Creating a log stream suitable for fprintf3: - #include <syslog.h> -#include <stdio.h> -#include <string.h> -#include <unistd.h> -#include <systemd/sd-journal.h> -#include <systemd/sd-daemon.h> - -int main(int argc, char *argv[]) { - int fd; - FILE *log; - fd = sd_journal_stream_fd("test", LOG_INFO, 1); - if (fd < 0) { - fprintf(stderr, "Failed to create stream fd: %s\n", strerror(-fd)); - return 1; - } - log = fdopen(fd, "w"); - if (!log) { - fprintf(stderr, "Failed to create file object: %m\n"); - close(fd); - return 1; - } - fprintf(log, "Hello World!\n"); - fprintf(log, SD_WARNING "This is a warning!\n"); - fclose(log); - return 0; -} - +