1 <?xml version='
1.0'
?> <!--*-nxml-*-->
2 <!DOCTYPE refentry PUBLIC
"-//OASIS//DTD DocBook XML V4.2//EN"
3 "http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd">
6 SPDX-License-Identifier: LGPL-2.1+
8 This file is part of systemd.
10 Copyright 2012 Lennart Poettering
12 systemd is free software; you can redistribute it and/or modify it
13 under the terms of the GNU Lesser General Public License as published by
14 the Free Software Foundation; either version 2.1 of the License, or
15 (at your option) any later version.
17 systemd is distributed in the hope that it will be useful, but
18 WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 Lesser General Public License for more details.
22 You should have received a copy of the GNU Lesser General Public License
23 along with systemd; If not, see <http://www.gnu.org/licenses/>.
26 <refentry id=
"sd_journal_stream_fd">
29 <title>sd_journal_stream_fd
</title>
30 <productname>systemd
</productname>
34 <contrib>Developer
</contrib>
35 <firstname>Lennart
</firstname>
36 <surname>Poettering
</surname>
37 <email>lennart@poettering.net
</email>
43 <refentrytitle>sd_journal_stream_fd
</refentrytitle>
44 <manvolnum>3</manvolnum>
48 <refname>sd_journal_stream_fd
</refname>
49 <refpurpose>Create log stream file descriptor to the journal
</refpurpose>
54 <funcsynopsisinfo>#include
<systemd/sd-journal.h
></funcsynopsisinfo>
57 <funcdef>int
<function>sd_journal_stream_fd
</function></funcdef>
58 <paramdef>const char *
<parameter>identifier
</parameter></paramdef>
59 <paramdef>int
<parameter>priority
</parameter></paramdef>
60 <paramdef>int
<parameter>level_prefix
</parameter></paramdef>
67 <title>Description
</title>
69 <para><function>sd_journal_stream_fd()
</function> may be used to
70 create a log stream file descriptor. Log messages written to this
71 file descriptor as simple newline-separated text strings are
72 written to the journal. This file descriptor can be used
73 internally by applications or be made standard output or standard
74 error of other processes executed.
</para>
76 <para><function>sd_journal_stream_fd()
</function> takes a short
77 program identifier string as first argument, which will be written
78 to the journal as _SYSLOG_IDENTIFIER= field for each log entry
80 <citerefentry><refentrytitle>systemd.journal-fields
</refentrytitle><manvolnum>7</manvolnum></citerefentry>
81 for more information). The second argument shall be the default
82 priority level for all messages. The priority level is one of
83 <constant>LOG_EMERG
</constant>,
<constant>LOG_ALERT
</constant>,
84 <constant>LOG_CRIT
</constant>,
<constant>LOG_ERR
</constant>,
85 <constant>LOG_WARNING
</constant>,
<constant>LOG_NOTICE
</constant>,
86 <constant>LOG_INFO
</constant>,
<constant>LOG_DEBUG
</constant>, as
87 defined in
<filename>syslog.h
</filename>, see
88 <citerefentry project='man-pages'
><refentrytitle>syslog
</refentrytitle><manvolnum>3</manvolnum></citerefentry>
89 for details. The third argument is a boolean: if true kernel-style
90 log level prefixes (such as
<constant>SD_WARNING
</constant>) are
92 <citerefentry><refentrytitle>sd-daemon
</refentrytitle><manvolnum>3</manvolnum></citerefentry>
93 for more information.
</para>
95 <para>It is recommended that applications log UTF-
8 messages only
96 with this API, but this is not enforced.
</para>
98 <para>Each invocation of
<function>sd_journal_stream_fd()
</function> allocates a new log stream file descriptor,
99 that is not shared with prior or later invocations. The file descriptor is write-only (its reading direction is
100 shut down), and
<constant>O_NONBLOCK
</constant> is turned off initially.
</para>
104 <title>Return Value
</title>
106 <para>The call returns a valid write-only file descriptor on
107 success or a negative errno-style error code.
</para>
111 <title>Signal safety
</title>
113 <para><function>sd_journal_stream_fd()
</function> is
"async signal safe" in the meaning of
<citerefentry
114 project='man-pages'
><refentrytitle>signal-safety
</refentrytitle><manvolnum>7</manvolnum></citerefentry>.
121 <para>Function
<function>sd_journal_stream_fd()
</function> is thread-safe and may be called
122 from multiple threads.
</para>
124 <para>The
<function>sd_journal_stream_fd()
</function> interface is
125 available as a shared library, which can be compiled and linked to
127 <constant>libsystemd
</constant> <citerefentry project='die-net'
><refentrytitle>pkg-config
</refentrytitle><manvolnum>1</manvolnum></citerefentry>
132 <title>Examples
</title>
134 <para>Creating a log stream suitable for
135 <citerefentry project='man-pages'
><refentrytitle>fprintf
</refentrytitle><manvolnum>3</manvolnum></citerefentry>:
</para>
137 <programlisting>#include
<syslog.h
>
138 #include
<stdio.h
>
139 #include
<string.h
>
140 #include
<unistd.h
>
141 #include
<systemd/sd-journal.h
>
142 #include
<systemd/sd-daemon.h
>
144 int main(int argc, char *argv[]) {
147 fd = sd_journal_stream_fd(
"test", LOG_INFO,
1);
149 fprintf(stderr,
"Failed to create stream fd: %s\n", strerror(-fd));
152 log = fdopen(fd,
"w");
154 fprintf(stderr,
"Failed to create file object: %m\n");
158 fprintf(log,
"Hello World!\n");
159 fprintf(log, SD_WARNING
"This is a warning!\n");
167 <title>See Also
</title>
170 <citerefentry><refentrytitle>systemd
</refentrytitle><manvolnum>1</manvolnum></citerefentry>,
171 <citerefentry><refentrytitle>sd-journal
</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
172 <citerefentry><refentrytitle>sd-daemon
</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
173 <citerefentry><refentrytitle>sd_journal_print
</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
174 <citerefentry project='man-pages'
><refentrytitle>syslog
</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
175 <citerefentry project='man-pages'
><refentrytitle>fprintf
</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
176 <citerefentry><refentrytitle>systemd.journal-fields
</refentrytitle><manvolnum>7</manvolnum></citerefentry>