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");
74 static bool shall_print(bool show_all, char *p, size_t l) {
78 if (l > PRINT_THRESHOLD)
81 if (contains_unprintable(p, l))
87 static int output_short(sd_journal *j, unsigned line, bool show_all) {
96 char *hostname = NULL, *identifier = NULL, *comm = NULL, *pid = NULL, *fake_pid = NULL, *message = NULL;
97 size_t hostname_len = 0, identifier_len = 0, comm_len = 0, pid_len = 0, fake_pid_len = 0, message_len = 0;
101 SD_JOURNAL_FOREACH_DATA(j, data, length) {
103 r = parse_field(data, length, "_HOSTNAME=", &hostname, &hostname_len);
109 r = parse_field(data, length, "SYSLOG_IDENTIFIER=", &identifier, &identifier_len);
115 r = parse_field(data, length, "_COMM=", &comm, &comm_len);
121 r = parse_field(data, length, "_PID=", &pid, &pid_len);
127 r = parse_field(data, length, "SYSLOG_PID=", &fake_pid, &fake_pid_len);
133 r = parse_field(data, length, "MESSAGE=", &message, &message_len);
143 r = sd_journal_get_realtime_usec(j, &realtime);
145 log_error("Failed to get realtime: %s", strerror(-r));
149 t = (time_t) (realtime / USEC_PER_SEC);
150 if (strftime(buf, sizeof(buf), "%b %d %H:%M:%S", localtime_r(&t, &tm)) <= 0) {
151 log_error("Failed to format time.");
158 if (hostname && shall_print(show_all, hostname, hostname_len)) {
159 printf(" %.*s", (int) hostname_len, hostname);
160 n += hostname_len + 1;
163 if (identifier && shall_print(show_all, identifier, identifier_len)) {
164 printf(" %.*s", (int) identifier_len, identifier);
165 n += identifier_len + 1;
166 } else if (comm && shall_print(show_all, comm, comm_len)) {
167 printf(" %.*s", (int) comm_len, comm);
171 if (pid && shall_print(show_all, pid, pid_len)) {
172 printf("[%.*s]", (int) pid_len, pid);
174 } else if (fake_pid && shall_print(show_all, fake_pid, fake_pid_len)) {
175 printf("[%.*s]", (int) fake_pid_len, fake_pid);
176 n += fake_pid_len + 2;
180 printf(": %.*s\n", (int) message_len, message);
181 else if (contains_unprintable(message, message_len))
182 fputs(": [blob data]\n", stdout);
183 else if (message_len + n < columns())
184 printf(": %.*s\n", (int) message_len, message);
185 else if (n < columns()) {
188 e = ellipsize_mem(message, message_len, columns() - n - 2, 90);
191 printf(": %.*s\n", (int) message_len, message);
212 static int output_verbose(sd_journal *j, unsigned line, bool show_all) {
217 char ts[FORMAT_TIMESTAMP_MAX];
222 r = sd_journal_get_realtime_usec(j, &realtime);
224 log_error("Failed to get realtime timestamp: %s", strerror(-r));
228 r = sd_journal_get_cursor(j, &cursor);
230 log_error("Failed to get cursor: %s", strerror(-r));
235 format_timestamp(ts, sizeof(ts), realtime),
240 SD_JOURNAL_FOREACH_DATA(j, data, length) {
241 if (!show_all && (length > PRINT_THRESHOLD ||
242 contains_unprintable(data, length))) {
245 c = memchr(data, '=', length);
247 log_error("Invalid field.");
251 printf("\t%.*s=[blob data]\n",
252 (int) (c - (const char*) data),
255 printf("\t%.*s\n", (int) length, (const char*) data);
261 static int output_export(sd_journal *j, unsigned line, bool show_all) {
265 usec_t realtime, monotonic;
272 r = sd_journal_get_realtime_usec(j, &realtime);
274 log_error("Failed to get realtime timestamp: %s", strerror(-r));
278 r = sd_journal_get_monotonic_usec(j, &monotonic, &boot_id);
280 log_error("Failed to get monotonic timestamp: %s", strerror(-r));
284 r = sd_journal_get_cursor(j, &cursor);
286 log_error("Failed to get cursor: %s", strerror(-r));
290 printf(".cursor=%s\n"
295 (unsigned long long) realtime,
296 (unsigned long long) monotonic,
297 sd_id128_to_string(boot_id, sid));
301 SD_JOURNAL_FOREACH_DATA(j, data, length) {
303 if (contains_unprintable(data, length)) {
307 c = memchr(data, '=', length);
309 log_error("Invalid field.");
313 fwrite(data, c - (const char*) data, 1, stdout);
315 le64 = htole64(length - (c - (const char*) data) - 1);
316 fwrite(&le64, sizeof(le64), 1, stdout);
317 fwrite(c + 1, length - (c - (const char*) data) - 1, 1, stdout);
319 fwrite(data, length, 1, stdout);
329 static void json_escape(const char* p, size_t l) {
331 if (contains_unprintable(p, l)) {
332 bool not_first = false;
338 printf(", %u", (uint8_t) *p);
341 printf("%u", (uint8_t) *p);
353 if (*p == '"' || *p == '\\') {
367 static int output_json(sd_journal *j, unsigned line, bool show_all) {
368 uint64_t realtime, monotonic;
378 r = sd_journal_get_realtime_usec(j, &realtime);
380 log_error("Failed to get realtime timestamp: %s", strerror(-r));
384 r = sd_journal_get_monotonic_usec(j, &monotonic, &boot_id);
386 log_error("Failed to get monotonic timestamp: %s", strerror(-r));
390 r = sd_journal_get_cursor(j, &cursor);
392 log_error("Failed to get cursor: %s", strerror(-r));
399 fputs(",\n", stdout);
402 "\t\".cursor\" : \"%s\",\n"
403 "\t\".realtime\" : %llu,\n"
404 "\t\".monotonic\" : %llu,\n"
405 "\t\".boot_id\" : \"%s\"",
407 (unsigned long long) realtime,
408 (unsigned long long) monotonic,
409 sd_id128_to_string(boot_id, sid));
413 SD_JOURNAL_FOREACH_DATA(j, data, length) {
416 c = memchr(data, '=', length);
418 log_error("Invalid field.");
422 fputs(",\n\t", stdout);
423 json_escape(data, c - (const char*) data);
424 fputs(" : ", stdout);
425 json_escape(c + 1, length - (c - (const char*) data) - 1);
428 fputs("\n}", stdout);
434 static int (*output_funcs[_OUTPUT_MODE_MAX])(sd_journal*j, unsigned line, bool show_all) = {
435 [OUTPUT_SHORT] = output_short,
436 [OUTPUT_VERBOSE] = output_verbose,
437 [OUTPUT_EXPORT] = output_export,
438 [OUTPUT_JSON] = output_json
441 int output_journal(sd_journal *j, OutputMode mode, unsigned line, bool show_all) {
443 assert(mode < _OUTPUT_MODE_MAX);
445 return output_funcs[mode](j, line, show_all);
448 int show_journal_by_unit(
463 bool need_seek = false;
466 assert(mode < _OUTPUT_MODE_MAX);
469 if (!endswith(unit, ".service") &&
470 !endswith(unit, ".socket") &&
471 !endswith(unit, ".mount") &&
472 !endswith(unit, ".swap"))
479 n_columns = columns();
484 if (asprintf(&m, "_SYSTEMD_UNIT=%s", unit) < 0) {
489 r = sd_journal_open(&j, SD_JOURNAL_LOCAL_ONLY|SD_JOURNAL_SYSTEM_ONLY);
493 fd = sd_journal_get_fd(j);
497 r = sd_journal_add_match(j, m, strlen(m));
501 r = sd_journal_seek_tail(j);
505 r = sd_journal_previous_skip(j, how_many);
509 if (mode == OUTPUT_JSON) {
519 r = sd_journal_next(j);
529 if (not_before > 0) {
530 r = sd_journal_get_monotonic_usec(j, &usec, NULL);
532 /* -ESTALE is returned if the
533 timestamp is not from this boot */
539 if (usec < not_before)
545 r = output_journal(j, mode, line, show_all);
553 r = fd_wait_for_event(fd, POLLIN);
557 r = sd_journal_process(j);
563 if (mode == OUTPUT_JSON)
564 fputs("\n]\n", stdout);
576 static const char *const output_mode_table[_OUTPUT_MODE_MAX] = {
577 [OUTPUT_SHORT] = "short",
578 [OUTPUT_VERBOSE] = "verbose",
579 [OUTPUT_EXPORT] = "export",
580 [OUTPUT_JSON] = "json"
583 DEFINE_STRING_TABLE_LOOKUP(output_mode, OutputMode);