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))
62 memcpy(buf, (const char*) data + fl, nl);
65 log_error("Out of memory");
76 static bool shall_print(bool show_all, char *p, size_t l) {
80 if (l > PRINT_THRESHOLD)
83 if (contains_unprintable(p, l))
89 static int output_short(sd_journal *j, unsigned line, bool show_all, bool monotonic_mode) {
94 char *hostname = NULL, *identifier = NULL, *comm = NULL, *pid = NULL, *fake_pid = NULL, *message = NULL, *realtime = NULL, *monotonic = NULL;
95 size_t hostname_len = 0, identifier_len = 0, comm_len = 0, pid_len = 0, fake_pid_len = 0, message_len = 0, realtime_len = 0, monotonic_len = 0;
99 SD_JOURNAL_FOREACH_DATA(j, data, length) {
101 r = parse_field(data, length, "_HOSTNAME=", &hostname, &hostname_len);
107 r = parse_field(data, length, "SYSLOG_IDENTIFIER=", &identifier, &identifier_len);
113 r = parse_field(data, length, "_COMM=", &comm, &comm_len);
119 r = parse_field(data, length, "_PID=", &pid, &pid_len);
125 r = parse_field(data, length, "SYSLOG_PID=", &fake_pid, &fake_pid_len);
131 r = parse_field(data, length, "_SOURCE_REALTIME_TIMESTAMP=", &realtime, &realtime_len);
137 r = parse_field(data, length, "_SOURCE_MONOTONIC_TIMESTAMP=", &monotonic, &monotonic_len);
143 r = parse_field(data, length, "MESSAGE=", &message, &message_len);
153 if (monotonic_mode) {
158 r = safe_atou64(monotonic, &t);
161 r = sd_journal_get_monotonic_usec(j, &t, NULL);
164 printf("[%5llu.%06llu]",
165 (unsigned long long) (t / USEC_PER_SEC),
166 (unsigned long long) (t % USEC_PER_SEC));
168 n += 1 + 5 + 1 + 6 + 1;
179 r = safe_atou64(realtime, &x);
182 r = sd_journal_get_realtime_usec(j, &x);
185 log_error("Failed to get realtime: %s", strerror(-r));
189 t = (time_t) (x / USEC_PER_SEC);
190 if (strftime(buf, sizeof(buf), "%b %d %H:%M:%S", localtime_r(&t, &tm)) <= 0) {
191 log_error("Failed to format time.");
199 if (hostname && shall_print(show_all, hostname, hostname_len)) {
200 printf(" %.*s", (int) hostname_len, hostname);
201 n += hostname_len + 1;
204 if (identifier && shall_print(show_all, identifier, identifier_len)) {
205 printf(" %.*s", (int) identifier_len, identifier);
206 n += identifier_len + 1;
207 } else if (comm && shall_print(show_all, comm, comm_len)) {
208 printf(" %.*s", (int) comm_len, comm);
212 if (pid && shall_print(show_all, pid, pid_len)) {
213 printf("[%.*s]", (int) pid_len, pid);
215 } else if (fake_pid && shall_print(show_all, fake_pid, fake_pid_len)) {
216 printf("[%.*s]", (int) fake_pid_len, fake_pid);
217 n += fake_pid_len + 2;
221 printf(": %.*s\n", (int) message_len, message);
222 else if (contains_unprintable(message, message_len))
223 fputs(": [blob data]\n", stdout);
224 else if (message_len + n < columns())
225 printf(": %.*s\n", (int) message_len, message);
226 else if (n < columns()) {
229 e = ellipsize_mem(message, message_len, columns() - n - 2, 90);
232 printf(": %.*s\n", (int) message_len, message);
255 static int output_short_realtime(sd_journal *j, unsigned line, bool show_all) {
256 return output_short(j, line, show_all, false);
259 static int output_short_monotonic(sd_journal *j, unsigned line, bool show_all) {
260 return output_short(j, line, show_all, true);
263 static int output_verbose(sd_journal *j, unsigned line, bool show_all) {
268 char ts[FORMAT_TIMESTAMP_MAX];
273 r = sd_journal_get_realtime_usec(j, &realtime);
275 log_error("Failed to get realtime timestamp: %s", strerror(-r));
279 r = sd_journal_get_cursor(j, &cursor);
281 log_error("Failed to get cursor: %s", strerror(-r));
286 format_timestamp(ts, sizeof(ts), realtime),
291 SD_JOURNAL_FOREACH_DATA(j, data, length) {
292 if (!show_all && (length > PRINT_THRESHOLD ||
293 contains_unprintable(data, length))) {
296 c = memchr(data, '=', length);
298 log_error("Invalid field.");
302 printf("\t%.*s=[blob data]\n",
303 (int) (c - (const char*) data),
306 printf("\t%.*s\n", (int) length, (const char*) data);
312 static int output_export(sd_journal *j, unsigned line, bool show_all) {
316 usec_t realtime, monotonic;
323 r = sd_journal_get_realtime_usec(j, &realtime);
325 log_error("Failed to get realtime timestamp: %s", strerror(-r));
329 r = sd_journal_get_monotonic_usec(j, &monotonic, &boot_id);
331 log_error("Failed to get monotonic timestamp: %s", strerror(-r));
335 r = sd_journal_get_cursor(j, &cursor);
337 log_error("Failed to get cursor: %s", strerror(-r));
341 printf(".cursor=%s\n"
346 (unsigned long long) realtime,
347 (unsigned long long) monotonic,
348 sd_id128_to_string(boot_id, sid));
352 SD_JOURNAL_FOREACH_DATA(j, data, length) {
354 if (contains_unprintable(data, length)) {
358 c = memchr(data, '=', length);
360 log_error("Invalid field.");
364 fwrite(data, c - (const char*) data, 1, stdout);
366 le64 = htole64(length - (c - (const char*) data) - 1);
367 fwrite(&le64, sizeof(le64), 1, stdout);
368 fwrite(c + 1, length - (c - (const char*) data) - 1, 1, stdout);
370 fwrite(data, length, 1, stdout);
380 static void json_escape(const char* p, size_t l) {
382 if (contains_unprintable(p, l)) {
383 bool not_first = false;
389 printf(", %u", (uint8_t) *p);
392 printf("%u", (uint8_t) *p);
404 if (*p == '"' || *p == '\\') {
418 static int output_json(sd_journal *j, unsigned line, bool show_all) {
419 uint64_t realtime, monotonic;
429 r = sd_journal_get_realtime_usec(j, &realtime);
431 log_error("Failed to get realtime timestamp: %s", strerror(-r));
435 r = sd_journal_get_monotonic_usec(j, &monotonic, &boot_id);
437 log_error("Failed to get monotonic timestamp: %s", strerror(-r));
441 r = sd_journal_get_cursor(j, &cursor);
443 log_error("Failed to get cursor: %s", strerror(-r));
450 fputs(",\n", stdout);
453 "\t\".cursor\" : \"%s\",\n"
454 "\t\".realtime\" : %llu,\n"
455 "\t\".monotonic\" : %llu,\n"
456 "\t\".boot_id\" : \"%s\"",
458 (unsigned long long) realtime,
459 (unsigned long long) monotonic,
460 sd_id128_to_string(boot_id, sid));
464 SD_JOURNAL_FOREACH_DATA(j, data, length) {
467 c = memchr(data, '=', length);
469 log_error("Invalid field.");
473 fputs(",\n\t", stdout);
474 json_escape(data, c - (const char*) data);
475 fputs(" : ", stdout);
476 json_escape(c + 1, length - (c - (const char*) data) - 1);
479 fputs("\n}", stdout);
485 static int (*output_funcs[_OUTPUT_MODE_MAX])(sd_journal*j, unsigned line, bool show_all) = {
486 [OUTPUT_SHORT] = output_short_realtime,
487 [OUTPUT_SHORT_MONOTONIC] = output_short_monotonic,
488 [OUTPUT_VERBOSE] = output_verbose,
489 [OUTPUT_EXPORT] = output_export,
490 [OUTPUT_JSON] = output_json
493 int output_journal(sd_journal *j, OutputMode mode, unsigned line, bool show_all) {
495 assert(mode < _OUTPUT_MODE_MAX);
497 return output_funcs[mode](j, line, show_all);
500 int show_journal_by_unit(
515 bool need_seek = false;
518 assert(mode < _OUTPUT_MODE_MAX);
521 if (!endswith(unit, ".service") &&
522 !endswith(unit, ".socket") &&
523 !endswith(unit, ".mount") &&
524 !endswith(unit, ".swap"))
531 n_columns = columns();
536 if (asprintf(&m, "_SYSTEMD_UNIT=%s", unit) < 0) {
541 r = sd_journal_open(&j, SD_JOURNAL_LOCAL_ONLY|SD_JOURNAL_SYSTEM_ONLY);
545 fd = sd_journal_get_fd(j);
549 r = sd_journal_add_match(j, m, strlen(m));
553 r = sd_journal_seek_tail(j);
557 r = sd_journal_previous_skip(j, how_many);
561 if (mode == OUTPUT_JSON) {
571 r = sd_journal_next(j);
581 if (not_before > 0) {
582 r = sd_journal_get_monotonic_usec(j, &usec, NULL);
584 /* -ESTALE is returned if the
585 timestamp is not from this boot */
591 if (usec < not_before)
597 r = output_journal(j, mode, line, show_all);
605 r = fd_wait_for_event(fd, POLLIN);
609 r = sd_journal_process(j);
615 if (mode == OUTPUT_JSON)
616 fputs("\n]\n", stdout);
628 static const char *const output_mode_table[_OUTPUT_MODE_MAX] = {
629 [OUTPUT_SHORT] = "short",
630 [OUTPUT_SHORT_MONOTONIC] = "short-monotonic",
631 [OUTPUT_VERBOSE] = "verbose",
632 [OUTPUT_EXPORT] = "export",
633 [OUTPUT_JSON] = "json"
636 DEFINE_STRING_TABLE_LOOKUP(output_mode, OutputMode);