SD\_JOURNAL\_STREAM\_FD(3) sd\_journal\_stream\_fd SD\_JOURNAL\_STREAM\_FD(3)

.SH "NAME" sd\_journal\_stream\_fd, sd\_journal\_stream\_fd\_with\_namespace - Create log stream file descriptor to the journal

.SH "SYNOPSIS"

```
#include <systemd/sd-journal.h>
```

.HP 600u **int sd\_journal\_stream\_fd(const char \*identifier, int priority, int level\_prefix);**

.HP 960u **int sd\_journal\_stream\_fd\_with\_namespace(const char \*name\_space, const char \*identifier, int priority, int level\_prefix);**

.SH "DESCRIPTION"

.PP **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.

.PP **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)](/man/os/systemd/systemd.journal-fields.7) for more information). The second argument shall be the default priority level for all messages. The priority level is one of **LOG\_EMERG**, **LOG\_ALERT**, **LOG\_CRIT**, **LOG\_ERR**, **LOG\_WARNING**, **LOG\_NOTICE**, **LOG\_INFO**, **LOG\_DEBUG**, as defined in syslog.h, see [**syslog**(3)](/man/os/systemd/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)](/man/os/systemd/sd-daemon.3) for more information.

.PP **sd\_journal\_stream\_fd\_with\_namespace()** is similar to **sd\_journal\_stream\_fd()**, but takes an additional *name\_space* parameter that specifies which journal namespace to operate on. If specified as **NULL** the call is identical to **sd\_journal\_stream\_fd()**. For details about journal namespaces, see [**systemd-journald.service**(8)](/man/os/systemd/systemd-journald.service.8).

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

.PP Each invocation of these functions 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.

.SH "RETURN VALUE"

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

.SH "SIGNAL SAFETY"

.PP **sd\_journal\_stream\_fd()** and **sd\_journal\_stream\_fd\_with\_namespace()** are "async signal safe" in the meaning of [**signal-safety**(7)](/man/os/systemd/signal-safety.7).

.SH "NOTES"

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

.PP Functions described here are available as a shared library, which can be compiled against and linked to with the **libsystemd** [**pkg-config**(1)](/man/os/systemd/pkg-config.1) file.

.SH "EXAMPLES"

.PP Creating a log stream suitable for [**fprintf**(3)](/man/os/systemd/fprintf.3):

```
/* SPDX-License-Identifier: MIT-0 */
#define _GNU_SOURCE 1
#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) {


    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: %s\n", strerror(errno));


    close(fd);


    return 1;


  }


  fprintf(log, "Hello World!\n");


  fprintf(log, SD_WARNING "This is a warning!\n");


  fclose(log);


  return 0;
}
```

.SH "HISTORY"

.PP **sd\_journal\_stream\_fd()** was added in version 187.

.PP **sd\_journal\_stream\_fd\_with\_namespace()** was added in version 256.

.SH "SEE ALSO"

.PP [**systemd**(1)](/man/os/systemd/systemd.1), [**sd-journal**(3)](/man/os/systemd/sd-journal.3), [**sd-daemon**(3)](/man/os/systemd/sd-daemon.3), [**sd\_journal\_print**(3)](/man/os/systemd/sd_journal_print.3), [**syslog**(3)](/man/os/systemd/syslog.3), [**fprintf**(3)](/man/os/systemd/fprintf.3), [**systemd.journal-fields**(7)](/man/os/systemd/systemd.journal-fields.7)

systemd 262.2

---

Document: sd_journal_stream_fd(3) from systemd 262.2-r1 (Chainguard OS)
Source: https://github.com/systemd/systemd
