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+ -->
6 <refentry id=
"sd_journal_next" xmlns:
xi=
"http://www.w3.org/2001/XInclude">
9 <title>sd_journal_next
</title>
10 <productname>systemd
</productname>
14 <refentrytitle>sd_journal_next
</refentrytitle>
15 <manvolnum>3</manvolnum>
19 <refname>sd_journal_next
</refname>
20 <refname>sd_journal_previous
</refname>
21 <refname>sd_journal_next_skip
</refname>
22 <refname>sd_journal_previous_skip
</refname>
23 <refname>SD_JOURNAL_FOREACH
</refname>
24 <refname>SD_JOURNAL_FOREACH_BACKWARDS
</refname>
25 <refpurpose>Advance or set back the read pointer in the journal
</refpurpose>
30 <funcsynopsisinfo>#include
<systemd/sd-journal.h
></funcsynopsisinfo>
33 <funcdef>int
<function>sd_journal_next
</function></funcdef>
34 <paramdef>sd_journal *
<parameter>j
</parameter></paramdef>
38 <funcdef>int
<function>sd_journal_previous
</function></funcdef>
39 <paramdef>sd_journal *
<parameter>j
</parameter></paramdef>
43 <funcdef>int
<function>sd_journal_next_skip
</function></funcdef>
44 <paramdef>sd_journal *
<parameter>j
</parameter></paramdef>
45 <paramdef>uint64_t
<parameter>skip
</parameter></paramdef>
49 <funcdef>int
<function>sd_journal_previous_skip
</function></funcdef>
50 <paramdef>sd_journal *
<parameter>j
</parameter></paramdef>
51 <paramdef>uint64_t
<parameter>skip
</parameter></paramdef>
55 <funcdef><function>SD_JOURNAL_FOREACH
</function></funcdef>
56 <paramdef>sd_journal *
<parameter>j
</parameter></paramdef>
60 <funcdef><function>SD_JOURNAL_FOREACH_BACKWARDS
</function></funcdef>
61 <paramdef>sd_journal *
<parameter>j
</parameter></paramdef>
67 <title>Description
</title>
69 <para><function>sd_journal_next()
</function> advances the read
70 pointer into the journal by one entry. The only argument taken is
71 a journal context object as allocated via
72 <citerefentry><refentrytitle>sd_journal_open
</refentrytitle><manvolnum>3</manvolnum></citerefentry>.
73 After successful invocation the entry may be read with functions
75 <citerefentry><refentrytitle>sd_journal_get_data
</refentrytitle><manvolnum>3</manvolnum></citerefentry>.
</para>
77 <para>Similarly,
<function>sd_journal_previous()
</function> sets
78 the read pointer back one entry.
</para>
80 <para><function>sd_journal_next_skip()
</function> and
81 <function>sd_journal_previous_skip()
</function> advance/set back
82 the read pointer by multiple entries at once, as specified in the
83 <varname>skip
</varname> parameter.
</para>
85 <para>The journal is strictly ordered by reception time, and hence
86 advancing to the next entry guarantees that the entry then
87 pointing to is later in time than then previous one, or has the
88 same timestamp.
</para>
91 <citerefentry><refentrytitle>sd_journal_get_data
</refentrytitle><manvolnum>3</manvolnum></citerefentry>
92 and related calls will fail unless
93 <function>sd_journal_next()
</function> has been invoked at least
94 once in order to position the read pointer on a journal
97 <para>Note that the
<function>SD_JOURNAL_FOREACH()
</function>
98 macro may be used as a wrapper around
99 <citerefentry><refentrytitle>sd_journal_seek_head
</refentrytitle><manvolnum>3</manvolnum></citerefentry>
100 and
<function>sd_journal_next()
</function> in order to make
101 iterating through the journal easier. See below for an example.
102 Similarly,
<function>SD_JOURNAL_FOREACH_BACKWARDS()
</function> may
103 be used for iterating the journal in reverse order.
</para>
107 <title>Return Value
</title>
109 <para>The four calls return the number of entries advanced/set
110 back on success or a negative errno-style error code. When the end
111 or beginning of the journal is reached, a number smaller than
112 requested is returned. More specifically, if
113 <function>sd_journal_next()
</function> or
114 <function>sd_journal_previous()
</function> reach the end/beginning
115 of the journal they will return
0, instead of
1 when they are
116 successful. This should be considered an EOF marker.
</para>
122 <xi:include href=
"threads-aware.xml" xpointer=
"strict"/>
124 <xi:include href=
"libsystemd-pkgconfig.xml" xpointer=
"pkgconfig-text"/>
128 <title>Examples
</title>
130 <para>Iterating through the journal:
</para>
132 <programlisting>#include
<stdio.h
>
133 #include
<string.h
>
134 #include
<systemd/sd-journal.h
>
136 int main(int argc, char *argv[]) {
139 r = sd_journal_open(
&j, SD_JOURNAL_LOCAL_ONLY);
141 fprintf(stderr,
"Failed to open journal: %s\n", strerror(-r));
144 SD_JOURNAL_FOREACH(j) {
148 r = sd_journal_get_data(j,
"MESSAGE", (const void **)
&d,
&l);
150 fprintf(stderr,
"Failed to read message field: %s\n", strerror(-r));
154 printf(
"%.*s\n", (int) l, d);
163 <title>See Also
</title>
166 <citerefentry><refentrytitle>systemd
</refentrytitle><manvolnum>1</manvolnum></citerefentry>,
167 <citerefentry><refentrytitle>sd-journal
</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
168 <citerefentry><refentrytitle>sd_journal_open
</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
169 <citerefentry><refentrytitle>sd_journal_get_data
</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
170 <citerefentry><refentrytitle>sd_journal_get_realtime_usec
</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
171 <citerefentry><refentrytitle>sd_journal_get_cursor
</refentrytitle><manvolnum>3</manvolnum></citerefentry>