1
0
mirror of https://github.com/systemd/systemd.git synced 2024-12-23 21:35:11 +03:00
systemd/man/sd_journal_print.xml

291 lines
16 KiB
XML
Raw Normal View History

2012-07-09 17:46:21 +04:00
<?xml version='1.0'?> <!--*-nxml-*-->
<!DOCTYPE refentry PUBLIC "-//OASIS//DTD DocBook XML V4.5//EN"
"http://www.oasis-open.org/docbook/xml/4.5/docbookx.dtd">
<!-- SPDX-License-Identifier: LGPL-2.1-or-later -->
2012-07-09 17:46:21 +04:00
<refentry id="sd_journal_print" xmlns:xi="http://www.w3.org/2001/XInclude">
2012-07-09 17:46:21 +04:00
2015-02-04 05:14:13 +03:00
<refentryinfo>
<title>sd_journal_print</title>
<productname>systemd</productname>
</refentryinfo>
<refmeta>
<refentrytitle>sd_journal_print</refentrytitle>
<manvolnum>3</manvolnum>
</refmeta>
<refnamediv>
<refname>sd_journal_print</refname>
<refname>sd_journal_printv</refname>
<refname>sd_journal_send</refname>
<refname>sd_journal_sendv</refname>
<refname>sd_journal_perror</refname>
<refname>SD_JOURNAL_SUPPRESS_LOCATION</refname>
<refname>sd_journal_print_with_location</refname>
<refname>sd_journal_printv_with_location</refname>
<refname>sd_journal_send_with_location</refname>
<refname>sd_journal_sendv_with_location</refname>
<refname>sd_journal_perror_with_location</refname>
2015-02-04 05:14:13 +03:00
<refpurpose>Submit log entries to the journal</refpurpose>
</refnamediv>
<refsynopsisdiv>
<funcsynopsis>
<funcsynopsisinfo>#include &lt;systemd/sd-journal.h&gt;</funcsynopsisinfo>
<funcprototype>
<funcdef>int <function>sd_journal_print</function></funcdef>
<paramdef>int <parameter>priority</parameter></paramdef>
<paramdef>const char *<parameter>format</parameter></paramdef>
<paramdef></paramdef>
2015-02-04 05:14:13 +03:00
</funcprototype>
<funcprototype>
<funcdef>int <function>sd_journal_printv</function></funcdef>
<paramdef>int <parameter>priority</parameter></paramdef>
<paramdef>const char *<parameter>format</parameter></paramdef>
<paramdef>va_list <parameter>ap</parameter></paramdef>
</funcprototype>
<funcprototype>
<funcdef>int <function>sd_journal_send</function></funcdef>
<paramdef>const char *<parameter>format</parameter></paramdef>
<paramdef></paramdef>
2015-02-04 05:14:13 +03:00
</funcprototype>
<funcprototype>
<funcdef>int <function>sd_journal_sendv</function></funcdef>
<paramdef>const struct iovec *<parameter>iov</parameter></paramdef>
<paramdef>int <parameter>n</parameter></paramdef>
</funcprototype>
<funcprototype>
<funcdef>int <function>sd_journal_perror</function></funcdef>
<paramdef>const char *<parameter>message</parameter></paramdef>
</funcprototype>
<funcprototype>
<funcdef>int <function>sd_journal_print_with_location</function></funcdef>
<paramdef>int <parameter>priority</parameter></paramdef>
<paramdef>const char *<parameter>file</parameter></paramdef>
<paramdef>const char *<parameter>line</parameter></paramdef>
<paramdef>const char *<parameter>func</parameter></paramdef>
<paramdef>const char *<parameter>format</parameter></paramdef>
<paramdef></paramdef>
</funcprototype>
<funcprototype>
<funcdef>int <function>sd_journal_printv_with_location</function></funcdef>
<paramdef>int <parameter>priority</parameter></paramdef>
<paramdef>const char *<parameter>file</parameter></paramdef>
<paramdef>const char *<parameter>line</parameter></paramdef>
<paramdef>const char *<parameter>func</parameter></paramdef>
<paramdef>const char *<parameter>format</parameter></paramdef>
<paramdef>va_list <parameter>ap</parameter></paramdef>
</funcprototype>
<funcprototype>
<funcdef>int <function>sd_journal_send_with_location</function></funcdef>
<paramdef>const char *<parameter>file</parameter></paramdef>
<paramdef>const char *<parameter>line</parameter></paramdef>
<paramdef>const char *<parameter>func</parameter></paramdef>
<paramdef>const char *<parameter>format</parameter></paramdef>
<paramdef></paramdef>
</funcprototype>
<funcprototype>
<funcdef>int <function>sd_journal_sendv_with_location</function></funcdef>
<paramdef>const char *<parameter>file</parameter></paramdef>
<paramdef>const char *<parameter>line</parameter></paramdef>
<paramdef>const char *<parameter>func</parameter></paramdef>
<paramdef>const struct iovec *<parameter>iov</parameter></paramdef>
<paramdef>int <parameter>n</parameter></paramdef>
</funcprototype>
<funcprototype>
<funcdef>int <function>sd_journal_perror_with_location</function></funcdef>
<paramdef>const char *<parameter>file</parameter></paramdef>
<paramdef>const char *<parameter>line</parameter></paramdef>
<paramdef>const char *<parameter>func</parameter></paramdef>
<paramdef>const char *<parameter>message</parameter></paramdef>
</funcprototype>
2015-02-04 05:14:13 +03:00
</funcsynopsis>
</refsynopsisdiv>
<refsect1>
<title>Description</title>
<para><function>sd_journal_print()</function> may be used to submit simple, plain text log entries to the system
journal. The first argument is a priority value. This is followed by a format string and its parameters, similar to
<citerefentry project='man-pages'><refentrytitle>printf</refentrytitle><manvolnum>3</manvolnum></citerefentry> or
2015-02-04 05:14:13 +03:00
<citerefentry project='man-pages'><refentrytitle>syslog</refentrytitle><manvolnum>3</manvolnum></citerefentry>.
Note that currently the resulting message will be truncated to <constant>LINE_MAX - 8</constant>.
The priority value is one of <constant>LOG_EMERG</constant>, <constant>LOG_ALERT</constant>,
<constant>LOG_CRIT</constant>, <constant>LOG_ERR</constant>, <constant>LOG_WARNING</constant>,
<constant>LOG_NOTICE</constant>, <constant>LOG_INFO</constant>, <constant>LOG_DEBUG</constant>, as defined in
<filename>syslog.h</filename>, see <citerefentry
project='man-pages'><refentrytitle>syslog</refentrytitle><manvolnum>3</manvolnum></citerefentry> for details. It is
recommended to use this call to submit log messages in the application locale or system locale and in UTF-8 format,
but no such restrictions are enforced. Note that log messages written using this function are generally not
expected to end in a new-line character. However, as all trailing whitespace (including spaces, new-lines,
tabulators and carriage returns) are automatically stripped from the logged string, it is acceptable to specify one
(or more). Empty lines (after trailing whitespace removal) are suppressed. On non-empty lines, leading whitespace
(as well as inner whitespace) is left unmodified. </para>
2015-02-04 05:14:13 +03:00
<para><function>sd_journal_printv()</function> is similar to
<function>sd_journal_print()</function> but takes a variable
argument list encapsulated in an object of type
<varname>va_list</varname> (see
<citerefentry project='man-pages'><refentrytitle>stdarg</refentrytitle><manvolnum>3</manvolnum></citerefentry>
2015-02-04 05:14:13 +03:00
for more information) instead of the format string. It is
otherwise equivalent in behavior.</para>
<para><function>sd_journal_send()</function> may be used to submit structured log entries to the system journal. It
takes a series of format strings, each immediately followed by their associated parameters, terminated by
<constant>NULL</constant>. The strings passed should be of the format <literal>VARIABLE=value</literal>. The
variable name must be in uppercase and consist only of characters, numbers and underscores, and may not begin with
an underscore. (All assignments that do not follow this syntax will be ignored.) The value can be of any size and
format. It is highly recommended to submit text strings formatted in the UTF-8 character encoding only, and submit
binary fields only when formatting in UTF-8 strings is not sensible. A number of well-known fields are defined, see
<citerefentry><refentrytitle>systemd.journal-fields</refentrytitle><manvolnum>7</manvolnum></citerefentry> for
details, but additional application defined fields may be used. A variable may be assigned more than one value per
entry. If this function is used, trailing whitespace is automatically removed from each formatted field.</para>
<para><function>sd_journal_sendv()</function> is similar to <function>sd_journal_send()</function> but takes an
array of <varname>struct iovec</varname> (as defined in <filename>uio.h</filename>, see <citerefentry
project='man-pages'><refentrytitle>readv</refentrytitle><manvolnum>3</manvolnum></citerefentry> for details)
instead of the format string. Each structure should reference one field of the entry to submit. The second argument
specifies the number of structures in the array. <function>sd_journal_sendv()</function> is particularly useful to
2017-01-19 18:54:22 +03:00
submit binary objects to the journal where that is necessary. Note that this function will not strip trailing
whitespace of the passed fields, but passes the specified data along unmodified. This is different from both
<function>sd_journal_print()</function> and <function>sd_journal_send()</function> described above, which are based
on format strings, and do strip trailing whitespace.</para>
2015-02-04 05:14:13 +03:00
<para><function>sd_journal_perror()</function> is a similar to
<citerefentry project='die-net'><refentrytitle>perror</refentrytitle><manvolnum>3</manvolnum></citerefentry>
2015-02-04 05:14:13 +03:00
and writes a message to the journal that consists of the passed
string, suffixed with ": " and a human-readable representation of
2015-02-04 05:14:13 +03:00
the current error code stored in
<citerefentry project='man-pages'><refentrytitle>errno</refentrytitle><manvolnum>3</manvolnum></citerefentry>.
If the message string is passed as <constant>NULL</constant> or
empty string, only the error string representation will be
written, prefixed with nothing. An additional journal field ERRNO=
is included in the entry containing the numeric error code
formatted as decimal string. The log priority used is
<constant>LOG_ERR</constant> (3).</para>
<para>Note that <function>sd_journal_send()</function> is a
wrapper around <function>sd_journal_sendv()</function> to make it
easier to use when only text strings shall be submitted. Also, the
following two calls are mostly equivalent:</para>
<programlisting>sd_journal_print(LOG_INFO, "Hello World, this is PID %lu!", (unsigned long) getpid());
2012-07-09 17:46:21 +04:00
sd_journal_send("MESSAGE=Hello World, this is PID %lu!", (unsigned long) getpid(),
"PRIORITY=%i", LOG_INFO,
NULL);</programlisting>
2015-02-04 05:14:13 +03:00
<para>Note that these calls implicitly add fields for the source file, function name and code line where
invoked. This is implemented with macros. If this is not desired, it can be turned off by defining
<constant>SD_JOURNAL_SUPPRESS_LOCATION</constant> before including <filename>sd-journal.h</filename>.
</para>
<para><function>sd_journal_print_with_location()</function>,
<function>sd_journal_printv_with_location()</function>, <function>sd_journal_send_with_location()</function>,
<function>sd_journal_sendv_with_location()</function>, and
<function>sd_journal_perror_with_location()</function> are similar to their counterparts without
<literal>_with_location</literal>, but accept additional parameters to explicitly set the source file
man: fix sd_journal_*_with_location's func argument `sd_journal_print_with_location` and similar functions behave inconsistently compared to their documentation, which says: sd_journal_print_with_location(), sd_journal_printv_with_location(), sd_journal_send_with_location(), sd_journal_sendv_with_location(), and sd_journal_perror_with_location() [...] accept additional parameters to explicitly set the source file name, function, and line. Those arguments must contain valid journal entries including the variable name, e.g. "CODE_FILE=src/foo.c", "CODE_LINE=666", "CODE_FUNC=myfunc". Calling e.g. `sd_journal_sendv_with_location` with `CODE_FUNC=myfunction` as the value of the argument `func` results in "CODE_FUNC" : "CODE_FUNC=myfunction" because `sd_journal_*_with_location` implicitly prefix the argument `func` with `CODE_FUNC=`. For example: _public_ int sd_journal_sendv_with_location( const char *file, const char *line, const char *func, const struct iovec *iov, int n) { [...] char *f; [...] niov = newa(struct iovec, n + 3); [...] ALLOCA_CODE_FUNC(f, func); [...] niov[n++] = IOVEC_MAKE_STRING(f); return sd_journal_sendv(niov, n); } where `ALLOCA_CODE_FUNC` is: #define ALLOCA_CODE_FUNC(f, func) \ do { \ size_t _fl; \ const char *_func = (func); \ char **_f = &(f); \ _fl = strlen(_func) + 1; \ *_f = newa(char, _fl + 10); \ memcpy(*_f, "CODE_FUNC=", 10); \ memcpy(*_f + 10, _func, _fl); \ } while (false) The arguments `file` and `line` are _not_ prefixed similarly but expected to be prefixed already with `CODE_FILE=` and `CODE_LINE=` respectively and sent as is like the documentation describes. That is, the argument `func` is treated differently and behaves inconsistently compared to the arguments `file` and `line`. The behavior seems still intentional: _public_ int sd_journal_printv_with_location(int priority, const char *file, const char *line, const char *func, const char *format, va_list ap) { [...] /* func is initialized from __func__ which is not a macro, but * a static const char[], hence cannot easily be prefixed with * CODE_FUNC=, hence let's do it manually here. */ ALLOCA_CODE_FUNC(f, func); [...] } Thus, change the documentation to match the actual behavior. Note: `sd_journal_{print,send}` and `sd_journal_{print,send}v` work as expected as they only pass the function name (i.e. without `CODE_FUNC=`) to the `func` argument of the `sd_journal_*_with_location` functions they call. For example: #define sd_journal_print(priority, ...) sd_journal_print_with_location(priority, "CODE_FILE=" __FILE__, "CODE_LINE=" _SD_STRINGIFY(__LINE__), __func__, __VA_ARGS__)
2023-05-30 13:48:20 +03:00
name, function, and line. The arguments <literal>file</literal> and <literal>line</literal> must contain valid
journal entries including the variable name, e.g. <literal>CODE_FILE=src/foo.c</literal> and
<literal>CODE_LINE=666</literal>, while <literal>func</literal> must only contain the function name, i.e. the value
without <literal>CODE_FUNC=</literal>. These variants are primarily useful when writing custom wrappers, for
example in bindings for a different language.</para>
2015-02-04 05:14:13 +03:00
<para><citerefentry project='man-pages'><refentrytitle>syslog</refentrytitle><manvolnum>3</manvolnum></citerefentry>
and <function>sd_journal_print()</function> may
largely be used interchangeably
functionality-wise. However, note that log messages
logged via the former take a different path to the
journal server than the later, and hence global
chronological ordering between the two streams cannot
be guaranteed. Using
<function>sd_journal_print()</function> has the
benefit of logging source code line, filenames, and
functions as metadata along all entries, and
guaranteeing chronological ordering with structured
log entries that are generated via
<function>sd_journal_send()</function>. Using
<function>syslog()</function> has the benefit of being
more portable.</para>
2021-04-08 23:14:57 +03:00
<para>These functions implement a client to the <ulink
url="https://systemd.io/JOURNAL_NATIVE_PROTOCOL">Native Journal Protocol</ulink>.</para>
2015-02-04 05:14:13 +03:00
</refsect1>
<refsect1>
<title>Return Value</title>
<para>The ten functions return 0 on success or a negative errno-style error code. The
<citerefentry project='man-pages'><refentrytitle>errno</refentrytitle><manvolnum>3</manvolnum></citerefentry>
variable itself is not altered.</para>
2015-02-04 05:14:13 +03:00
<para>If
<citerefentry><refentrytitle>systemd-journald</refentrytitle><manvolnum>8</manvolnum></citerefentry>
is not running (the socket is not present), those functions do
nothing, and also return 0.</para>
</refsect1>
<refsect1>
<title>Thread safety</title>
<xi:include href="threads-aware.xml" xpointer="safe"/>
<para><function>sd_journal_sendv()</function> and <function>sd_journal_sendv_with_location()</function>
are "async signal safe" in the meaning of
<citerefentry project='man-pages'><refentrytitle>signal-safety</refentrytitle><manvolnum>7</manvolnum></citerefentry>.
2015-02-04 05:14:13 +03:00
</para>
<para><function>sd_journal_print()</function>,
<function>sd_journal_printv()</function>,
<function>sd_journal_send()</function>,
<function>sd_journal_perror()</function>,
and their counterparts with <literal>_with_location</literal>
are not async signal safe.</para>
2015-02-04 05:14:13 +03:00
</refsect1>
<xi:include href="libsystemd-pkgconfig.xml" />
2015-02-04 05:14:13 +03:00
<refsect1>
<title>History</title>
<para><function>sd_journal_print()</function>,
<function>sd_journal_printv()</function>,
<function>sd_journal_send()</function>, and
<function>sd_journal_sendv()</function> were added in version 187.</para>
<para><function>sd_journal_perror()</function> was added in version 188.</para>
<para><function>sd_journal_print_with_location()</function>,
<function>sd_journal_printv_with_location()</function>,
<function>sd_journal_send_with_location()</function>,
<function>sd_journal_sendv_with_location()</function>, and
<function>sd_journal_perror_with_location()</function> were added in version 246.</para>
</refsect1>
2015-02-04 05:14:13 +03:00
<refsect1>
<title>See Also</title>
<para><simplelist type="inline">
<member><citerefentry><refentrytitle>systemd</refentrytitle><manvolnum>1</manvolnum></citerefentry></member>
<member><citerefentry><refentrytitle>sd-journal</refentrytitle><manvolnum>3</manvolnum></citerefentry></member>
<member><citerefentry><refentrytitle>sd_journal_stream_fd</refentrytitle><manvolnum>3</manvolnum></citerefentry></member>
<member><citerefentry project='man-pages'><refentrytitle>syslog</refentrytitle><manvolnum>3</manvolnum></citerefentry></member>
<member><citerefentry project='die-net'><refentrytitle>perror</refentrytitle><manvolnum>3</manvolnum></citerefentry></member>
<member><citerefentry project='man-pages'><refentrytitle>errno</refentrytitle><manvolnum>3</manvolnum></citerefentry></member>
<member><citerefentry><refentrytitle>systemd.journal-fields</refentrytitle><manvolnum>7</manvolnum></citerefentry></member>
<member><citerefentry project='man-pages'><refentrytitle>signal</refentrytitle><manvolnum>7</manvolnum></citerefentry></member>
<member><citerefentry project='man-pages'><refentrytitle>socket</refentrytitle><manvolnum>7</manvolnum></citerefentry></member>
</simplelist></para>
2015-02-04 05:14:13 +03:00
</refsect1>
2012-07-09 17:46:21 +04:00
</refentry>