chiark / gitweb /
journalctl: only output 10 most recent lines in --follow mode
[elogind.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 "sd-journal.h"
34 #include "log.h"
35 #include "util.h"
36 #include "build.h"
37 #include "pager.h"
38 #include "logs-show.h"
39
40 static output_mode arg_output = OUTPUT_SHORT;
41 static bool arg_follow = false;
42 static bool arg_show_all = false;
43 static bool arg_no_pager = false;
44 static int arg_lines = -1;
45 static bool arg_no_tail = false;
46
47 static int help(void) {
48
49         printf("%s [OPTIONS...] {COMMAND} ...\n\n"
50                "Send control commands to or query the login manager.\n\n"
51                "  -h --help           Show this help\n"
52                "     --version        Show package version\n"
53                "     --no-pager       Do not pipe output into a pager\n"
54                "  -a --all            Show all properties, including long and unprintable\n"
55                "  -f --follow         Follow journal\n"
56                "  -n --lines=INTEGER  Lines to show\n"
57                "     --no-tail        Show all lines, even in follow mode\n"
58                "  -o --output=STRING  Change output mode (short, verbose, export, json)\n",
59                program_invocation_short_name);
60
61         return 0;
62 }
63
64 static int parse_argv(int argc, char *argv[]) {
65
66         enum {
67                 ARG_VERSION = 0x100,
68                 ARG_NO_PAGER,
69                 ARG_NO_TAIL
70         };
71
72         static const struct option options[] = {
73                 { "help",      no_argument,       NULL, 'h'           },
74                 { "version" ,  no_argument,       NULL, ARG_VERSION   },
75                 { "no-pager",  no_argument,       NULL, ARG_NO_PAGER  },
76                 { "follow",    no_argument,       NULL, 'f'           },
77                 { "output",    required_argument, NULL, 'o'           },
78                 { "all",       no_argument,       NULL, 'a'           },
79                 { "lines",     required_argument, NULL, 'n'           },
80                 { "no-tail",   no_argument,       NULL, ARG_NO_TAIL   },
81                 { NULL,        0,                 NULL, 0             }
82         };
83
84         int c, r;
85
86         assert(argc >= 0);
87         assert(argv);
88
89         while ((c = getopt_long(argc, argv, "hfo:an:", options, NULL)) >= 0) {
90
91                 switch (c) {
92
93                 case 'h':
94                         help();
95                         return 0;
96
97                 case ARG_VERSION:
98                         puts(PACKAGE_STRING);
99                         puts(DISTRIBUTION);
100                         puts(SYSTEMD_FEATURES);
101                         return 0;
102
103                 case ARG_NO_PAGER:
104                         arg_no_pager = true;
105                         break;
106
107                 case 'f':
108                         arg_follow = true;
109                         break;
110
111                 case 'o':
112                         if (streq(optarg, "short"))
113                                 arg_output = OUTPUT_SHORT;
114                         else if (streq(optarg, "verbose"))
115                                 arg_output = OUTPUT_VERBOSE;
116                         else if (streq(optarg, "export"))
117                                 arg_output = OUTPUT_EXPORT;
118                         else if (streq(optarg, "json"))
119                                 arg_output = OUTPUT_JSON;
120                         else {
121                                 log_error("Unknown output '%s'.", optarg);
122                                 return -EINVAL;
123                         }
124                         break;
125
126                 case 'a':
127                         arg_show_all = true;
128                         break;
129
130                 case 'n':
131                         r = safe_atoi(optarg, &arg_lines);
132                         if (r < 0 || arg_lines < 0) {
133                                 log_error("Failed to parse lines '%s'", optarg);
134                                 return -EINVAL;
135                         }
136                         break;
137
138                 case ARG_NO_TAIL:
139                         arg_no_tail = true;
140                         break;
141
142                 case '?':
143                         return -EINVAL;
144
145                 default:
146                         log_error("Unknown option code %c", c);
147                         return -EINVAL;
148                 }
149         }
150
151         if (arg_follow && !arg_no_tail)
152                 arg_lines = 10;
153
154         return 1;
155 }
156
157 int main(int argc, char *argv[]) {
158         int r, i, fd;
159         sd_journal *j = NULL;
160         unsigned line = 0;
161         bool need_seek = false;
162
163         log_parse_environment();
164         log_open();
165
166         r = parse_argv(argc, argv);
167         if (r <= 0)
168                 goto finish;
169
170         r = sd_journal_open(&j, 0);
171         if (r < 0) {
172                 log_error("Failed to open journal: %s", strerror(-r));
173                 goto finish;
174         }
175
176         for (i = optind; i < argc; i++) {
177                 r = sd_journal_add_match(j, argv[i], strlen(argv[i]));
178                 if (r < 0) {
179                         log_error("Failed to add match: %s", strerror(-r));
180                         goto finish;
181                 }
182         }
183
184         fd = sd_journal_get_fd(j);
185         if (fd < 0) {
186                 log_error("Failed to get wakeup fd: %s", strerror(-fd));
187                 goto finish;
188         }
189
190         if (arg_lines >= 0) {
191                 r = sd_journal_seek_tail(j);
192                 if (r < 0) {
193                         log_error("Failed to seek to tail: %s", strerror(-r));
194                         goto finish;
195                 }
196
197                 r = sd_journal_previous_skip(j, arg_lines);
198         } else {
199                 r = sd_journal_seek_head(j);
200                 if (r < 0) {
201                         log_error("Failed to seek to head: %s", strerror(-r));
202                         goto finish;
203                 }
204
205                 r = sd_journal_next(j);
206         }
207
208         if (r < 0) {
209                 log_error("Failed to iterate through journal: %s", strerror(-r));
210                 goto finish;
211         }
212
213         if (!arg_no_pager && !arg_follow) {
214                 columns();
215                 pager_open();
216         }
217
218         if (arg_output == OUTPUT_JSON) {
219                 fputc('[', stdout);
220                 fflush(stdout);
221         }
222
223         for (;;) {
224                 struct pollfd pollfd;
225
226                 for (;;) {
227                         if (need_seek) {
228                                 r = sd_journal_next(j);
229                                 if (r < 0) {
230                                         log_error("Failed to iterate through journal: %s", strerror(-r));
231                                         goto finish;
232                                 }
233                         }
234
235                         if (r == 0)
236                                 break;
237
238                         line ++;
239
240                         r = output_journal(j, arg_output, line, arg_show_all);
241                         if (r < 0)
242                                 goto finish;
243
244                         need_seek = true;
245                 }
246
247                 if (!arg_follow)
248                         break;
249
250                 zero(pollfd);
251                 pollfd.fd = fd;
252                 pollfd.events = POLLIN;
253
254                 if (poll(&pollfd, 1, -1) < 0) {
255                         if (errno == EINTR)
256                                 break;
257
258                         log_error("poll(): %m");
259                         r = -errno;
260                         goto finish;
261                 }
262
263                 r = sd_journal_process(j);
264                 if (r < 0) {
265                         log_error("Failed to process: %s", strerror(-r));
266                         goto finish;
267                 }
268         }
269
270         if (arg_output == OUTPUT_JSON)
271                 fputs("\n]\n", stdout);
272
273 finish:
274         if (j)
275                 sd_journal_close(j);
276
277         pager_close();
278
279         return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
280 }