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