1SD_JOURNAL_NEXT(3) sd_journal_next SD_JOURNAL_NEXT(3)
2
3
4
6 sd_journal_next, sd_journal_previous, sd_journal_step_one,
7 sd_journal_next_skip, 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_step_one(sd_journal *j, int advanced);
19
20 int sd_journal_next_skip(sd_journal *j, uint64_t skip);
21
22 int sd_journal_previous_skip(sd_journal *j, uint64_t skip);
23
24 SD_JOURNAL_FOREACH(sd_journal *j);
25
26 SD_JOURNAL_FOREACH_BACKWARDS(sd_journal *j);
27
29 sd_journal_next() advances the read pointer into the journal by one
30 entry. The only argument taken is a journal context object as allocated
31 via sd_journal_open(3). After successful invocation the entry may be
32 read with functions such as sd_journal_get_data(3).
33
34 Similarly, sd_journal_previous() sets the read pointer back one entry.
35
36 sd_journal_step_one() also moves the read pointer. If the current
37 location is the head of the journal, e.g. when this is called following
38 sd_journal_seek_head(), then this is equivalent to sd_journal_next(),
39 and the argument advanced will be ignored. Similarly, if the current
40 location is the tail of the journal, e.g. when this is called following
41 sd_journal_seek_tail(), then this is equivalent to
42 sd_journal_previous(), and advanced will be ignored. Otherwise, this is
43 equivalent to sd_journal_next() when advanced is non-zero, and
44 sd_journal_previous() when advanced is zero.
45
46 sd_journal_next_skip() and sd_journal_previous_skip() advance/set back
47 the read pointer by multiple entries at once, as specified in the skip
48 parameter. The skip parameter must be less than or equal to 2147483647
49 (2³¹-1).
50
51 The journal is strictly ordered by reception time, and hence advancing
52 to the next entry guarantees that the entry then pointing to is later
53 in time than then previous one, or has the same timestamp.
54
55 Note that sd_journal_get_data(3) and related calls will fail unless
56 sd_journal_next() has been invoked at least once in order to position
57 the read pointer on a journal entry.
58
59 Note that the SD_JOURNAL_FOREACH() macro may be used as a wrapper
60 around sd_journal_seek_head(3) and sd_journal_next() in order to make
61 iterating through the journal easier. See below for an example.
62 Similarly, SD_JOURNAL_FOREACH_BACKWARDS() may be used for iterating the
63 journal in reverse order.
64
66 The four calls return the number of entries advanced/set back on
67 success or a negative errno-style error code. When the end or beginning
68 of the journal is reached, a number smaller than requested is returned.
69 More specifically, if sd_journal_next() or sd_journal_previous() reach
70 the end/beginning of the journal they will return 0, instead of 1 when
71 they are successful. This should be considered an EOF marker.
72
74 All functions listed here are thread-agnostic and only a single
75 specific thread may operate on a given object during its entire
76 lifetime. It's safe to allocate multiple independent objects and use
77 each from a specific thread in parallel. However, it's not safe to
78 allocate such an object in one thread, and operate or free it from any
79 other, even if locking is used to ensure these threads don't operate on
80 it at the very same time.
81
82 Functions described here are available as a shared library, which can
83 be compiled against and linked to with the libsystemd pkg-config(1)
84 file.
85
87 Iterating through the journal:
88
89 /* SPDX-License-Identifier: MIT-0 */
90
91 #include <errno.h>
92 #include <stdio.h>
93 #include <systemd/sd-journal.h>
94
95 int main(int argc, char *argv[]) {
96 int r;
97 sd_journal *j;
98 r = sd_journal_open(&j, SD_JOURNAL_LOCAL_ONLY);
99 if (r < 0) {
100 errno = -r;
101 fprintf(stderr, "Failed to open journal: %m\n");
102 return 1;
103 }
104 SD_JOURNAL_FOREACH(j) {
105 const char *d;
106 size_t l;
107
108 r = sd_journal_get_data(j, "MESSAGE", (const void **)&d, &l);
109 if (r < 0) {
110 errno = -r;
111 fprintf(stderr, "Failed to read message field: %m\n");
112 continue;
113 }
114
115 printf("%.*s\n", (int) l, d);
116 }
117 sd_journal_close(j);
118 return 0;
119 }
120
122 systemd(1), sd-journal(3), sd_journal_open(3), sd_journal_get_data(3),
123 sd_journal_get_realtime_usec(3), sd_journal_get_cursor(3)
124
125
126
127systemd 254 SD_JOURNAL_NEXT(3)