chiark / gitweb /
17d6a7fc2355d53f244dadf4ee2180cbce30fb39
[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 OutputMode 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 journal.\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  Journal entries to show\n"
57                "     --no-tail        Show all lines, even in follow mode\n"
58                "  -o --output=STRING  Change journal 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                         arg_output =  output_mode_from_string(optarg);
113                         if (arg_output < 0) {
114                                 log_error("Unknown output '%s'.", optarg);
115                                 return -EINVAL;
116                         }
117
118                         break;
119
120                 case 'a':
121                         arg_show_all = true;
122                         break;
123
124                 case 'n':
125                         r = safe_atoi(optarg, &arg_lines);
126                         if (r < 0 || arg_lines < 0) {
127                                 log_error("Failed to parse lines '%s'", optarg);
128                                 return -EINVAL;
129                         }
130                         break;
131
132                 case ARG_NO_TAIL:
133                         arg_no_tail = true;
134                         break;
135
136                 case '?':
137                         return -EINVAL;
138
139                 default:
140                         log_error("Unknown option code %c", c);
141                         return -EINVAL;
142                 }
143         }
144
145         if (arg_follow && !arg_no_tail)
146                 arg_lines = 10;
147
148         return 1;
149 }
150
151 int main(int argc, char *argv[]) {
152         int r, i, fd;
153         sd_journal *j = NULL;
154         unsigned line = 0;
155         bool need_seek = false;
156
157         log_parse_environment();
158         log_open();
159
160         r = parse_argv(argc, argv);
161         if (r <= 0)
162                 goto finish;
163
164         r = sd_journal_open(&j, 0);
165         if (r < 0) {
166                 log_error("Failed to open journal: %s", strerror(-r));
167                 goto finish;
168         }
169
170         for (i = optind; i < argc; i++) {
171                 r = sd_journal_add_match(j, argv[i], strlen(argv[i]));
172                 if (r < 0) {
173                         log_error("Failed to add match: %s", strerror(-r));
174                         goto finish;
175                 }
176         }
177
178         fd = sd_journal_get_fd(j);
179         if (fd < 0) {
180                 log_error("Failed to get wakeup fd: %s", strerror(-fd));
181                 goto finish;
182         }
183
184         if (arg_lines >= 0) {
185                 r = sd_journal_seek_tail(j);
186                 if (r < 0) {
187                         log_error("Failed to seek to tail: %s", strerror(-r));
188                         goto finish;
189                 }
190
191                 r = sd_journal_previous_skip(j, arg_lines);
192         } else {
193                 r = sd_journal_seek_head(j);
194                 if (r < 0) {
195                         log_error("Failed to seek to head: %s", strerror(-r));
196                         goto finish;
197                 }
198
199                 r = sd_journal_next(j);
200         }
201
202         if (r < 0) {
203                 log_error("Failed to iterate through journal: %s", strerror(-r));
204                 goto finish;
205         }
206
207         if (!arg_no_pager && !arg_follow) {
208                 columns();
209                 pager_open();
210         }
211
212         if (arg_output == OUTPUT_JSON) {
213                 fputc('[', stdout);
214                 fflush(stdout);
215         }
216
217         for (;;) {
218                 for (;;) {
219                         if (need_seek) {
220                                 r = sd_journal_next(j);
221                                 if (r < 0) {
222                                         log_error("Failed to iterate through journal: %s", strerror(-r));
223                                         goto finish;
224                                 }
225                         }
226
227                         if (r == 0)
228                                 break;
229
230                         line ++;
231
232                         r = output_journal(j, arg_output, line, arg_show_all);
233                         if (r < 0)
234                                 goto finish;
235
236                         need_seek = true;
237                 }
238
239                 if (!arg_follow)
240                         break;
241
242                 r = fd_wait_for_event(fd, POLLIN);
243                 if (r < 0) {
244                         log_error("Couldn't wait for event: %s", strerror(-r));
245                         goto finish;
246                 }
247
248                 r = sd_journal_process(j);
249                 if (r < 0) {
250                         log_error("Failed to process: %s", strerror(-r));
251                         goto finish;
252                 }
253         }
254
255         if (arg_output == OUTPUT_JSON)
256                 fputs("\n]\n", stdout);
257
258 finish:
259         if (j)
260                 sd_journal_close(j);
261
262         pager_close();
263
264         return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
265 }