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