]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
man: use external .c files for three examples
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Mon, 10 Oct 2022 07:18:26 +0000 (09:18 +0200)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Tue, 11 Oct 2022 14:59:00 +0000 (16:59 +0200)
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 [new file with mode: 0644]
man/journal-iterate-foreach.c [new file with mode: 0644]
man/journal-stream-fd.c [new file with mode: 0644]
man/sd_journal_enumerate_fields.xml
man/sd_journal_next.xml
man/sd_journal_stream_fd.xml

diff --git a/man/journal-enumerate-fields.c b/man/journal-enumerate-fields.c
new file mode 100644 (file)
index 0000000..8a35785
--- /dev/null
@@ -0,0 +1,21 @@
+/* SPDX-License-Identifier: CC0-1.0 */
+
+#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/journal-iterate-foreach.c b/man/journal-iterate-foreach.c
new file mode 100644 (file)
index 0000000..ae53d46
--- /dev/null
@@ -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;
+}
diff --git a/man/journal-stream-fd.c b/man/journal-stream-fd.c
new file mode 100644 (file)
index 0000000..6bc5582
--- /dev/null
@@ -0,0 +1,28 @@
+/* SPDX-License-Identifier: CC0-1.0 */
+
+#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;
+}
index e074906980c94c4c2663eab2f9baeddd29a4d964..2d8055ec8986562244af2ec9bfa5c2cf3cdab658 100644 (file)
     <para>Use the <function>SD_JOURNAL_FOREACH_FIELD()</function> macro to iterate through all field names in use in the
     current journal.</para>
 
-    <programlisting>#include &lt;stdio.h&gt;
-#include &lt;string.h&gt;
-#include &lt;systemd/sd-journal.h&gt;
-
-int main(int argc, char *argv[]) {
-        sd_journal *j;
-        const char *field;
-        int r;
-
-        r = sd_journal_open(&amp;j, SD_JOURNAL_LOCAL_ONLY);
-        if (r &lt; 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;
-}</programlisting>
-
+    <programlisting><xi:include href="journal-enumerate-fields.c" parse="text" /></programlisting>
   </refsect1>
 
   <refsect1>
index e0f0a049749e2a9af5547a12ada54be11cbbf795..628abb296cb5044d782bb199f04369a7bbe2e88b 100644 (file)
 
     <para>Iterating through the journal:</para>
 
-    <programlisting>#include &lt;stdio.h&gt;
-#include &lt;string.h&gt;
-#include &lt;systemd/sd-journal.h&gt;
-
-int main(int argc, char *argv[]) {
-  int r;
-  sd_journal *j;
-  r = sd_journal_open(&amp;j, SD_JOURNAL_LOCAL_ONLY);
-  if (r &lt; 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 **)&amp;d, &amp;l);
-    if (r &lt; 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;
-}</programlisting>
-
+    <programlisting><xi:include href="journal-iterate-foreach.c" parse="text" /></programlisting>
   </refsect1>
 
   <refsect1>
index af2234e77d69c1db6f9cd6594c2bfa4450fcc249..40d28041211f06cfd56bcb86de4d213268b47851 100644 (file)
     <para>Creating a log stream suitable for
     <citerefentry project='man-pages'><refentrytitle>fprintf</refentrytitle><manvolnum>3</manvolnum></citerefentry>:</para>
 
-    <programlisting>#include &lt;syslog.h&gt;
-#include &lt;stdio.h&gt;
-#include &lt;string.h&gt;
-#include &lt;unistd.h&gt;
-#include &lt;systemd/sd-journal.h&gt;
-#include &lt;systemd/sd-daemon.h&gt;
-
-int main(int argc, char *argv[]) {
-  int fd;
-  FILE *log;
-  fd = sd_journal_stream_fd("test", LOG_INFO, 1);
-  if (fd &lt; 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;
-}</programlisting>
-
+    <programlisting><xi:include href="journal-stream-fd.c" parse="text" /></programlisting>
   </refsect1>
 
   <refsect1>