1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2011 Lennart Poettering
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.
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.
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/>.
33 #include "sd-journal.h"
38 #include "logs-show.h"
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;
47 static int help(void) {
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);
64 static int parse_argv(int argc, char *argv[]) {
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 },
89 while ((c = getopt_long(argc, argv, "hfo:an:", options, NULL)) >= 0) {
100 puts(SYSTEMD_FEATURES);
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;
121 log_error("Unknown output '%s'.", optarg);
131 r = safe_atoi(optarg, &arg_lines);
132 if (r < 0 || arg_lines < 0) {
133 log_error("Failed to parse lines '%s'", optarg);
146 log_error("Unknown option code %c", c);
151 if (arg_follow && !arg_no_tail)
157 int main(int argc, char *argv[]) {
159 sd_journal *j = NULL;
161 bool need_seek = false;
163 log_parse_environment();
166 r = parse_argv(argc, argv);
170 r = sd_journal_open(&j, 0);
172 log_error("Failed to open journal: %s", strerror(-r));
176 for (i = optind; i < argc; i++) {
177 r = sd_journal_add_match(j, argv[i], strlen(argv[i]));
179 log_error("Failed to add match: %s", strerror(-r));
184 fd = sd_journal_get_fd(j);
186 log_error("Failed to get wakeup fd: %s", strerror(-fd));
190 if (arg_lines >= 0) {
191 r = sd_journal_seek_tail(j);
193 log_error("Failed to seek to tail: %s", strerror(-r));
197 r = sd_journal_previous_skip(j, arg_lines);
199 r = sd_journal_seek_head(j);
201 log_error("Failed to seek to head: %s", strerror(-r));
205 r = sd_journal_next(j);
209 log_error("Failed to iterate through journal: %s", strerror(-r));
213 if (!arg_no_pager && !arg_follow) {
218 if (arg_output == OUTPUT_JSON) {
224 struct pollfd pollfd;
228 r = sd_journal_next(j);
230 log_error("Failed to iterate through journal: %s", strerror(-r));
240 r = output_journal(j, arg_output, line, arg_show_all);
252 pollfd.events = POLLIN;
254 if (poll(&pollfd, 1, -1) < 0) {
258 log_error("poll(): %m");
263 r = sd_journal_process(j);
265 log_error("Failed to process: %s", strerror(-r));
270 if (arg_output == OUTPUT_JSON)
271 fputs("\n]\n", stdout);
279 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;