1SD_JOURNAL_NEXT(3)              sd_journal_next             SD_JOURNAL_NEXT(3)
2
3
4

NAME

6       sd_journal_next, sd_journal_previous, sd_journal_next_skip,
7       sd_journal_previous_skip, SD_JOURNAL_FOREACH,
8       SD_JOURNAL_FOREACH_BACKWARDS - Advance or set back the read pointer in
9       the journal
10

SYNOPSIS

12       #include <systemd/sd-journal.h>
13
14       int sd_journal_next(sd_journal *j);
15
16       int sd_journal_previous(sd_journal *j);
17
18       int sd_journal_next_skip(sd_journal *j, uint64_t skip);
19
20       int sd_journal_previous_skip(sd_journal *j, uint64_t skip);
21
22       SD_JOURNAL_FOREACH(sd_journal *j);
23
24       SD_JOURNAL_FOREACH_BACKWARDS(sd_journal *j);
25

DESCRIPTION

27       sd_journal_next() advances the read pointer into the journal by one
28       entry. The only argument taken is a journal context object as allocated
29       via sd_journal_open(3). After successful invocation the entry may be
30       read with functions such as sd_journal_get_data(3).
31
32       Similarly, sd_journal_previous() sets the read pointer back one entry.
33
34       sd_journal_next_skip() and sd_journal_previous_skip() advance/set back
35       the read pointer by multiple entries at once, as specified in the skip
36       parameter.
37
38       The journal is strictly ordered by reception time, and hence advancing
39       to the next entry guarantees that the entry then pointing to is later
40       in time than then previous one, or has the same timestamp.
41
42       Note that sd_journal_get_data(3) and related calls will fail unless
43       sd_journal_next() has been invoked at least once in order to position
44       the read pointer on a journal entry.
45
46       Note that the SD_JOURNAL_FOREACH() macro may be used as a wrapper
47       around sd_journal_seek_head(3) and sd_journal_next() in order to make
48       iterating through the journal easier. See below for an example.
49       Similarly, SD_JOURNAL_FOREACH_BACKWARDS() may be used for iterating the
50       journal in reverse order.
51

RETURN VALUE

53       The four calls return the number of entries advanced/set back on
54       success or a negative errno-style error code. When the end or beginning
55       of the journal is reached, a number smaller than requested is returned.
56       More specifically, if sd_journal_next() or sd_journal_previous() reach
57       the end/beginning of the journal they will return 0, instead of 1 when
58       they are successful. This should be considered an EOF marker.
59

NOTES

61       All functions listed here are thread-agnostic and only a single
62       specific thread may operate on a given object during its entire
63       lifetime. It's safe to allocate multiple independent objects and use
64       each from a specific thread in parallel. However, it's not safe to
65       allocate such an object in one thread, and operate or free it from any
66       other, even if locking is used to ensure these threads don't operate on
67       it at the very same time.
68
69       These APIs are implemented as a shared library, which can be compiled
70       and linked to with the libsystemd pkg-config(1) file.
71

EXAMPLES

73       Iterating through the journal:
74
75           #include <stdio.h>
76           #include <string.h>
77           #include <systemd/sd-journal.h>
78
79           int main(int argc, char *argv[]) {
80             int r;
81             sd_journal *j;
82             r = sd_journal_open(&j, SD_JOURNAL_LOCAL_ONLY);
83             if (r < 0) {
84               fprintf(stderr, "Failed to open journal: %s\n", strerror(-r));
85               return 1;
86             }
87             SD_JOURNAL_FOREACH(j) {
88               const char *d;
89               size_t l;
90
91               r = sd_journal_get_data(j, "MESSAGE", (const void **)&d, &l);
92               if (r < 0) {
93                 fprintf(stderr, "Failed to read message field: %s\n", strerror(-r));
94                 continue;
95               }
96
97               printf("%.*s\n", (int) l, d);
98             }
99             sd_journal_close(j);
100             return 0;
101           }
102

SEE ALSO

104       systemd(1), sd-journal(3), sd_journal_open(3), sd_journal_get_data(3),
105       sd_journal_get_realtime_usec(3), sd_journal_get_cursor(3)
106
107
108
109systemd 245                                                 SD_JOURNAL_NEXT(3)
Impressum