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 This file is part of systemd.
8 Copyright 2012 Lennart Poettering
10 systemd is free software; you can redistribute it and/or modify it
11 under the terms of the GNU Lesser General Public License as published by
12 the Free Software Foundation; either version 2.1 of the License, or
13 (at your option) any later version.
15 systemd is distributed in the hope that it will be useful, but
16 WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 Lesser General Public License for more details.
20 You should have received a copy of the GNU Lesser General Public License
21 along with systemd; If not, see <http://www.gnu.org/licenses/>.
24 <refentry id="sd_journal_next">
27 <title>sd_journal_next</title>
28 <productname>systemd</productname>
32 <contrib>Developer</contrib>
33 <firstname>Lennart</firstname>
34 <surname>Poettering</surname>
35 <email>lennart@poettering.net</email>
41 <refentrytitle>sd_journal_next</refentrytitle>
42 <manvolnum>3</manvolnum>
46 <refname>sd_journal_next</refname>
47 <refname>sd_journal_previous</refname>
48 <refname>sd_journal_next_skip</refname>
49 <refname>sd_journal_previous_skip</refname>
50 <refname>SD_JOURNAL_FOREACH</refname>
51 <refname>SD_JOURNAL_FOREACH_BACKWARDS</refname>
52 <refpurpose>Advance or set back the read pointer in the journal</refpurpose>
57 <funcsynopsisinfo>#include <systemd/sd-journal.h></funcsynopsisinfo>
60 <funcdef>int <function>sd_journal_next</function></funcdef>
61 <paramdef>sd_journal *<parameter>j</parameter></paramdef>
65 <funcdef>int <function>sd_journal_previous</function></funcdef>
66 <paramdef>sd_journal *<parameter>j</parameter></paramdef>
70 <funcdef>int <function>sd_journal_next_skip</function></funcdef>
71 <paramdef>sd_journal *<parameter>j</parameter></paramdef>
72 <paramdef>uint64_t <parameter>skip</parameter></paramdef>
76 <funcdef>int <function>sd_journal_previous_skip</function></funcdef>
77 <paramdef>sd_journal *<parameter>j</parameter></paramdef>
78 <paramdef>uint64_t <parameter>skip</parameter></paramdef>
82 <funcdef><function>SD_JOURNAL_FOREACH</function></funcdef>
83 <paramdef>sd_journal *<parameter>j</parameter></paramdef>
87 <funcdef><function>SD_JOURNAL_FOREACH_BACKWARDS</function></funcdef>
88 <paramdef>sd_journal *<parameter>j</parameter></paramdef>
94 <title>Description</title>
96 <para><function>sd_journal_next()</function> advances the read
97 pointer into the journal by one entry. The only argument taken is
98 a journal context object as allocated via
99 <citerefentry><refentrytitle>sd_journal_open</refentrytitle><manvolnum>3</manvolnum></citerefentry>.
100 After successful invocation the entry may be read with functions
102 <citerefentry><refentrytitle>sd_journal_get_data</refentrytitle><manvolnum>3</manvolnum></citerefentry>.</para>
104 <para>Similarly, <function>sd_journal_previous()</function> sets
105 the read pointer back one entry.</para>
107 <para><function>sd_journal_next_skip()</function> and
108 <function>sd_journal_previous_skip()</function> advance/set back
109 the read pointer by multiple entries at once, as specified in the
110 <varname>skip</varname> parameter.</para>
112 <para>The journal is strictly ordered by reception time, and hence
113 advancing to the next entry guarantees that the entry then
114 pointing to is later in time than then previous one, or has the
115 same timestamp.</para>
118 <citerefentry><refentrytitle>sd_journal_get_data</refentrytitle><manvolnum>3</manvolnum></citerefentry>
119 and related calls will fail unless
120 <function>sd_journal_next()</function> has been invoked at least
121 once in order to position the read pointer on a journal
124 <para>Note that the <function>SD_JOURNAL_FOREACH()</function>
125 macro may be used as a wrapper around
126 <citerefentry><refentrytitle>sd_journal_seek_head</refentrytitle><manvolnum>3</manvolnum></citerefentry>
127 and <function>sd_journal_next()</function> in order to make
128 iterating through the journal easier. See below for an example.
129 Similarly, <function>SD_JOURNAL_FOREACH_BACKWARDS()</function> may
130 be used for iterating the journal in reverse order.</para>
134 <title>Return Value</title>
136 <para>The four calls return the number of entries advanced/set
137 back on success or a negative errno-style error code. When the end
138 or beginning of the journal is reached, a number smaller than
139 requested is returned. More specifically, if
140 <function>sd_journal_next()</function> or
141 <function>sd_journal_previous()</function> reach the end/beginning
142 of the journal they will return 0, instead of 1 when they are
143 successful. This should be considered an EOF marker.</para>
149 <para>The <function>sd_journal_next()</function>,
150 <function>sd_journal_previous()</function>,
151 <function>sd_journal_next_skip()</function> and
152 <function>sd_journal_previous_skip()</function> interfaces are
153 available as a shared library, which can be compiled and linked to
155 <constant>libsystemd</constant> <citerefentry project='die-net'><refentrytitle>pkg-config</refentrytitle><manvolnum>1</manvolnum></citerefentry>
160 <title>Examples</title>
162 <para>Iterating through the journal:</para>
164 <programlisting>#include <stdio.h>
165 #include <string.h>
166 #include <systemd/sd-journal.h>
168 int main(int argc, char *argv[]) {
171 r = sd_journal_open(&j, SD_JOURNAL_LOCAL_ONLY);
173 fprintf(stderr, "Failed to open journal: %s\n", strerror(-r));
176 SD_JOURNAL_FOREACH(j) {
180 r = sd_journal_get_data(j, "MESSAGE", (const void **)&d, &l);
182 fprintf(stderr, "Failed to read message field: %s\n", strerror(-r));
186 printf("%.*s\n", (int) l, d);
195 <title>See Also</title>
198 <citerefentry><refentrytitle>systemd</refentrytitle><manvolnum>1</manvolnum></citerefentry>,
199 <citerefentry><refentrytitle>sd-journal</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
200 <citerefentry><refentrytitle>sd_journal_open</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
201 <citerefentry><refentrytitle>sd_journal_get_data</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
202 <citerefentry><refentrytitle>sd_journal_get_realtime_usec</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
203 <citerefentry><refentrytitle>sd_journal_get_cursor</refentrytitle><manvolnum>3</manvolnum></citerefentry>