1SD_JOURNAL_NEXT(3) sd_journal_next SD_JOURNAL_NEXT(3)
2
3
4
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
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
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³¹-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
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
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
74 Iterating through the journal:
75
76 /* SPDX-License-Identifier: MIT-0 */
77
78 #include <errno.h>
79 #include <stdio.h>
80 #include <systemd/sd-journal.h>
81
82 int main(int argc, char *argv[]) {
83 int r;
84 sd_journal *j;
85 r = sd_journal_open(&j, SD_JOURNAL_LOCAL_ONLY);
86 if (r < 0) {
87 errno = -r;
88 fprintf(stderr, "Failed to open journal: %m\n");
89 return 1;
90 }
91 SD_JOURNAL_FOREACH(j) {
92 const char *d;
93 size_t l;
94
95 r = sd_journal_get_data(j, "MESSAGE", (const void **)&d, &l);
96 if (r < 0) {
97 errno = -r;
98 fprintf(stderr, "Failed to read message field: %m\n");
99 continue;
100 }
101
102 printf("%.*s\n", (int) l, d);
103 }
104 sd_journal_close(j);
105 return 0;
106 }
107
109 systemd(1), sd-journal(3), sd_journal_open(3), sd_journal_get_data(3),
110 sd_journal_get_realtime_usec(3), sd_journal_get_cursor(3)
111
112
113
114systemd 253 SD_JOURNAL_NEXT(3)