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 <systemd/sd-journal.h>
39 #include "logs-show.h"
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;
48 static int help(void) {
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);
65 static int parse_argv(int argc, char *argv[]) {
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 },
90 while ((c = getopt_long(argc, argv, "hfo:an:", options, NULL)) >= 0) {
101 puts(SYSTEMD_FEATURES);
113 arg_output = output_mode_from_string(optarg);
114 if (arg_output < 0) {
115 log_error("Unknown output '%s'.", optarg);
126 r = safe_atoi(optarg, &arg_lines);
127 if (r < 0 || arg_lines < 0) {
128 log_error("Failed to parse lines '%s'", optarg);
141 log_error("Unknown option code %c", c);
146 if (arg_follow && !arg_no_tail)
152 int main(int argc, char *argv[]) {
154 sd_journal *j = NULL;
156 bool need_seek = false;
158 log_parse_environment();
161 r = parse_argv(argc, argv);
165 r = sd_journal_open(&j, 0);
167 log_error("Failed to open journal: %s", strerror(-r));
171 for (i = optind; i < argc; i++) {
172 r = sd_journal_add_match(j, argv[i], strlen(argv[i]));
174 log_error("Failed to add match: %s", strerror(-r));
179 fd = sd_journal_get_fd(j);
181 log_error("Failed to get wakeup fd: %s", strerror(-fd));
185 if (arg_lines >= 0) {
186 r = sd_journal_seek_tail(j);
188 log_error("Failed to seek to tail: %s", strerror(-r));
192 r = sd_journal_previous_skip(j, arg_lines);
194 r = sd_journal_seek_head(j);
196 log_error("Failed to seek to head: %s", strerror(-r));
200 r = sd_journal_next(j);
204 log_error("Failed to iterate through journal: %s", strerror(-r));
208 if (!arg_no_pager && !arg_follow) {
213 if (arg_output == OUTPUT_JSON) {
221 r = sd_journal_next(j);
223 log_error("Failed to iterate through journal: %s", strerror(-r));
233 r = output_journal(j, arg_output, line, arg_show_all);
243 r = fd_wait_for_event(fd, POLLIN);
245 log_error("Couldn't wait for event: %s", strerror(-r));
249 r = sd_journal_process(j);
251 log_error("Failed to process: %s", strerror(-r));
256 if (arg_output == OUTPUT_JSON)
257 fputs("\n]\n", stdout);
265 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;