systemd/man/journal-stream-fd.c
Zbigniew Jędrzejewski-Szmek b4096cecff man: recommend %m over strerror()
The need to set errno is very very ugly, but at least it is thread-safe and
works correctly. Using strerror() is likely to be wrong, so let's not recommend
that. People who do a lot of logging would provide use some wrapper that sets
errno like we do, so nudge people towards %m.

I tested that all the separate .c files compile cleanly.
2022-10-11 16:59:00 +02:00

30 lines
643 B
C

/* SPDX-License-Identifier: CC0-1.0 */
#include <errno.h>
#include <syslog.h>
#include <stdio.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) {
errno = -fd;
fprintf(stderr, "Failed to create stream fd: %m\n");
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;
}