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+
9 <refentry id=
"sd_journal_next" xmlns:
xi=
"http://www.w3.org/2001/XInclude">
12 <title>sd_journal_next
</title>
13 <productname>systemd
</productname>
17 <refentrytitle>sd_journal_next
</refentrytitle>
18 <manvolnum>3</manvolnum>
22 <refname>sd_journal_next
</refname>
23 <refname>sd_journal_previous
</refname>
24 <refname>sd_journal_next_skip
</refname>
25 <refname>sd_journal_previous_skip
</refname>
26 <refname>SD_JOURNAL_FOREACH
</refname>
27 <refname>SD_JOURNAL_FOREACH_BACKWARDS
</refname>
28 <refpurpose>Advance or set back the read pointer in the journal
</refpurpose>
33 <funcsynopsisinfo>#include
<systemd/sd-journal.h
></funcsynopsisinfo>
36 <funcdef>int
<function>sd_journal_next
</function></funcdef>
37 <paramdef>sd_journal *
<parameter>j
</parameter></paramdef>
41 <funcdef>int
<function>sd_journal_previous
</function></funcdef>
42 <paramdef>sd_journal *
<parameter>j
</parameter></paramdef>
46 <funcdef>int
<function>sd_journal_next_skip
</function></funcdef>
47 <paramdef>sd_journal *
<parameter>j
</parameter></paramdef>
48 <paramdef>uint64_t
<parameter>skip
</parameter></paramdef>
52 <funcdef>int
<function>sd_journal_previous_skip
</function></funcdef>
53 <paramdef>sd_journal *
<parameter>j
</parameter></paramdef>
54 <paramdef>uint64_t
<parameter>skip
</parameter></paramdef>
58 <funcdef><function>SD_JOURNAL_FOREACH
</function></funcdef>
59 <paramdef>sd_journal *
<parameter>j
</parameter></paramdef>
63 <funcdef><function>SD_JOURNAL_FOREACH_BACKWARDS
</function></funcdef>
64 <paramdef>sd_journal *
<parameter>j
</parameter></paramdef>
70 <title>Description
</title>
72 <para><function>sd_journal_next()
</function> advances the read
73 pointer into the journal by one entry. The only argument taken is
74 a journal context object as allocated via
75 <citerefentry><refentrytitle>sd_journal_open
</refentrytitle><manvolnum>3</manvolnum></citerefentry>.
76 After successful invocation the entry may be read with functions
78 <citerefentry><refentrytitle>sd_journal_get_data
</refentrytitle><manvolnum>3</manvolnum></citerefentry>.
</para>
80 <para>Similarly,
<function>sd_journal_previous()
</function> sets
81 the read pointer back one entry.
</para>
83 <para><function>sd_journal_next_skip()
</function> and
84 <function>sd_journal_previous_skip()
</function> advance/set back
85 the read pointer by multiple entries at once, as specified in the
86 <varname>skip
</varname> parameter.
</para>
88 <para>The journal is strictly ordered by reception time, and hence
89 advancing to the next entry guarantees that the entry then
90 pointing to is later in time than then previous one, or has the
91 same timestamp.
</para>
94 <citerefentry><refentrytitle>sd_journal_get_data
</refentrytitle><manvolnum>3</manvolnum></citerefentry>
95 and related calls will fail unless
96 <function>sd_journal_next()
</function> has been invoked at least
97 once in order to position the read pointer on a journal
100 <para>Note that the
<function>SD_JOURNAL_FOREACH()
</function>
101 macro may be used as a wrapper around
102 <citerefentry><refentrytitle>sd_journal_seek_head
</refentrytitle><manvolnum>3</manvolnum></citerefentry>
103 and
<function>sd_journal_next()
</function> in order to make
104 iterating through the journal easier. See below for an example.
105 Similarly,
<function>SD_JOURNAL_FOREACH_BACKWARDS()
</function> may
106 be used for iterating the journal in reverse order.
</para>
110 <title>Return Value
</title>
112 <para>The four calls return the number of entries advanced/set
113 back on success or a negative errno-style error code. When the end
114 or beginning of the journal is reached, a number smaller than
115 requested is returned. More specifically, if
116 <function>sd_journal_next()
</function> or
117 <function>sd_journal_previous()
</function> reach the end/beginning
118 of the journal they will return
0, instead of
1 when they are
119 successful. This should be considered an EOF marker.
</para>
125 <xi:include href=
"threads-aware.xml" xpointer=
"strict"/>
127 <xi:include href=
"libsystemd-pkgconfig.xml" xpointer=
"pkgconfig-text"/>
131 <title>Examples
</title>
133 <para>Iterating through the journal:
</para>
135 <programlisting>#include
<stdio.h
>
136 #include
<string.h
>
137 #include
<systemd/sd-journal.h
>
139 int main(int argc, char *argv[]) {
142 r = sd_journal_open(
&j, SD_JOURNAL_LOCAL_ONLY);
144 fprintf(stderr,
"Failed to open journal: %s\n", strerror(-r));
147 SD_JOURNAL_FOREACH(j) {
151 r = sd_journal_get_data(j,
"MESSAGE", (const void **)
&d,
&l);
153 fprintf(stderr,
"Failed to read message field: %s\n", strerror(-r));
157 printf(
"%.*s\n", (int) l, d);
166 <title>See Also
</title>
169 <citerefentry><refentrytitle>systemd
</refentrytitle><manvolnum>1</manvolnum></citerefentry>,
170 <citerefentry><refentrytitle>sd-journal
</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
171 <citerefentry><refentrytitle>sd_journal_open
</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
172 <citerefentry><refentrytitle>sd_journal_get_data
</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
173 <citerefentry><refentrytitle>sd_journal_get_realtime_usec
</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
174 <citerefentry><refentrytitle>sd_journal_get_cursor
</refentrytitle><manvolnum>3</manvolnum></citerefentry>