1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2012 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/>.
27 #include "logs-show.h"
31 #define PRINT_THRESHOLD 128
33 static bool contains_unprintable(const void *p, size_t l) {
36 for (j = p; j < (const char *) p + l; j++)
37 if (*j < ' ' || *j >= 127)
43 static int output_short(sd_journal *j, unsigned line, bool show_all) {
55 r = sd_journal_get_realtime_usec(j, &realtime);
57 log_error("Failed to get realtime: %s", strerror(-r));
61 t = (time_t) (realtime / USEC_PER_SEC);
62 if (strftime(buf, sizeof(buf), "%b %d %H:%M:%S", localtime_r(&t, &tm)) <= 0) {
63 log_error("Failed to format time.");
70 if (sd_journal_get_data(j, "_HOSTNAME", &data, &length) >= 0 &&
71 (show_all || (!contains_unprintable(data, length) &&
72 length < PRINT_THRESHOLD))) {
73 printf(" %.*s", (int) length - 10, ((const char*) data) + 10);
77 if (sd_journal_get_data(j, "MESSAGE", &data, &length) >= 0) {
79 printf(" %.*s", (int) length - 8, ((const char*) data) + 8);
80 else if (contains_unprintable(data, length))
81 fputs(" [blob data]", stdout);
82 else if (length - 8 + n < columns())
83 printf(" %.*s", (int) length - 8, ((const char*) data) + 8);
84 else if (n < columns()) {
87 e = ellipsize_mem((const char *) data + 8, length - 8, columns() - n - 2, 90);
90 printf(" %.*s", (int) length - 8, ((const char*) data) + 8);
103 static int output_verbose(sd_journal *j, unsigned line, bool show_all) {
108 char ts[FORMAT_TIMESTAMP_MAX];
113 r = sd_journal_get_realtime_usec(j, &realtime);
115 log_error("Failed to get realtime timestamp: %s", strerror(-r));
119 r = sd_journal_get_cursor(j, &cursor);
121 log_error("Failed to get cursor: %s", strerror(-r));
126 format_timestamp(ts, sizeof(ts), realtime),
131 SD_JOURNAL_FOREACH_DATA(j, data, length) {
132 if (!show_all && (length > PRINT_THRESHOLD ||
133 contains_unprintable(data, length))) {
136 c = memchr(data, '=', length);
138 log_error("Invalid field.");
142 printf("\t%.*s=[blob data]\n",
143 (int) (c - (const char*) data),
146 printf("\t%.*s\n", (int) length, (const char*) data);
152 static int output_export(sd_journal *j, unsigned line, bool show_all) {
156 usec_t realtime, monotonic;
163 r = sd_journal_get_realtime_usec(j, &realtime);
165 log_error("Failed to get realtime timestamp: %s", strerror(-r));
169 r = sd_journal_get_monotonic_usec(j, &monotonic, &boot_id);
171 log_error("Failed to get monotonic timestamp: %s", strerror(-r));
175 r = sd_journal_get_cursor(j, &cursor);
177 log_error("Failed to get cursor: %s", strerror(-r));
181 printf(".cursor=%s\n"
186 (unsigned long long) realtime,
187 (unsigned long long) monotonic,
188 sd_id128_to_string(boot_id, sid));
192 SD_JOURNAL_FOREACH_DATA(j, data, length) {
194 if (contains_unprintable(data, length)) {
198 c = memchr(data, '=', length);
200 log_error("Invalid field.");
204 fwrite(data, c - (const char*) data, 1, stdout);
206 le64 = htole64(length - (c - (const char*) data) - 1);
207 fwrite(&le64, sizeof(le64), 1, stdout);
208 fwrite(c + 1, length - (c - (const char*) data) - 1, 1, stdout);
210 fwrite(data, length, 1, stdout);
220 static void json_escape(const char* p, size_t l) {
222 if (contains_unprintable(p, l)) {
223 bool not_first = false;
229 printf(", %u", (uint8_t) *p);
232 printf("%u", (uint8_t) *p);
244 if (*p == '"' || *p == '\\') {
258 static int output_json(sd_journal *j, unsigned line, bool show_all) {
259 uint64_t realtime, monotonic;
269 r = sd_journal_get_realtime_usec(j, &realtime);
271 log_error("Failed to get realtime timestamp: %s", strerror(-r));
275 r = sd_journal_get_monotonic_usec(j, &monotonic, &boot_id);
277 log_error("Failed to get monotonic timestamp: %s", strerror(-r));
281 r = sd_journal_get_cursor(j, &cursor);
283 log_error("Failed to get cursor: %s", strerror(-r));
290 fputs(",\n", stdout);
293 "\t\".cursor\" : \"%s\",\n"
294 "\t\".realtime\" : %llu,\n"
295 "\t\".monotonic\" : %llu,\n"
296 "\t\".boot_id\" : \"%s\"",
298 (unsigned long long) realtime,
299 (unsigned long long) monotonic,
300 sd_id128_to_string(boot_id, sid));
304 SD_JOURNAL_FOREACH_DATA(j, data, length) {
307 c = memchr(data, '=', length);
309 log_error("Invalid field.");
313 fputs(",\n\t", stdout);
314 json_escape(data, c - (const char*) data);
315 fputs(" : ", stdout);
316 json_escape(c + 1, length - (c - (const char*) data) - 1);
319 fputs("\n}", stdout);
325 static int (*output_funcs[_OUTPUT_MODE_MAX])(sd_journal*j, unsigned line, bool show_all) = {
326 [OUTPUT_SHORT] = output_short,
327 [OUTPUT_VERBOSE] = output_verbose,
328 [OUTPUT_EXPORT] = output_export,
329 [OUTPUT_JSON] = output_json
332 int output_journal(sd_journal *j, OutputMode mode, unsigned line, bool show_all) {
334 assert(mode < _OUTPUT_MODE_MAX);
336 return output_funcs[mode](j, line, show_all);
339 int show_journal_by_unit(
354 bool need_seek = false;
357 assert(mode < _OUTPUT_MODE_MAX);
360 if (!endswith(unit, ".service") &&
361 !endswith(unit, ".socket") &&
362 !endswith(unit, ".mount") &&
363 !endswith(unit, ".swap"))
370 n_columns = columns();
375 if (asprintf(&m, "_SYSTEMD_UNIT=%s", unit) < 0) {
380 r = sd_journal_open(&j, SD_JOURNAL_LOCAL_ONLY|SD_JOURNAL_SYSTEM_ONLY);
384 fd = sd_journal_get_fd(j);
388 r = sd_journal_add_match(j, m, strlen(m));
392 r = sd_journal_seek_tail(j);
396 r = sd_journal_previous_skip(j, how_many);
400 if (mode == OUTPUT_JSON) {
410 r = sd_journal_next(j);
420 if (not_before > 0) {
421 r = sd_journal_get_monotonic_usec(j, &usec, NULL);
423 /* -ESTALE is returned if the
424 timestamp is not from this boot */
430 if (usec < not_before)
436 r = output_journal(j, mode, line, show_all);
444 r = fd_wait_for_event(fd, POLLIN);
448 r = sd_journal_process(j);
454 if (mode == OUTPUT_JSON)
455 fputs("\n]\n", stdout);
467 static const char *const output_mode_table[_OUTPUT_MODE_MAX] = {
468 [OUTPUT_SHORT] = "short",
469 [OUTPUT_VERBOSE] = "verbose",
470 [OUTPUT_EXPORT] = "export",
471 [OUTPUT_JSON] = "json"
474 DEFINE_STRING_TABLE_LOOKUP(output_mode, OutputMode);