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/>.
28 #include "logs-show.h"
32 #define PRINT_THRESHOLD 128
34 static bool contains_unprintable(const void *p, size_t l) {
37 for (j = p; j < (const char *) p + l; j++)
38 if (*j < ' ' || *j >= 127)
44 static int parse_field(const void *data, size_t length, const char *field, char **target, size_t *target_size) {
57 if (memcmp(data, field, fl))
61 buf = memdup((const char*) data + fl, nl);
63 log_error("Out of memory");
73 static bool shall_print(bool show_all, char *p, size_t l) {
77 if (l > PRINT_THRESHOLD)
80 if (contains_unprintable(p, l))
86 static int output_short(sd_journal *j, unsigned line, bool show_all) {
95 char *hostname = NULL, *identifier = NULL, *comm = NULL, *pid = NULL, *message = NULL;
96 size_t hostname_len = 0, identifier_len = 0, comm_len = 0, pid_len = 0, message_len = 0;
100 SD_JOURNAL_FOREACH_DATA(j, data, length) {
102 r = parse_field(data, length, "_HOSTNAME=", &hostname, &hostname_len);
108 r = parse_field(data, length, "SYSLOG_IDENTIFIER=", &identifier, &identifier_len);
114 r = parse_field(data, length, "_COMM=", &comm, &comm_len);
120 r = parse_field(data, length, "_PID=", &pid, &pid_len);
126 r = parse_field(data, length, "MESSAGE=", &message, &message_len);
136 r = sd_journal_get_realtime_usec(j, &realtime);
138 log_error("Failed to get realtime: %s", strerror(-r));
142 t = (time_t) (realtime / USEC_PER_SEC);
143 if (strftime(buf, sizeof(buf), "%b %d %H:%M:%S", localtime_r(&t, &tm)) <= 0) {
144 log_error("Failed to format time.");
151 if (hostname && shall_print(show_all, hostname, hostname_len)) {
152 printf(" %.*s", (int) hostname_len, hostname);
153 n += hostname_len + 1;
156 if (identifier && shall_print(show_all, identifier, identifier_len)) {
157 printf(" %.*s", (int) identifier_len, identifier);
158 n += identifier_len + 1;
159 } else if (comm && shall_print(show_all, comm, comm_len)) {
160 printf(" %.*s", (int) comm_len, comm);
164 if (pid && shall_print(show_all, pid, pid_len)) {
165 printf("[%.*s]", (int) pid_len, pid);
170 printf(": %.*s\n", (int) message_len, message);
171 else if (contains_unprintable(message, message_len))
172 fputs(": [blob data]\n", stdout);
173 else if (message_len + n < columns())
174 printf(": %.*s\n", (int) message_len, message);
175 else if (n < columns()) {
178 e = ellipsize_mem(message, message_len, columns() - n - 2, 90);
181 printf(": %.*s\n", (int) message_len, message);
201 static int output_verbose(sd_journal *j, unsigned line, bool show_all) {
206 char ts[FORMAT_TIMESTAMP_MAX];
211 r = sd_journal_get_realtime_usec(j, &realtime);
213 log_error("Failed to get realtime timestamp: %s", strerror(-r));
217 r = sd_journal_get_cursor(j, &cursor);
219 log_error("Failed to get cursor: %s", strerror(-r));
224 format_timestamp(ts, sizeof(ts), realtime),
229 SD_JOURNAL_FOREACH_DATA(j, data, length) {
230 if (!show_all && (length > PRINT_THRESHOLD ||
231 contains_unprintable(data, length))) {
234 c = memchr(data, '=', length);
236 log_error("Invalid field.");
240 printf("\t%.*s=[blob data]\n",
241 (int) (c - (const char*) data),
244 printf("\t%.*s\n", (int) length, (const char*) data);
250 static int output_export(sd_journal *j, unsigned line, bool show_all) {
254 usec_t realtime, monotonic;
261 r = sd_journal_get_realtime_usec(j, &realtime);
263 log_error("Failed to get realtime timestamp: %s", strerror(-r));
267 r = sd_journal_get_monotonic_usec(j, &monotonic, &boot_id);
269 log_error("Failed to get monotonic timestamp: %s", strerror(-r));
273 r = sd_journal_get_cursor(j, &cursor);
275 log_error("Failed to get cursor: %s", strerror(-r));
279 printf(".cursor=%s\n"
284 (unsigned long long) realtime,
285 (unsigned long long) monotonic,
286 sd_id128_to_string(boot_id, sid));
290 SD_JOURNAL_FOREACH_DATA(j, data, length) {
292 if (contains_unprintable(data, length)) {
296 c = memchr(data, '=', length);
298 log_error("Invalid field.");
302 fwrite(data, c - (const char*) data, 1, stdout);
304 le64 = htole64(length - (c - (const char*) data) - 1);
305 fwrite(&le64, sizeof(le64), 1, stdout);
306 fwrite(c + 1, length - (c - (const char*) data) - 1, 1, stdout);
308 fwrite(data, length, 1, stdout);
318 static void json_escape(const char* p, size_t l) {
320 if (contains_unprintable(p, l)) {
321 bool not_first = false;
327 printf(", %u", (uint8_t) *p);
330 printf("%u", (uint8_t) *p);
342 if (*p == '"' || *p == '\\') {
356 static int output_json(sd_journal *j, unsigned line, bool show_all) {
357 uint64_t realtime, monotonic;
367 r = sd_journal_get_realtime_usec(j, &realtime);
369 log_error("Failed to get realtime timestamp: %s", strerror(-r));
373 r = sd_journal_get_monotonic_usec(j, &monotonic, &boot_id);
375 log_error("Failed to get monotonic timestamp: %s", strerror(-r));
379 r = sd_journal_get_cursor(j, &cursor);
381 log_error("Failed to get cursor: %s", strerror(-r));
388 fputs(",\n", stdout);
391 "\t\".cursor\" : \"%s\",\n"
392 "\t\".realtime\" : %llu,\n"
393 "\t\".monotonic\" : %llu,\n"
394 "\t\".boot_id\" : \"%s\"",
396 (unsigned long long) realtime,
397 (unsigned long long) monotonic,
398 sd_id128_to_string(boot_id, sid));
402 SD_JOURNAL_FOREACH_DATA(j, data, length) {
405 c = memchr(data, '=', length);
407 log_error("Invalid field.");
411 fputs(",\n\t", stdout);
412 json_escape(data, c - (const char*) data);
413 fputs(" : ", stdout);
414 json_escape(c + 1, length - (c - (const char*) data) - 1);
417 fputs("\n}", stdout);
423 static int (*output_funcs[_OUTPUT_MODE_MAX])(sd_journal*j, unsigned line, bool show_all) = {
424 [OUTPUT_SHORT] = output_short,
425 [OUTPUT_VERBOSE] = output_verbose,
426 [OUTPUT_EXPORT] = output_export,
427 [OUTPUT_JSON] = output_json
430 int output_journal(sd_journal *j, OutputMode mode, unsigned line, bool show_all) {
432 assert(mode < _OUTPUT_MODE_MAX);
434 return output_funcs[mode](j, line, show_all);
437 int show_journal_by_unit(
452 bool need_seek = false;
455 assert(mode < _OUTPUT_MODE_MAX);
458 if (!endswith(unit, ".service") &&
459 !endswith(unit, ".socket") &&
460 !endswith(unit, ".mount") &&
461 !endswith(unit, ".swap"))
468 n_columns = columns();
473 if (asprintf(&m, "_SYSTEMD_UNIT=%s", unit) < 0) {
478 r = sd_journal_open(&j, SD_JOURNAL_LOCAL_ONLY|SD_JOURNAL_SYSTEM_ONLY);
482 fd = sd_journal_get_fd(j);
486 r = sd_journal_add_match(j, m, strlen(m));
490 r = sd_journal_seek_tail(j);
494 r = sd_journal_previous_skip(j, how_many);
498 if (mode == OUTPUT_JSON) {
508 r = sd_journal_next(j);
518 if (not_before > 0) {
519 r = sd_journal_get_monotonic_usec(j, &usec, NULL);
521 /* -ESTALE is returned if the
522 timestamp is not from this boot */
528 if (usec < not_before)
534 r = output_journal(j, mode, line, show_all);
542 r = fd_wait_for_event(fd, POLLIN);
546 r = sd_journal_process(j);
552 if (mode == OUTPUT_JSON)
553 fputs("\n]\n", stdout);
565 static const char *const output_mode_table[_OUTPUT_MODE_MAX] = {
566 [OUTPUT_SHORT] = "short",
567 [OUTPUT_VERBOSE] = "verbose",
568 [OUTPUT_EXPORT] = "export",
569 [OUTPUT_JSON] = "json"
572 DEFINE_STRING_TABLE_LOOKUP(output_mode, OutputMode);