sd_journal_stream_fd(3) — Linux manual page


SD_JOURNAL_STREAM_FD(3)     sd_journal_stream_fd     SD_JOURNAL_STREAM_FD(3)

NAME         top

       sd_journal_stream_fd - Create log stream file descriptor to the

SYNOPSIS         top

       #include <systemd/sd-journal.h>

       int sd_journal_stream_fd(const char *identifier, int priority,
                                int level_prefix);

DESCRIPTION         top

       sd_journal_stream_fd() may be used to create a log stream file
       descriptor. Log messages written to this file descriptor as simple
       newline-separated text strings are written to the journal. This file
       descriptor can be used internally by applications or be made standard
       output or standard error of other processes executed.

       sd_journal_stream_fd() takes a short program identifier string as
       first argument, which will be written to the journal as
       SYSLOG_IDENTIFIER= field for each log entry (see
       systemd.journal-fields(7) for more information). The second argument
       shall be the default priority level for all messages. The priority
       LOG_NOTICE, LOG_INFO, LOG_DEBUG, as defined in syslog.h, see
       syslog(3) for details. The third argument is a boolean: if true
       kernel-style log level prefixes (such as SD_WARNING) are interpreted,
       see sd-daemon(3) for more information.

       It is recommended that applications log UTF-8 messages only with this
       API, but this is not enforced.

       Each invocation of sd_journal_stream_fd() allocates a new log stream
       file descriptor, that is not shared with prior or later invocations.
       The file descriptor is write-only (its reading direction is shut
       down), and O_NONBLOCK is turned off initially.

RETURN VALUE         top

       The call returns a valid write-only file descriptor on success or a
       negative errno-style error code.

SIGNAL SAFETY         top

       sd_journal_stream_fd() is "async signal safe" in the meaning of

NOTES         top

       All functions listed here are thread-safe and may be called in
       parallel from multiple threads.

       These APIs are implemented as a shared library, which can be compiled
       and linked to with the libsystemd pkg-config(1) file.

EXAMPLES         top

       Creating a log stream suitable for fprintf(3):

           #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");
               return 1;
             fprintf(log, "Hello World!\n");
             fprintf(log, SD_WARNING "This is a warning!\n");
             return 0;

SEE ALSO         top

       systemd(1), sd-journal(3), sd-daemon(3), sd_journal_print(3),
       syslog(3), fprintf(3), systemd.journal-fields(7)

COLOPHON         top

       This page is part of the systemd (systemd system and service manager)
       project.  Information about the project can be found at 
       ⟨⟩.  If you have a bug
       report for this manual page, see
       ⟨⟩.  This
       page was obtained from the project's upstream Git repository
       ⟨⟩ on 2020-09-18.  (At that
       time, the date of the most recent commit that was found in the repos‐
       itory was 2020-09-18.)  If you discover any rendering problems in
       this HTML version of the page, or you believe there is a better or
       more up-to-date source for the page, or you have corrections or
       improvements to the information in this COLOPHON (which is not part
       of the original manual page), send a mail to

systemd 246                                          SD_JOURNAL_STREAM_FD(3)

Pages that refer to this page: sd-journal(3)sd_journal_perror(3)sd_journal_perror_with_location(3)sd_journal_print(3)sd_journal_printv(3)sd_journal_printv_with_location(3)sd_journal_print_with_location(3)sd_journal_send(3)sd_journal_sendv(3)sd_journal_sendv_with_location(3)sd_journal_send_with_location(3)sd_journal_suppress_location(3)SD_JOURNAL_SUPPRESS_LOCATION(3)30-systemd-environment-d-generator(7)systemd.directives(7)systemd.index(7)