1 <?xml version='1.0'?> <!--*-nxml-*-->
2 <!DOCTYPE refentry PUBLIC "-//OASIS//DTD DocBook XML V4.5//EN"
3 "http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd">
4 <!-- SPDX-License-Identifier: LGPL-2.1-or-later -->
6 <refentry id="sd_journal_print" xmlns:xi="http://www.w3.org/2001/XInclude">
9 <title>sd_journal_print</title>
10 <productname>systemd</productname>
14 <refentrytitle>sd_journal_print</refentrytitle>
15 <manvolnum>3</manvolnum>
19 <refname>sd_journal_print</refname>
20 <refname>sd_journal_printv</refname>
21 <refname>sd_journal_send</refname>
22 <refname>sd_journal_sendv</refname>
23 <refname>sd_journal_perror</refname>
24 <refname>SD_JOURNAL_SUPPRESS_LOCATION</refname>
25 <refname>sd_journal_print_with_location</refname>
26 <refname>sd_journal_printv_with_location</refname>
27 <refname>sd_journal_send_with_location</refname>
28 <refname>sd_journal_sendv_with_location</refname>
29 <refname>sd_journal_perror_with_location</refname>
31 <refpurpose>Submit log entries to the journal</refpurpose>
36 <funcsynopsisinfo>#include <systemd/sd-journal.h></funcsynopsisinfo>
39 <funcdef>int <function>sd_journal_print</function></funcdef>
40 <paramdef>int <parameter>priority</parameter></paramdef>
41 <paramdef>const char *<parameter>format</parameter></paramdef>
42 <paramdef>…</paramdef>
46 <funcdef>int <function>sd_journal_printv</function></funcdef>
47 <paramdef>int <parameter>priority</parameter></paramdef>
48 <paramdef>const char *<parameter>format</parameter></paramdef>
49 <paramdef>va_list <parameter>ap</parameter></paramdef>
53 <funcdef>int <function>sd_journal_send</function></funcdef>
54 <paramdef>const char *<parameter>format</parameter></paramdef>
55 <paramdef>…</paramdef>
59 <funcdef>int <function>sd_journal_sendv</function></funcdef>
60 <paramdef>const struct iovec *<parameter>iov</parameter></paramdef>
61 <paramdef>int <parameter>n</parameter></paramdef>
65 <funcdef>int <function>sd_journal_perror</function></funcdef>
66 <paramdef>const char *<parameter>message</parameter></paramdef>
70 <funcdef>int <function>sd_journal_print_with_location</function></funcdef>
71 <paramdef>int <parameter>priority</parameter></paramdef>
72 <paramdef>const char *<parameter>file</parameter></paramdef>
73 <paramdef>const char *<parameter>line</parameter></paramdef>
74 <paramdef>const char *<parameter>func</parameter></paramdef>
75 <paramdef>const char *<parameter>format</parameter></paramdef>
76 <paramdef>…</paramdef>
80 <funcdef>int <function>sd_journal_printv_with_location</function></funcdef>
81 <paramdef>int <parameter>priority</parameter></paramdef>
82 <paramdef>const char *<parameter>file</parameter></paramdef>
83 <paramdef>const char *<parameter>line</parameter></paramdef>
84 <paramdef>const char *<parameter>func</parameter></paramdef>
85 <paramdef>const char *<parameter>format</parameter></paramdef>
86 <paramdef>va_list <parameter>ap</parameter></paramdef>
90 <funcdef>int <function>sd_journal_send_with_location</function></funcdef>
91 <paramdef>const char *<parameter>file</parameter></paramdef>
92 <paramdef>const char *<parameter>line</parameter></paramdef>
93 <paramdef>const char *<parameter>func</parameter></paramdef>
94 <paramdef>const char *<parameter>format</parameter></paramdef>
95 <paramdef>…</paramdef>
99 <funcdef>int <function>sd_journal_sendv_with_location</function></funcdef>
100 <paramdef>const char *<parameter>file</parameter></paramdef>
101 <paramdef>const char *<parameter>line</parameter></paramdef>
102 <paramdef>const char *<parameter>func</parameter></paramdef>
103 <paramdef>const struct iovec *<parameter>iov</parameter></paramdef>
104 <paramdef>int <parameter>n</parameter></paramdef>
108 <funcdef>int <function>sd_journal_perror_with_location</function></funcdef>
109 <paramdef>const char *<parameter>file</parameter></paramdef>
110 <paramdef>const char *<parameter>line</parameter></paramdef>
111 <paramdef>const char *<parameter>func</parameter></paramdef>
112 <paramdef>const char *<parameter>message</parameter></paramdef>
118 <title>Description</title>
120 <para><function>sd_journal_print()</function> may be used to submit simple, plain text log entries to the system
121 journal. The first argument is a priority value. This is followed by a format string and its parameters, similar to
122 <citerefentry project='man-pages'><refentrytitle>printf</refentrytitle><manvolnum>3</manvolnum></citerefentry> or
123 <citerefentry project='man-pages'><refentrytitle>syslog</refentrytitle><manvolnum>3</manvolnum></citerefentry>.
124 Note that currently the resulting message will be truncated to <constant>LINE_MAX - 8</constant>.
125 The priority value is one of <constant>LOG_EMERG</constant>, <constant>LOG_ALERT</constant>,
126 <constant>LOG_CRIT</constant>, <constant>LOG_ERR</constant>, <constant>LOG_WARNING</constant>,
127 <constant>LOG_NOTICE</constant>, <constant>LOG_INFO</constant>, <constant>LOG_DEBUG</constant>, as defined in
128 <filename>syslog.h</filename>, see <citerefentry
129 project='man-pages'><refentrytitle>syslog</refentrytitle><manvolnum>3</manvolnum></citerefentry> for details. It is
130 recommended to use this call to submit log messages in the application locale or system locale and in UTF-8 format,
131 but no such restrictions are enforced. Note that log messages written using this function are generally not
132 expected to end in a new-line character. However, as all trailing whitespace (including spaces, new-lines,
133 tabulators and carriage returns) are automatically stripped from the logged string, it is acceptable to specify one
134 (or more). Empty lines (after trailing whitespace removal) are suppressed. On non-empty lines, leading whitespace
135 (as well as inner whitespace) is left unmodified. </para>
137 <para><function>sd_journal_printv()</function> is similar to
138 <function>sd_journal_print()</function> but takes a variable
139 argument list encapsulated in an object of type
140 <varname>va_list</varname> (see
141 <citerefentry project='man-pages'><refentrytitle>stdarg</refentrytitle><manvolnum>3</manvolnum></citerefentry>
142 for more information) instead of the format string. It is
143 otherwise equivalent in behavior.</para>
145 <para><function>sd_journal_send()</function> may be used to submit structured log entries to the system journal. It
146 takes a series of format strings, each immediately followed by their associated parameters, terminated by
147 <constant>NULL</constant>. The strings passed should be of the format <literal>VARIABLE=value</literal>. The
148 variable name must be in uppercase and consist only of characters, numbers and underscores, and may not begin with
149 an underscore. (All assignments that do not follow this syntax will be ignored.) The value can be of any size and
150 format. It is highly recommended to submit text strings formatted in the UTF-8 character encoding only, and submit
151 binary fields only when formatting in UTF-8 strings is not sensible. A number of well-known fields are defined, see
152 <citerefentry><refentrytitle>systemd.journal-fields</refentrytitle><manvolnum>7</manvolnum></citerefentry> for
153 details, but additional application defined fields may be used. A variable may be assigned more than one value per
154 entry. If this function is used, trailing whitespace is automatically removed from each formatted field.</para>
156 <para><function>sd_journal_sendv()</function> is similar to <function>sd_journal_send()</function> but takes an
157 array of <varname>struct iovec</varname> (as defined in <filename>uio.h</filename>, see <citerefentry
158 project='man-pages'><refentrytitle>readv</refentrytitle><manvolnum>3</manvolnum></citerefentry> for details)
159 instead of the format string. Each structure should reference one field of the entry to submit. The second argument
160 specifies the number of structures in the array. <function>sd_journal_sendv()</function> is particularly useful to
161 submit binary objects to the journal where that is necessary. Note that this function will not strip trailing
162 whitespace of the passed fields, but passes the specified data along unmodified. This is different from both
163 <function>sd_journal_print()</function> and <function>sd_journal_send()</function> described above, which are based
164 on format strings, and do strip trailing whitespace.</para>
166 <para><function>sd_journal_perror()</function> is a similar to
167 <citerefentry project='die-net'><refentrytitle>perror</refentrytitle><manvolnum>3</manvolnum></citerefentry>
168 and writes a message to the journal that consists of the passed
169 string, suffixed with ": " and a human-readable representation of
170 the current error code stored in
171 <citerefentry project='man-pages'><refentrytitle>errno</refentrytitle><manvolnum>3</manvolnum></citerefentry>.
172 If the message string is passed as <constant>NULL</constant> or
173 empty string, only the error string representation will be
174 written, prefixed with nothing. An additional journal field ERRNO=
175 is included in the entry containing the numeric error code
176 formatted as decimal string. The log priority used is
177 <constant>LOG_ERR</constant> (3).</para>
179 <para>Note that <function>sd_journal_send()</function> is a
180 wrapper around <function>sd_journal_sendv()</function> to make it
181 easier to use when only text strings shall be submitted. Also, the
182 following two calls are mostly equivalent:</para>
184 <programlisting>sd_journal_print(LOG_INFO, "Hello World, this is PID %lu!", (unsigned long) getpid());
186 sd_journal_send("MESSAGE=Hello World, this is PID %lu!", (unsigned long) getpid(),
187 "PRIORITY=%i", LOG_INFO,
188 NULL);</programlisting>
190 <para>Note that these calls implicitly add fields for the source file, function name and code line where
191 invoked. This is implemented with macros. If this is not desired, it can be turned off by defining
192 <constant>SD_JOURNAL_SUPPRESS_LOCATION</constant> before including <filename>sd-journal.h</filename>.
195 <para><function>sd_journal_print_with_location()</function>,
196 <function>sd_journal_printv_with_location()</function>, <function>sd_journal_send_with_location()</function>,
197 <function>sd_journal_sendv_with_location()</function>, and
198 <function>sd_journal_perror_with_location()</function> are similar to their counterparts without
199 <literal>_with_location</literal>, but accept additional parameters to explicitly set the source file
200 name, function, and line. The arguments <literal>file</literal> and <literal>line</literal> must contain valid
201 journal entries including the variable name, e.g. <literal>CODE_FILE=src/foo.c</literal> and
202 <literal>CODE_LINE=666</literal>, while <literal>func</literal> must only contain the function name, i.e. the value
203 without <literal>CODE_FUNC=</literal>. These variants are primarily useful when writing custom wrappers, for
204 example in bindings for a different language.</para>
206 <para><citerefentry project='man-pages'><refentrytitle>syslog</refentrytitle><manvolnum>3</manvolnum></citerefentry>
207 and <function>sd_journal_print()</function> may
208 largely be used interchangeably
209 functionality-wise. However, note that log messages
210 logged via the former take a different path to the
211 journal server than the later, and hence global
212 chronological ordering between the two streams cannot
214 <function>sd_journal_print()</function> has the
215 benefit of logging source code line, filenames, and
216 functions as metadata along all entries, and
217 guaranteeing chronological ordering with structured
218 log entries that are generated via
219 <function>sd_journal_send()</function>. Using
220 <function>syslog()</function> has the benefit of being
221 more portable.</para>
223 <para>These functions implement a client to the <ulink
224 url="https://systemd.io/JOURNAL_NATIVE_PROTOCOL">Native Journal Protocol</ulink>.</para>
228 <title>Return Value</title>
230 <para>The ten functions return 0 on success or a negative errno-style error code. The
231 <citerefentry project='man-pages'><refentrytitle>errno</refentrytitle><manvolnum>3</manvolnum></citerefentry>
232 variable itself is not altered.</para>
235 <citerefentry><refentrytitle>systemd-journald</refentrytitle><manvolnum>8</manvolnum></citerefentry>
236 is not running (the socket is not present), those functions do
237 nothing, and also return 0.</para>
241 <title>Thread safety</title>
243 <xi:include href="threads-aware.xml" xpointer="safe"/>
245 <para><function>sd_journal_sendv()</function> and <function>sd_journal_sendv_with_location()</function>
246 are "async signal safe" in the meaning of
247 <citerefentry project='man-pages'><refentrytitle>signal-safety</refentrytitle><manvolnum>7</manvolnum></citerefentry>.
250 <para><function>sd_journal_print()</function>,
251 <function>sd_journal_printv()</function>,
252 <function>sd_journal_send()</function>,
253 <function>sd_journal_perror()</function>,
254 and their counterparts with <literal>_with_location</literal>
255 are not async signal safe.</para>
258 <xi:include href="libsystemd-pkgconfig.xml" />
261 <title>History</title>
262 <para><function>sd_journal_print()</function>,
263 <function>sd_journal_printv()</function>,
264 <function>sd_journal_send()</function>, and
265 <function>sd_journal_sendv()</function> were added in version 187.</para>
266 <para><function>sd_journal_perror()</function> was added in version 188.</para>
267 <para><function>sd_journal_print_with_location()</function>,
268 <function>sd_journal_printv_with_location()</function>,
269 <function>sd_journal_send_with_location()</function>,
270 <function>sd_journal_sendv_with_location()</function>, and
271 <function>sd_journal_perror_with_location()</function> were added in version 246.</para>
275 <title>See Also</title>
278 <citerefentry><refentrytitle>systemd</refentrytitle><manvolnum>1</manvolnum></citerefentry>,
279 <citerefentry><refentrytitle>sd-journal</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
280 <citerefentry><refentrytitle>sd_journal_stream_fd</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
281 <citerefentry project='man-pages'><refentrytitle>syslog</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
282 <citerefentry project='die-net'><refentrytitle>perror</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
283 <citerefentry project='man-pages'><refentrytitle>errno</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
284 <citerefentry><refentrytitle>systemd.journal-fields</refentrytitle><manvolnum>7</manvolnum></citerefentry>,
285 <citerefentry project='man-pages'><refentrytitle>signal</refentrytitle><manvolnum>7</manvolnum></citerefentry>,
286 <citerefentry project='man-pages'><refentrytitle>socket</refentrytitle><manvolnum>7</manvolnum></citerefentry>