]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/journal/journalctl.c
journalctl: add new short-monotonic output mode
[thirdparty/systemd.git] / src / journal / journalctl.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4 This file is part of systemd.
5
6 Copyright 2011 Lennart Poettering
7
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 ***/
21
22 #include <fcntl.h>
23 #include <errno.h>
24 #include <stddef.h>
25 #include <string.h>
26 #include <stdio.h>
27 #include <unistd.h>
28 #include <stdlib.h>
29 #include <sys/poll.h>
30 #include <time.h>
31 #include <getopt.h>
32
33 #include <systemd/sd-journal.h>
34
35 #include "log.h"
36 #include "util.h"
37 #include "build.h"
38 #include "pager.h"
39 #include "logs-show.h"
40
41 static OutputMode arg_output = OUTPUT_SHORT;
42 static bool arg_follow = false;
43 static bool arg_show_all = false;
44 static bool arg_no_pager = false;
45 static int arg_lines = -1;
46 static bool arg_no_tail = false;
47 static bool arg_new_id128 = false;
48
49 static int help(void) {
50
51 printf("%s [OPTIONS...] {COMMAND} ...\n\n"
52 "Send control commands to or query the journal.\n\n"
53 " -h --help Show this help\n"
54 " --version Show package version\n"
55 " --no-pager Do not pipe output into a pager\n"
56 " -a --all Show all properties, including long and unprintable\n"
57 " -f --follow Follow journal\n"
58 " -n --lines=INTEGER Journal entries to show\n"
59 " --no-tail Show all lines, even in follow mode\n"
60 " -o --output=STRING Change journal output mode (short, short-verbose, verbose, export, json)\n"
61 " --new-id128 Generate a new 128 Bit id\n",
62 program_invocation_short_name);
63
64 return 0;
65 }
66
67 static int parse_argv(int argc, char *argv[]) {
68
69 enum {
70 ARG_VERSION = 0x100,
71 ARG_NO_PAGER,
72 ARG_NO_TAIL,
73 ARG_NEW_ID128
74 };
75
76 static const struct option options[] = {
77 { "help", no_argument, NULL, 'h' },
78 { "version" , no_argument, NULL, ARG_VERSION },
79 { "no-pager", no_argument, NULL, ARG_NO_PAGER },
80 { "follow", no_argument, NULL, 'f' },
81 { "output", required_argument, NULL, 'o' },
82 { "all", no_argument, NULL, 'a' },
83 { "lines", required_argument, NULL, 'n' },
84 { "no-tail", no_argument, NULL, ARG_NO_TAIL },
85 { "new-id128", no_argument, NULL, ARG_NEW_ID128 },
86 { NULL, 0, NULL, 0 }
87 };
88
89 int c, r;
90
91 assert(argc >= 0);
92 assert(argv);
93
94 while ((c = getopt_long(argc, argv, "hfo:an:", options, NULL)) >= 0) {
95
96 switch (c) {
97
98 case 'h':
99 help();
100 return 0;
101
102 case ARG_VERSION:
103 puts(PACKAGE_STRING);
104 puts(DISTRIBUTION);
105 puts(SYSTEMD_FEATURES);
106 return 0;
107
108 case ARG_NO_PAGER:
109 arg_no_pager = true;
110 break;
111
112 case 'f':
113 arg_follow = true;
114 break;
115
116 case 'o':
117 arg_output = output_mode_from_string(optarg);
118 if (arg_output < 0) {
119 log_error("Unknown output '%s'.", optarg);
120 return -EINVAL;
121 }
122
123 break;
124
125 case 'a':
126 arg_show_all = true;
127 break;
128
129 case 'n':
130 r = safe_atoi(optarg, &arg_lines);
131 if (r < 0 || arg_lines < 0) {
132 log_error("Failed to parse lines '%s'", optarg);
133 return -EINVAL;
134 }
135 break;
136
137 case ARG_NO_TAIL:
138 arg_no_tail = true;
139 break;
140
141 case ARG_NEW_ID128:
142 arg_new_id128 = true;
143 break;
144
145 case '?':
146 return -EINVAL;
147
148 default:
149 log_error("Unknown option code %c", c);
150 return -EINVAL;
151 }
152 }
153
154 if (arg_follow && !arg_no_tail)
155 arg_lines = 10;
156
157 return 1;
158 }
159
160 static int generate_new_id128(void) {
161 sd_id128_t id;
162 int r;
163 unsigned i;
164
165 r = sd_id128_randomize(&id);
166 if (r < 0) {
167 log_error("Failed to generate ID: %s", strerror(-r));
168 return r;
169 }
170
171 printf("As string:\n"
172 SD_ID128_FORMAT_STR "\n\n"
173 "As UUID:\n"
174 "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x\n\n"
175 "As macro:\n"
176 "#define MESSAGE_XYZ SD_ID128_MAKE(",
177 SD_ID128_FORMAT_VAL(id),
178 SD_ID128_FORMAT_VAL(id));
179
180 for (i = 0; i < 16; i++)
181 printf("%02x%s", id.bytes[i], i != 15 ? "," : "");
182
183 fputs(")\n", stdout);
184
185 return 0;
186 }
187
188 int main(int argc, char *argv[]) {
189 int r, i, fd;
190 sd_journal *j = NULL;
191 unsigned line = 0;
192 bool need_seek = false;
193
194 log_parse_environment();
195 log_open();
196
197 r = parse_argv(argc, argv);
198 if (r <= 0)
199 goto finish;
200
201 if (arg_new_id128) {
202 r = generate_new_id128();
203 goto finish;
204 }
205
206 r = sd_journal_open(&j, 0);
207 if (r < 0) {
208 log_error("Failed to open journal: %s", strerror(-r));
209 goto finish;
210 }
211
212 for (i = optind; i < argc; i++) {
213 r = sd_journal_add_match(j, argv[i], strlen(argv[i]));
214 if (r < 0) {
215 log_error("Failed to add match: %s", strerror(-r));
216 goto finish;
217 }
218 }
219
220 fd = sd_journal_get_fd(j);
221 if (fd < 0) {
222 log_error("Failed to get wakeup fd: %s", strerror(-fd));
223 goto finish;
224 }
225
226 if (arg_lines >= 0) {
227 r = sd_journal_seek_tail(j);
228 if (r < 0) {
229 log_error("Failed to seek to tail: %s", strerror(-r));
230 goto finish;
231 }
232
233 r = sd_journal_previous_skip(j, arg_lines);
234 } else {
235 r = sd_journal_seek_head(j);
236 if (r < 0) {
237 log_error("Failed to seek to head: %s", strerror(-r));
238 goto finish;
239 }
240
241 r = sd_journal_next(j);
242 }
243
244 if (r < 0) {
245 log_error("Failed to iterate through journal: %s", strerror(-r));
246 goto finish;
247 }
248
249 if (!arg_no_pager && !arg_follow) {
250 columns();
251 pager_open();
252 }
253
254 if (arg_output == OUTPUT_JSON) {
255 fputc('[', stdout);
256 fflush(stdout);
257 }
258
259 for (;;) {
260 for (;;) {
261 if (need_seek) {
262 r = sd_journal_next(j);
263 if (r < 0) {
264 log_error("Failed to iterate through journal: %s", strerror(-r));
265 goto finish;
266 }
267 }
268
269 if (r == 0)
270 break;
271
272 line ++;
273
274 r = output_journal(j, arg_output, line, arg_show_all);
275 if (r < 0)
276 goto finish;
277
278 need_seek = true;
279 }
280
281 if (!arg_follow)
282 break;
283
284 r = fd_wait_for_event(fd, POLLIN);
285 if (r < 0) {
286 log_error("Couldn't wait for event: %s", strerror(-r));
287 goto finish;
288 }
289
290 r = sd_journal_process(j);
291 if (r < 0) {
292 log_error("Failed to process: %s", strerror(-r));
293 goto finish;
294 }
295 }
296
297 if (arg_output == OUTPUT_JSON)
298 fputs("\n]\n", stdout);
299
300 finish:
301 if (j)
302 sd_journal_close(j);
303
304 pager_close();
305
306 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
307 }