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