mirror of
https://github.com/systemd/systemd.git
synced 2024-11-27 04:03:36 +08:00
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.
This commit is contained in:
parent
0cf1a4b3a7
commit
29c45dc434
21
man/journal-enumerate-fields.c
Normal file
21
man/journal-enumerate-fields.c
Normal file
@ -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;
|
||||
}
|
29
man/journal-iterate-foreach.c
Normal file
29
man/journal-iterate-foreach.c
Normal file
@ -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;
|
||||
}
|
28
man/journal-stream-fd.c
Normal file
28
man/journal-stream-fd.c
Normal file
@ -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;
|
||||
}
|
@ -94,26 +94,7 @@
|
||||
<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 <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;
|
||||
}</programlisting>
|
||||
|
||||
<programlisting><xi:include href="journal-enumerate-fields.c" parse="text" /></programlisting>
|
||||
</refsect1>
|
||||
|
||||
<refsect1>
|
||||
|
@ -129,34 +129,7 @@
|
||||
|
||||
<para>Iterating through the journal:</para>
|
||||
|
||||
<programlisting>#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;
|
||||
}</programlisting>
|
||||
|
||||
<programlisting><xi:include href="journal-iterate-foreach.c" parse="text" /></programlisting>
|
||||
</refsect1>
|
||||
|
||||
<refsect1>
|
||||
|
@ -100,33 +100,7 @@
|
||||
<para>Creating a log stream suitable for
|
||||
<citerefentry project='man-pages'><refentrytitle>fprintf</refentrytitle><manvolnum>3</manvolnum></citerefentry>:</para>
|
||||
|
||||
<programlisting>#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;
|
||||
}</programlisting>
|
||||
|
||||
<programlisting><xi:include href="journal-stream-fd.c" parse="text" /></programlisting>
|
||||
</refsect1>
|
||||
|
||||
<refsect1>
|
||||
|
Loading…
Reference in New Issue
Block a user