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 Copyright 2012 Lennart Poettering
11 <refentry id=
"sd_journal_print" xmlns:
xi=
"http://www.w3.org/2001/XInclude">
14 <title>sd_journal_print
</title>
15 <productname>systemd
</productname>
19 <contrib>Developer
</contrib>
20 <firstname>Lennart
</firstname>
21 <surname>Poettering
</surname>
22 <email>lennart@poettering.net
</email>
28 <refentrytitle>sd_journal_print
</refentrytitle>
29 <manvolnum>3</manvolnum>
33 <refname>sd_journal_print
</refname>
34 <refname>sd_journal_printv
</refname>
35 <refname>sd_journal_send
</refname>
36 <refname>sd_journal_sendv
</refname>
37 <refname>sd_journal_perror
</refname>
38 <refname>SD_JOURNAL_SUPPRESS_LOCATION
</refname>
39 <refpurpose>Submit log entries to the journal
</refpurpose>
44 <funcsynopsisinfo>#include
<systemd/sd-journal.h
></funcsynopsisinfo>
47 <funcdef>int
<function>sd_journal_print
</function></funcdef>
48 <paramdef>int
<parameter>priority
</parameter></paramdef>
49 <paramdef>const char *
<parameter>format
</parameter></paramdef>
50 <paramdef>…
</paramdef>
54 <funcdef>int
<function>sd_journal_printv
</function></funcdef>
55 <paramdef>int
<parameter>priority
</parameter></paramdef>
56 <paramdef>const char *
<parameter>format
</parameter></paramdef>
57 <paramdef>va_list
<parameter>ap
</parameter></paramdef>
61 <funcdef>int
<function>sd_journal_send
</function></funcdef>
62 <paramdef>const char *
<parameter>format
</parameter></paramdef>
63 <paramdef>…
</paramdef>
67 <funcdef>int
<function>sd_journal_sendv
</function></funcdef>
68 <paramdef>const struct iovec *
<parameter>iov
</parameter></paramdef>
69 <paramdef>int
<parameter>n
</parameter></paramdef>
73 <funcdef>int
<function>sd_journal_perror
</function></funcdef>
74 <paramdef>const char *
<parameter>message
</parameter></paramdef>
81 <title>Description
</title>
83 <para><function>sd_journal_print()
</function> may be used to submit simple, plain text log entries to the system
84 journal. The first argument is a priority value. This is followed by a format string and its parameters, similar to
85 <citerefentry project='man-pages'
><refentrytitle>printf
</refentrytitle><manvolnum>3</manvolnum></citerefentry> or
86 <citerefentry project='man-pages'
><refentrytitle>syslog
</refentrytitle><manvolnum>3</manvolnum></citerefentry>.
87 The priority value is one of
<constant>LOG_EMERG
</constant>,
<constant>LOG_ALERT
</constant>,
88 <constant>LOG_CRIT
</constant>,
<constant>LOG_ERR
</constant>,
<constant>LOG_WARNING
</constant>,
89 <constant>LOG_NOTICE
</constant>,
<constant>LOG_INFO
</constant>,
<constant>LOG_DEBUG
</constant>, as defined in
90 <filename>syslog.h
</filename>, see
<citerefentry
91 project='man-pages'
><refentrytitle>syslog
</refentrytitle><manvolnum>3</manvolnum></citerefentry> for details. It is
92 recommended to use this call to submit log messages in the application locale or system locale and in UTF-
8 format,
93 but no such restrictions are enforced. Note that log messages written using this function are generally not
94 expected to end in a new-line character. However, as all trailing whitespace (including spaces, new-lines,
95 tabulators and carriage returns) are automatically stripped from the logged string, it is acceptable to specify one
96 (or more). Empty lines (after trailing whitespace removal) are suppressed. On non-empty lines, leading whitespace
97 (as well as inner whitespace) is left unmodified.
</para>
99 <para><function>sd_journal_printv()
</function> is similar to
100 <function>sd_journal_print()
</function> but takes a variable
101 argument list encapsulated in an object of type
102 <varname>va_list
</varname> (see
103 <citerefentry project='man-pages'
><refentrytitle>stdarg
</refentrytitle><manvolnum>3</manvolnum></citerefentry>
104 for more information) instead of the format string. It is
105 otherwise equivalent in behavior.
</para>
107 <para><function>sd_journal_send()
</function> may be used to submit structured log entries to the system journal. It
108 takes a series of format strings, each immediately followed by their associated parameters, terminated by
109 <constant>NULL
</constant>. The strings passed should be of the format
<literal>VARIABLE=value
</literal>. The
110 variable name must be in uppercase and consist only of characters, numbers and underscores, and may not begin with
111 an underscore. (All assignments that do not follow this syntax will be ignored.) The value can be of any size and
112 format. It is highly recommended to submit text strings formatted in the UTF-
8 character encoding only, and submit
113 binary fields only when formatting in UTF-
8 strings is not sensible. A number of well-known fields are defined, see
114 <citerefentry><refentrytitle>systemd.journal-fields
</refentrytitle><manvolnum>7</manvolnum></citerefentry> for
115 details, but additional application defined fields may be used. A variable may be assigned more than one value per
116 entry. If this function is used, trailing whitespace is automatically removed from each formatted field.
</para>
118 <para><function>sd_journal_sendv()
</function> is similar to
<function>sd_journal_send()
</function> but takes an
119 array of
<varname>struct iovec
</varname> (as defined in
<filename>uio.h
</filename>, see
<citerefentry
120 project='man-pages'
><refentrytitle>readv
</refentrytitle><manvolnum>3</manvolnum></citerefentry> for details)
121 instead of the format string. Each structure should reference one field of the entry to submit. The second argument
122 specifies the number of structures in the array.
<function>sd_journal_sendv()
</function> is particularly useful to
123 submit binary objects to the journal where that is necessary. Note that this function will not strip trailing
124 whitespace of the passed fields, but passes the specified data along unmodified. This is different from both
125 <function>sd_journal_print()
</function> and
<function>sd_journal_send()
</function> described above, which are based
126 on format strings, and do strip trailing whitespace.
</para>
128 <para><function>sd_journal_perror()
</function> is a similar to
129 <citerefentry project='die-net'
><refentrytitle>perror
</refentrytitle><manvolnum>3</manvolnum></citerefentry>
130 and writes a message to the journal that consists of the passed
131 string, suffixed with
": " and a human-readable representation of
132 the current error code stored in
133 <citerefentry project='man-pages'
><refentrytitle>errno
</refentrytitle><manvolnum>3</manvolnum></citerefentry>.
134 If the message string is passed as
<constant>NULL
</constant> or
135 empty string, only the error string representation will be
136 written, prefixed with nothing. An additional journal field ERRNO=
137 is included in the entry containing the numeric error code
138 formatted as decimal string. The log priority used is
139 <constant>LOG_ERR
</constant> (
3).
</para>
141 <para>Note that
<function>sd_journal_send()
</function> is a
142 wrapper around
<function>sd_journal_sendv()
</function> to make it
143 easier to use when only text strings shall be submitted. Also, the
144 following two calls are mostly equivalent:
</para>
146 <programlisting>sd_journal_print(LOG_INFO,
"Hello World, this is PID %lu!", (unsigned long) getpid());
148 sd_journal_send(
"MESSAGE=Hello World, this is PID %lu!", (unsigned long) getpid(),
149 "PRIORITY=%i", LOG_INFO,
150 NULL);
</programlisting>
152 <para>Note that these calls implicitly add fields for the source
153 file, function name and code line where invoked. This is
154 implemented with macros. If this is not desired, it can be turned
155 off by defining SD_JOURNAL_SUPPRESS_LOCATION before including
156 <filename>sd-journal.h
</filename>.
</para>
158 <para><citerefentry project='man-pages'
><refentrytitle>syslog
</refentrytitle><manvolnum>3</manvolnum></citerefentry>
159 and
<function>sd_journal_print()
</function> may
160 largely be used interchangeably
161 functionality-wise. However, note that log messages
162 logged via the former take a different path to the
163 journal server than the later, and hence global
164 chronological ordering between the two streams cannot
166 <function>sd_journal_print()
</function> has the
167 benefit of logging source code line, filenames, and
168 functions as metadata along all entries, and
169 guaranteeing chronological ordering with structured
170 log entries that are generated via
171 <function>sd_journal_send()
</function>. Using
172 <function>syslog()
</function> has the benefit of being
173 more portable.
</para>
177 <title>Return Value
</title>
179 <para>The five calls return
0 on success or a negative errno-style error code. The
<citerefentry
180 project='man-pages'
><refentrytitle>errno
</refentrytitle><manvolnum>3</manvolnum></citerefentry> variable itself is
184 <citerefentry><refentrytitle>systemd-journald
</refentrytitle><manvolnum>8</manvolnum></citerefentry>
185 is not running (the socket is not present), those functions do
186 nothing, and also return
0.
</para>
190 <title>Thread safety
</title>
191 <para>All functions listed here are thread-safe and may be called in parallel from multiple threads.
</para>
193 <para><function>sd_journal_sendv()
</function> is
"async signal safe" in the meaning of
<citerefentry
194 project='man-pages'
><refentrytitle>signal-safety
</refentrytitle><manvolnum>7</manvolnum></citerefentry>.
197 <para><function>sd_journal_print
</function>,
198 <function>sd_journal_printv
</function>,
199 <function>sd_journal_send
</function>, and
200 <function>sd_journal_perror
</function> are
201 not async signal safe.
</para>
204 <xi:include href=
"libsystemd-pkgconfig.xml" />
207 <title>See Also
</title>
210 <citerefentry><refentrytitle>systemd
</refentrytitle><manvolnum>1</manvolnum></citerefentry>,
211 <citerefentry><refentrytitle>sd-journal
</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
212 <citerefentry><refentrytitle>sd_journal_stream_fd
</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
213 <citerefentry project='man-pages'
><refentrytitle>syslog
</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
214 <citerefentry project='die-net'
><refentrytitle>perror
</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
215 <citerefentry project='man-pages'
><refentrytitle>errno
</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
216 <citerefentry><refentrytitle>systemd.journal-fields
</refentrytitle><manvolnum>7</manvolnum></citerefentry>,
217 <citerefentry project='man-pages'
><refentrytitle>signal
</refentrytitle><manvolnum>7</manvolnum></citerefentry>,
218 <citerefentry project='man-pages'
><refentrytitle>socket
</refentrytitle><manvolnum>7</manvolnum></citerefentry>