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/>.
32 #include "sd-journal.h"
36 #define PRINT_THRESHOLD 128
44 } arg_output = OUTPUT_JSON;
46 static bool arg_follow = false;
47 static bool arg_show_all = false;
49 static bool contains_unprintable(const void *p, size_t l) {
52 for (j = p; j < (const char *) p + l; j++)
53 if (*j < ' ' || *j >= 127)
59 static int output_short(sd_journal *j, unsigned line) {
71 r = sd_journal_get_realtime_usec(j, &realtime);
73 log_error("Failed to get realtime: %s", strerror(-r));
77 t = (time_t) (realtime / USEC_PER_SEC);
78 if (strftime(buf, sizeof(buf), "%b %d %H:%M:%S", localtime_r(&t, &tm)) <= 0) {
79 log_error("Failed to format time.");
86 if (sd_journal_get_data(j, "_HOSTNAME", &data, &length) >= 0 &&
87 (arg_show_all || (!contains_unprintable(data, length) &&
88 length < PRINT_THRESHOLD))) {
89 printf(" %.*s", (int) length - 10, ((const char*) data) + 10);
93 if (sd_journal_get_data(j, "MESSAGE", &data, &length) >= 0) {
95 printf(" %.*s", (int) length - 8, ((const char*) data) + 8);
96 else if (contains_unprintable(data, length))
97 fputs(" [blob data]", stdout);
98 else if (length - 8 + n < columns())
99 printf(" %.*s", (int) length - 8, ((const char*) data) + 8);
100 else if (n < columns()) {
103 e = ellipsize_mem((const char *) data + 8, length - 8, columns() - n - 2, 90);
106 printf(" %.*s", (int) length - 8, ((const char*) data) + 8);
119 static int output_verbose(sd_journal *j, unsigned line) {
124 char ts[FORMAT_TIMESTAMP_MAX];
129 r = sd_journal_get_realtime_usec(j, &realtime);
131 log_error("Failed to get realtime timestamp: %s", strerror(-r));
135 r = sd_journal_get_cursor(j, &cursor);
137 log_error("Failed to get cursor: %s", strerror(-r));
142 format_timestamp(ts, sizeof(ts), realtime),
147 SD_JOURNAL_FOREACH_DATA(j, data, length) {
148 if (!arg_show_all && (length > PRINT_THRESHOLD ||
149 contains_unprintable(data, length))) {
152 c = memchr(data, '=', length);
154 log_error("Invalid field.");
158 printf("\t%.*s=[blob data]\n",
159 (int) (c - (const char*) data),
162 printf("\t%.*s\n", (int) length, (const char*) data);
168 static int output_export(sd_journal *j, unsigned line) {
172 usec_t realtime, monotonic;
179 r = sd_journal_get_realtime_usec(j, &realtime);
181 log_error("Failed to get realtime timestamp: %s", strerror(-r));
185 r = sd_journal_get_monotonic_usec(j, &monotonic, &boot_id);
187 log_error("Failed to get monotonic timestamp: %s", strerror(-r));
191 r = sd_journal_get_cursor(j, &cursor);
193 log_error("Failed to get cursor: %s", strerror(-r));
197 printf(".cursor=%s\n"
202 (unsigned long long) realtime,
203 (unsigned long long) monotonic,
204 sd_id128_to_string(boot_id, sid));
208 SD_JOURNAL_FOREACH_DATA(j, data, length) {
210 if (contains_unprintable(data, length)) {
214 c = memchr(data, '=', length);
216 log_error("Invalid field.");
220 fwrite(data, c - (const char*) data, 1, stdout);
222 le64 = htole64(length - (c - (const char*) data) - 1);
223 fwrite(&le64, sizeof(le64), 1, stdout);
224 fwrite(c + 1, length - (c - (const char*) data) - 1, 1, stdout);
226 fwrite(data, length, 1, stdout);
236 static void json_escape(const char* p, size_t l) {
238 if (contains_unprintable(p, l)) {
239 bool not_first = false;
245 printf(", %u", (uint8_t) *p);
248 printf("%u", (uint8_t) *p);
260 if (*p == '"' || *p == '\\') {
274 static int output_json(sd_journal *j, unsigned line) {
275 uint64_t realtime, monotonic;
285 r = sd_journal_get_realtime_usec(j, &realtime);
287 log_error("Failed to get realtime timestamp: %s", strerror(-r));
291 r = sd_journal_get_monotonic_usec(j, &monotonic, &boot_id);
293 log_error("Failed to get monotonic timestamp: %s", strerror(-r));
297 r = sd_journal_get_cursor(j, &cursor);
299 log_error("Failed to get cursor: %s", strerror(-r));
306 fputs(",\n", stdout);
309 "\t\".cursor\" : \"%s\",\n"
310 "\t\".realtime\" : %llu,\n"
311 "\t\".monotonic\" : %llu,\n"
312 "\t\".boot_id\" : \"%s\"",
314 (unsigned long long) realtime,
315 (unsigned long long) monotonic,
316 sd_id128_to_string(boot_id, sid));
320 SD_JOURNAL_FOREACH_DATA(j, data, length) {
323 c = memchr(data, '=', length);
325 log_error("Invalid field.");
329 fputs(",\n\t", stdout);
330 json_escape(data, c - (const char*) data);
331 fputs(" : ", stdout);
332 json_escape(c + 1, length - (c - (const char*) data) - 1);
335 fputs("\n}", stdout);
341 static int (*output_funcs[_OUTPUT_MAX])(sd_journal*j, unsigned line) = {
342 [OUTPUT_SHORT] = output_short,
343 [OUTPUT_VERBOSE] = output_verbose,
344 [OUTPUT_EXPORT] = output_export,
345 [OUTPUT_JSON] = output_json
348 int main(int argc, char *argv[]) {
350 sd_journal *j = NULL;
353 log_set_max_level(LOG_DEBUG);
354 log_set_target(LOG_TARGET_CONSOLE);
356 log_parse_environment();
359 r = sd_journal_open(&j);
361 log_error("Failed to open journal: %s", strerror(-r));
365 for (i = 1; i < argc; i++) {
366 r = sd_journal_add_match(j, argv[i], strlen(argv[i]));
368 log_error("Failed to add match: %s", strerror(-r));
373 fd = sd_journal_get_fd(j);
375 log_error("Failed to get wakeup fd: %s", strerror(-fd));
379 r = sd_journal_seek_head(j);
381 log_error("Failed to seek to head: %s", strerror(-r));
385 if (arg_output == OUTPUT_JSON)
389 struct pollfd pollfd;
391 while (sd_journal_next(j) > 0) {
394 r = output_funcs[arg_output](j, line);
404 pollfd.events = POLLIN;
406 if (poll(&pollfd, 1, -1) < 0) {
410 log_error("poll(): %m");
415 r = sd_journal_process(j);
417 log_error("Failed to process: %s", strerror(-r));
422 if (arg_output == OUTPUT_JSON)
423 fputs("\n]\n", stdout);
429 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;