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"
39 #define PRINT_THRESHOLD 128
47 } arg_output = OUTPUT_SHORT;
49 static bool arg_follow = false;
50 static bool arg_show_all = false;
51 static bool arg_no_pager = false;
53 static bool contains_unprintable(const void *p, size_t l) {
56 for (j = p; j < (const char *) p + l; j++)
57 if (*j < ' ' || *j >= 127)
63 static int output_short(sd_journal *j, unsigned line) {
75 r = sd_journal_get_realtime_usec(j, &realtime);
77 log_error("Failed to get realtime: %s", strerror(-r));
81 t = (time_t) (realtime / USEC_PER_SEC);
82 if (strftime(buf, sizeof(buf), "%b %d %H:%M:%S", localtime_r(&t, &tm)) <= 0) {
83 log_error("Failed to format time.");
90 if (sd_journal_get_data(j, "_HOSTNAME", &data, &length) >= 0 &&
91 (arg_show_all || (!contains_unprintable(data, length) &&
92 length < PRINT_THRESHOLD))) {
93 printf(" %.*s", (int) length - 10, ((const char*) data) + 10);
97 if (sd_journal_get_data(j, "MESSAGE", &data, &length) >= 0) {
99 printf(" %.*s", (int) length - 8, ((const char*) data) + 8);
100 else if (contains_unprintable(data, length))
101 fputs(" [blob data]", stdout);
102 else if (length - 8 + n < columns())
103 printf(" %.*s", (int) length - 8, ((const char*) data) + 8);
104 else if (n < columns()) {
107 e = ellipsize_mem((const char *) data + 8, length - 8, columns() - n - 2, 90);
110 printf(" %.*s", (int) length - 8, ((const char*) data) + 8);
123 static int output_verbose(sd_journal *j, unsigned line) {
128 char ts[FORMAT_TIMESTAMP_MAX];
133 r = sd_journal_get_realtime_usec(j, &realtime);
135 log_error("Failed to get realtime timestamp: %s", strerror(-r));
139 r = sd_journal_get_cursor(j, &cursor);
141 log_error("Failed to get cursor: %s", strerror(-r));
146 format_timestamp(ts, sizeof(ts), realtime),
151 SD_JOURNAL_FOREACH_DATA(j, data, length) {
152 if (!arg_show_all && (length > PRINT_THRESHOLD ||
153 contains_unprintable(data, length))) {
156 c = memchr(data, '=', length);
158 log_error("Invalid field.");
162 printf("\t%.*s=[blob data]\n",
163 (int) (c - (const char*) data),
166 printf("\t%.*s\n", (int) length, (const char*) data);
172 static int output_export(sd_journal *j, unsigned line) {
176 usec_t realtime, monotonic;
183 r = sd_journal_get_realtime_usec(j, &realtime);
185 log_error("Failed to get realtime timestamp: %s", strerror(-r));
189 r = sd_journal_get_monotonic_usec(j, &monotonic, &boot_id);
191 log_error("Failed to get monotonic timestamp: %s", strerror(-r));
195 r = sd_journal_get_cursor(j, &cursor);
197 log_error("Failed to get cursor: %s", strerror(-r));
201 printf(".cursor=%s\n"
206 (unsigned long long) realtime,
207 (unsigned long long) monotonic,
208 sd_id128_to_string(boot_id, sid));
212 SD_JOURNAL_FOREACH_DATA(j, data, length) {
214 if (contains_unprintable(data, length)) {
218 c = memchr(data, '=', length);
220 log_error("Invalid field.");
224 fwrite(data, c - (const char*) data, 1, stdout);
226 le64 = htole64(length - (c - (const char*) data) - 1);
227 fwrite(&le64, sizeof(le64), 1, stdout);
228 fwrite(c + 1, length - (c - (const char*) data) - 1, 1, stdout);
230 fwrite(data, length, 1, stdout);
240 static void json_escape(const char* p, size_t l) {
242 if (contains_unprintable(p, l)) {
243 bool not_first = false;
249 printf(", %u", (uint8_t) *p);
252 printf("%u", (uint8_t) *p);
264 if (*p == '"' || *p == '\\') {
278 static int output_json(sd_journal *j, unsigned line) {
279 uint64_t realtime, monotonic;
289 r = sd_journal_get_realtime_usec(j, &realtime);
291 log_error("Failed to get realtime timestamp: %s", strerror(-r));
295 r = sd_journal_get_monotonic_usec(j, &monotonic, &boot_id);
297 log_error("Failed to get monotonic timestamp: %s", strerror(-r));
301 r = sd_journal_get_cursor(j, &cursor);
303 log_error("Failed to get cursor: %s", strerror(-r));
310 fputs(",\n", stdout);
313 "\t\".cursor\" : \"%s\",\n"
314 "\t\".realtime\" : %llu,\n"
315 "\t\".monotonic\" : %llu,\n"
316 "\t\".boot_id\" : \"%s\"",
318 (unsigned long long) realtime,
319 (unsigned long long) monotonic,
320 sd_id128_to_string(boot_id, sid));
324 SD_JOURNAL_FOREACH_DATA(j, data, length) {
327 c = memchr(data, '=', length);
329 log_error("Invalid field.");
333 fputs(",\n\t", stdout);
334 json_escape(data, c - (const char*) data);
335 fputs(" : ", stdout);
336 json_escape(c + 1, length - (c - (const char*) data) - 1);
339 fputs("\n}", stdout);
345 static int (*output_funcs[_OUTPUT_MAX])(sd_journal*j, unsigned line) = {
346 [OUTPUT_SHORT] = output_short,
347 [OUTPUT_VERBOSE] = output_verbose,
348 [OUTPUT_EXPORT] = output_export,
349 [OUTPUT_JSON] = output_json
352 static int help(void) {
354 printf("%s [OPTIONS...] {COMMAND} ...\n\n"
355 "Send control commands to or query the login manager.\n\n"
356 " -h --help Show this help\n"
357 " --version Show package version\n"
358 " --no-pager Do not pipe output into a pager\n"
359 " -a --all Show all properties, including long and unprintable\n"
360 " -f --follow Follow journal\n"
361 " -o --output=STRING Change output mode (short, verbose, export, json)\n",
362 program_invocation_short_name);
367 static int parse_argv(int argc, char *argv[]) {
374 static const struct option options[] = {
375 { "help", no_argument, NULL, 'h' },
376 { "version" , no_argument, NULL, ARG_VERSION },
377 { "no-pager", no_argument, NULL, ARG_NO_PAGER },
378 { "follow", no_argument, NULL, 'f' },
379 { "output", required_argument, NULL, 'o' },
380 { "all", no_argument, NULL, 'a' },
389 while ((c = getopt_long(argc, argv, "hfo:a", options, NULL)) >= 0) {
398 puts(PACKAGE_STRING);
400 puts(SYSTEMD_FEATURES);
412 if (streq(optarg, "short"))
413 arg_output = OUTPUT_SHORT;
414 else if (streq(optarg, "verbose"))
415 arg_output = OUTPUT_VERBOSE;
416 else if (streq(optarg, "export"))
417 arg_output = OUTPUT_EXPORT;
418 else if (streq(optarg, "json"))
419 arg_output = OUTPUT_JSON;
421 log_error("Unknown output '%s'.", optarg);
434 log_error("Unknown option code %c", c);
442 int main(int argc, char *argv[]) {
444 sd_journal *j = NULL;
447 log_parse_environment();
450 r = parse_argv(argc, argv);
454 r = sd_journal_open(&j, 0);
456 log_error("Failed to open journal: %s", strerror(-r));
460 for (i = optind; i < argc; i++) {
461 r = sd_journal_add_match(j, argv[i], strlen(argv[i]));
463 log_error("Failed to add match: %s", strerror(-r));
468 fd = sd_journal_get_fd(j);
470 log_error("Failed to get wakeup fd: %s", strerror(-fd));
474 r = sd_journal_seek_head(j);
476 log_error("Failed to seek to head: %s", strerror(-r));
480 if (!arg_no_pager && !arg_follow) {
485 if (arg_output == OUTPUT_JSON) {
491 struct pollfd pollfd;
494 r = sd_journal_next(j);
497 log_error("Failed to iterate through journal: %s", strerror(-r));
506 r = output_funcs[arg_output](j, line);
516 pollfd.events = POLLIN;
518 if (poll(&pollfd, 1, -1) < 0) {
522 log_error("poll(): %m");
527 r = sd_journal_process(j);
529 log_error("Failed to process: %s", strerror(-r));
534 if (arg_output == OUTPUT_JSON)
535 fputs("\n]\n", stdout);
543 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;