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. The skip parameter must be less than or equal to 2147483647
37       (2^31-1).
38
39       The journal is strictly ordered by reception time, and hence advancing
40       to the next entry guarantees that the entry then pointing to is later
41       in time than then previous one, or has the same timestamp.
42
43       Note that sd_journal_get_data(3) and related calls will fail unless
44       sd_journal_next() has been invoked at least once in order to position
45       the read pointer on a journal entry.
46
47       Note that the SD_JOURNAL_FOREACH() macro may be used as a wrapper
48       around sd_journal_seek_head(3) and sd_journal_next() in order to make
49       iterating through the journal easier. See below for an example.
50       Similarly, SD_JOURNAL_FOREACH_BACKWARDS() may be used for iterating the
51       journal in reverse order.
52

RETURN VALUE

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

NOTES

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

EXAMPLES

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

SEE ALSO

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