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 Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
28 #include "logs-show.h"
33 #include "journal-internal.h"
35 /* up to three lines (each up to 100 characters),
36 or 300 characters, whichever is less */
37 #define PRINT_LINE_THRESHOLD 3
38 #define PRINT_CHAR_THRESHOLD 300
40 #define JSON_THRESHOLD 4096
42 static int print_catalog(FILE *f, sd_journal *j) {
44 _cleanup_free_ char *t = NULL, *z = NULL;
47 r = sd_journal_get_catalog(j, &t);
51 z = strreplace(strstrip(t), "\n", "\n-- ");
62 static int parse_field(const void *data, size_t length, const char *field, char **target, size_t *target_size) {
75 if (memcmp(data, field, fl))
83 memcpy(buf, (const char*) data + fl, nl);
93 static bool shall_print(const char *p, size_t l, OutputFlags flags) {
96 if (flags & OUTPUT_SHOW_ALL)
99 if (l >= PRINT_CHAR_THRESHOLD)
102 if (!utf8_is_printable(p, l))
108 static bool print_multiline(FILE *f, unsigned prefix, unsigned n_columns, OutputMode flags, int priority, const char* message, size_t message_len) {
109 const char *color_on = "", *color_off = "";
110 const char *pos, *end;
111 bool ellipsized = false;
114 if (flags & OUTPUT_COLOR) {
115 if (priority <= LOG_ERR) {
116 color_on = ANSI_HIGHLIGHT_RED_ON;
117 color_off = ANSI_HIGHLIGHT_OFF;
118 } else if (priority <= LOG_NOTICE) {
119 color_on = ANSI_HIGHLIGHT_ON;
120 color_off = ANSI_HIGHLIGHT_OFF;
125 pos < message + message_len;
126 pos = end + 1, line++) {
127 bool continuation = line > 0;
130 for (end = pos; end < message + message_len && *end != '\n'; end++)
135 /* We need to figure out when we are showing the last line, and
136 * will skip subsequent lines. In that case, we will put the dots
137 * at the end of the line, instead of putting dots in the middle
141 line + 1 == PRINT_LINE_THRESHOLD ||
142 end + 1 >= message + message_len;
144 if (flags & (OUTPUT_FULL_WIDTH | OUTPUT_SHOW_ALL) ||
145 (prefix + len + 1 < n_columns && !tail_line)) {
146 fprintf(f, "%*s%s%.*s%s\n",
147 continuation * prefix, "",
148 color_on, len, pos, color_off);
152 /* Beyond this point, ellipsization will happen. */
155 if (prefix < n_columns && n_columns - prefix >= 3) {
156 if (n_columns - prefix > (unsigned) len + 3)
157 fprintf(f, "%*s%s%.*s...%s\n",
158 continuation * prefix, "",
159 color_on, len, pos, color_off);
161 _cleanup_free_ char *e;
163 e = ellipsize_mem(pos, len, n_columns - prefix,
164 tail_line ? 100 : 90);
166 fprintf(f, "%*s%s%.*s%s\n",
167 continuation * prefix, "",
168 color_on, len, pos, color_off);
170 fprintf(f, "%*s%s%s%s\n",
171 continuation * prefix, "",
172 color_on, e, color_off);
184 static int output_short(
195 _cleanup_free_ char *hostname = NULL, *identifier = NULL, *comm = NULL, *pid = NULL, *fake_pid = NULL, *message = NULL, *realtime = NULL, *monotonic = NULL, *priority = NULL;
196 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, priority_len = 0;
198 bool ellipsized = false;
203 /* Set the threshold to one bigger than the actual print
204 * treshold, so that if the line is actually longer than what
205 * we're willing to print, ellipsization will occur. This way
206 * we won't output a misleading line without any indication of
209 sd_journal_set_data_threshold(j, flags & (OUTPUT_SHOW_ALL|OUTPUT_FULL_WIDTH) ? 0 : PRINT_CHAR_THRESHOLD + 1);
211 JOURNAL_FOREACH_DATA_RETVAL(j, data, length, r) {
213 r = parse_field(data, length, "PRIORITY=", &priority, &priority_len);
219 r = parse_field(data, length, "_HOSTNAME=", &hostname, &hostname_len);
225 r = parse_field(data, length, "SYSLOG_IDENTIFIER=", &identifier, &identifier_len);
231 r = parse_field(data, length, "_COMM=", &comm, &comm_len);
237 r = parse_field(data, length, "_PID=", &pid, &pid_len);
243 r = parse_field(data, length, "SYSLOG_PID=", &fake_pid, &fake_pid_len);
249 r = parse_field(data, length, "_SOURCE_REALTIME_TIMESTAMP=", &realtime, &realtime_len);
255 r = parse_field(data, length, "_SOURCE_MONOTONIC_TIMESTAMP=", &monotonic, &monotonic_len);
261 r = parse_field(data, length, "MESSAGE=", &message, &message_len);
272 if (!(flags & OUTPUT_SHOW_ALL))
273 strip_tab_ansi(&message, &message_len);
275 if (priority_len == 1 && *priority >= '0' && *priority <= '7')
278 if (mode == OUTPUT_SHORT_MONOTONIC) {
285 r = safe_atou64(monotonic, &t);
288 r = sd_journal_get_monotonic_usec(j, &t, &boot_id);
291 log_error("Failed to get monotonic timestamp: %s", strerror(-r));
295 fprintf(f, "[%5llu.%06llu]",
296 (unsigned long long) (t / USEC_PER_SEC),
297 (unsigned long long) (t % USEC_PER_SEC));
299 n += 1 + 5 + 1 + 6 + 1;
310 r = safe_atou64(realtime, &x);
313 r = sd_journal_get_realtime_usec(j, &x);
316 log_error("Failed to get realtime timestamp: %s", strerror(-r));
320 t = (time_t) (x / USEC_PER_SEC);
323 case OUTPUT_SHORT_ISO:
324 r = strftime(buf, sizeof(buf), "%Y-%m-%dT%H:%M:%S%z", localtime_r(&t, &tm));
326 case OUTPUT_SHORT_PRECISE:
327 r = strftime(buf, sizeof(buf), "%b %d %H:%M:%S", localtime_r(&t, &tm));
329 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
330 ".%06llu", x % USEC_PER_SEC);
334 r = strftime(buf, sizeof(buf), "%b %d %H:%M:%S", localtime_r(&t, &tm));
338 log_error("Failed to format time.");
346 if (hostname && shall_print(hostname, hostname_len, flags)) {
347 fprintf(f, " %.*s", (int) hostname_len, hostname);
348 n += hostname_len + 1;
351 if (identifier && shall_print(identifier, identifier_len, flags)) {
352 fprintf(f, " %.*s", (int) identifier_len, identifier);
353 n += identifier_len + 1;
354 } else if (comm && shall_print(comm, comm_len, flags)) {
355 fprintf(f, " %.*s", (int) comm_len, comm);
360 if (pid && shall_print(pid, pid_len, flags)) {
361 fprintf(f, "[%.*s]", (int) pid_len, pid);
363 } else if (fake_pid && shall_print(fake_pid, fake_pid_len, flags)) {
364 fprintf(f, "[%.*s]", (int) fake_pid_len, fake_pid);
365 n += fake_pid_len + 2;
368 if (!(flags & OUTPUT_SHOW_ALL) && !utf8_is_printable(message, message_len)) {
369 char bytes[FORMAT_BYTES_MAX];
370 fprintf(f, ": [%s blob data]\n", format_bytes(bytes, sizeof(bytes), message_len));
374 print_multiline(f, n + 2, n_columns, flags, p, message, message_len);
377 if (flags & OUTPUT_CATALOG)
383 static int output_verbose(
392 _cleanup_free_ char *cursor = NULL;
394 char ts[FORMAT_TIMESTAMP_MAX + 7];
400 sd_journal_set_data_threshold(j, 0);
402 r = sd_journal_get_realtime_usec(j, &realtime);
404 log_full(r == -EADDRNOTAVAIL ? LOG_DEBUG : LOG_ERR,
405 "Failed to get realtime timestamp: %s", strerror(-r));
409 r = sd_journal_get_cursor(j, &cursor);
411 log_error("Failed to get cursor: %s", strerror(-r));
415 fprintf(f, "%s [%s]\n",
416 format_timestamp_us(ts, sizeof(ts), realtime),
419 JOURNAL_FOREACH_DATA_RETVAL(j, data, length, r) {
422 const char *on = "", *off = "";
424 c = memchr(data, '=', length);
426 log_error("Invalid field.");
429 fieldlen = c - (const char*) data;
431 if (flags & OUTPUT_COLOR && startswith(data, "MESSAGE=")) {
432 on = ANSI_HIGHLIGHT_ON;
433 off = ANSI_HIGHLIGHT_OFF;
436 if (flags & OUTPUT_SHOW_ALL ||
437 (((length < PRINT_CHAR_THRESHOLD) || flags & OUTPUT_FULL_WIDTH)
438 && utf8_is_printable(data, length))) {
439 fprintf(f, " %s%.*s=", on, fieldlen, (const char*)data);
440 print_multiline(f, 4 + fieldlen + 1, 0, OUTPUT_FULL_WIDTH, 0, c + 1, length - fieldlen - 1);
443 char bytes[FORMAT_BYTES_MAX];
445 fprintf(f, " %s%.*s=[%s blob data]%s\n",
447 (int) (c - (const char*) data),
449 format_bytes(bytes, sizeof(bytes), length - (c - (const char *) data) - 1),
457 if (flags & OUTPUT_CATALOG)
463 static int output_export(
473 usec_t realtime, monotonic;
474 _cleanup_free_ char *cursor = NULL;
480 sd_journal_set_data_threshold(j, 0);
482 r = sd_journal_get_realtime_usec(j, &realtime);
484 log_error("Failed to get realtime timestamp: %s", strerror(-r));
488 r = sd_journal_get_monotonic_usec(j, &monotonic, &boot_id);
490 log_error("Failed to get monotonic timestamp: %s", strerror(-r));
494 r = sd_journal_get_cursor(j, &cursor);
496 log_error("Failed to get cursor: %s", strerror(-r));
502 "__REALTIME_TIMESTAMP=%llu\n"
503 "__MONOTONIC_TIMESTAMP=%llu\n"
506 (unsigned long long) realtime,
507 (unsigned long long) monotonic,
508 sd_id128_to_string(boot_id, sid));
510 JOURNAL_FOREACH_DATA_RETVAL(j, data, length, r) {
512 /* We already printed the boot id, from the data in
513 * the header, hence let's suppress it here */
515 hasprefix(data, "_BOOT_ID="))
518 if (!utf8_is_printable(data, length)) {
522 c = memchr(data, '=', length);
524 log_error("Invalid field.");
528 fwrite(data, c - (const char*) data, 1, f);
530 le64 = htole64(length - (c - (const char*) data) - 1);
531 fwrite(&le64, sizeof(le64), 1, f);
532 fwrite(c + 1, length - (c - (const char*) data) - 1, 1, f);
534 fwrite(data, length, 1, f);
556 if (!(flags & OUTPUT_SHOW_ALL) && l >= JSON_THRESHOLD)
560 else if (!utf8_is_printable(p, l)) {
561 bool not_first = false;
567 fprintf(f, ", %u", (uint8_t) *p);
570 fprintf(f, "%u", (uint8_t) *p);
582 if (*p == '"' || *p == '\\') {
585 } else if (*p == '\n')
588 fprintf(f, "\\u%04x", *p);
600 static int output_json(
607 uint64_t realtime, monotonic;
608 _cleanup_free_ char *cursor = NULL;
615 bool done, separator;
619 sd_journal_set_data_threshold(j, flags & OUTPUT_SHOW_ALL ? 0 : JSON_THRESHOLD);
621 r = sd_journal_get_realtime_usec(j, &realtime);
623 log_error("Failed to get realtime timestamp: %s", strerror(-r));
627 r = sd_journal_get_monotonic_usec(j, &monotonic, &boot_id);
629 log_error("Failed to get monotonic timestamp: %s", strerror(-r));
633 r = sd_journal_get_cursor(j, &cursor);
635 log_error("Failed to get cursor: %s", strerror(-r));
639 if (mode == OUTPUT_JSON_PRETTY)
642 "\t\"__CURSOR\" : \"%s\",\n"
643 "\t\"__REALTIME_TIMESTAMP\" : \"%llu\",\n"
644 "\t\"__MONOTONIC_TIMESTAMP\" : \"%llu\",\n"
645 "\t\"_BOOT_ID\" : \"%s\"",
647 (unsigned long long) realtime,
648 (unsigned long long) monotonic,
649 sd_id128_to_string(boot_id, sid));
651 if (mode == OUTPUT_JSON_SSE)
655 "{ \"__CURSOR\" : \"%s\", "
656 "\"__REALTIME_TIMESTAMP\" : \"%llu\", "
657 "\"__MONOTONIC_TIMESTAMP\" : \"%llu\", "
658 "\"_BOOT_ID\" : \"%s\"",
660 (unsigned long long) realtime,
661 (unsigned long long) monotonic,
662 sd_id128_to_string(boot_id, sid));
665 h = hashmap_new(string_hash_func, string_compare_func);
669 /* First round, iterate through the entry and count how often each field appears */
670 JOURNAL_FOREACH_DATA_RETVAL(j, data, length, r) {
676 memcmp(data, "_BOOT_ID=", 9) == 0)
679 eq = memchr(data, '=', length);
683 n = strndup(data, eq - (const char*) data);
689 u = PTR_TO_UINT(hashmap_get(h, n));
691 r = hashmap_put(h, n, UINT_TO_PTR(1));
697 r = hashmap_update(h, n, UINT_TO_PTR(u + 1));
711 SD_JOURNAL_FOREACH_DATA(j, data, length) {
717 /* We already printed the boot id, from the data in
718 * the header, hence let's suppress it here */
720 memcmp(data, "_BOOT_ID=", 9) == 0)
723 eq = memchr(data, '=', length);
728 if (mode == OUTPUT_JSON_PRETTY)
734 m = eq - (const char*) data;
736 n = strndup(data, m);
742 u = PTR_TO_UINT(hashmap_get2(h, n, (void**) &kk));
744 /* We already printed this, let's jump to the next */
750 /* Field only appears once, output it directly */
752 json_escape(f, data, m, flags);
755 json_escape(f, eq + 1, length - m - 1, flags);
757 hashmap_remove(h, n);
766 /* Field appears multiple times, output it as array */
767 json_escape(f, data, m, flags);
769 json_escape(f, eq + 1, length - m - 1, flags);
771 /* Iterate through the end of the list */
773 while (sd_journal_enumerate_data(j, &data, &length) > 0) {
777 if (memcmp(data, n, m) != 0)
780 if (((const char*) data)[m] != '=')
784 json_escape(f, (const char*) data + m + 1, length - m - 1, flags);
789 hashmap_remove(h, n);
793 /* Iterate data fields form the beginning */
803 if (mode == OUTPUT_JSON_PRETTY)
805 else if (mode == OUTPUT_JSON_SSE)
813 while ((k = hashmap_steal_first_key(h)))
821 static int output_cat(
835 sd_journal_set_data_threshold(j, 0);
837 r = sd_journal_get_data(j, "MESSAGE", &data, &l);
839 /* An entry without MESSAGE=? */
843 log_error("Failed to get data: %s", strerror(-r));
849 fwrite((const char*) data + 8, 1, l - 8, f);
855 static int (*output_funcs[_OUTPUT_MODE_MAX])(
860 OutputFlags flags) = {
862 [OUTPUT_SHORT] = output_short,
863 [OUTPUT_SHORT_ISO] = output_short,
864 [OUTPUT_SHORT_PRECISE] = output_short,
865 [OUTPUT_SHORT_MONOTONIC] = output_short,
866 [OUTPUT_VERBOSE] = output_verbose,
867 [OUTPUT_EXPORT] = output_export,
868 [OUTPUT_JSON] = output_json,
869 [OUTPUT_JSON_PRETTY] = output_json,
870 [OUTPUT_JSON_SSE] = output_json,
871 [OUTPUT_CAT] = output_cat
884 assert(mode < _OUTPUT_MODE_MAX);
887 n_columns = columns();
889 ret = output_funcs[mode](f, j, mode, n_columns, flags);
892 if (ellipsized && ret > 0)
898 static int show_journal(FILE *f,
909 bool need_seek = false;
910 int warn_cutoff = flags & OUTPUT_WARN_CUTOFF;
914 assert(mode < _OUTPUT_MODE_MAX);
917 r = sd_journal_seek_tail(j);
921 r = sd_journal_previous_skip(j, how_many);
930 r = sd_journal_next(j);
940 if (not_before > 0) {
941 r = sd_journal_get_monotonic_usec(j, &usec, NULL);
943 /* -ESTALE is returned if the
944 timestamp is not from this boot */
950 if (usec < not_before)
956 r = output_journal(f, j, mode, n_columns, flags, ellipsized);
961 if (warn_cutoff && line < how_many && not_before > 0) {
965 /* Check whether the cutoff line is too early */
967 r = sd_id128_get_boot(&boot_id);
971 r = sd_journal_get_cutoff_monotonic_usec(j, boot_id, &cutoff, NULL);
975 if (r > 0 && not_before < cutoff)
976 fprintf(f, "Warning: Journal has been rotated since unit was started. Log output is incomplete or unavailable.\n");
981 if (!(flags & OUTPUT_FOLLOW))
984 r = sd_journal_wait(j, (usec_t) -1);
994 int add_matches_for_unit(sd_journal *j, const char *unit) {
996 char *m1, *m2, *m3, *m4;
1001 m1 = strappenda("_SYSTEMD_UNIT=", unit);
1002 m2 = strappenda("COREDUMP_UNIT=", unit);
1003 m3 = strappenda("UNIT=", unit);
1004 m4 = strappenda("OBJECT_SYSTEMD_UNIT=", unit);
1007 /* Look for messages from the service itself */
1008 (r = sd_journal_add_match(j, m1, 0)) ||
1010 /* Look for coredumps of the service */
1011 (r = sd_journal_add_disjunction(j)) ||
1012 (r = sd_journal_add_match(j, "MESSAGE_ID=fc2e22bc6ee647b6b90729ab34a250b1", 0)) ||
1013 (r = sd_journal_add_match(j, "_UID=0", 0)) ||
1014 (r = sd_journal_add_match(j, m2, 0)) ||
1016 /* Look for messages from PID 1 about this service */
1017 (r = sd_journal_add_disjunction(j)) ||
1018 (r = sd_journal_add_match(j, "_PID=1", 0)) ||
1019 (r = sd_journal_add_match(j, m3, 0)) ||
1021 /* Look for messages from authorized daemons about this service */
1022 (r = sd_journal_add_disjunction(j)) ||
1023 (r = sd_journal_add_match(j, "_UID=0", 0)) ||
1024 (r = sd_journal_add_match(j, m4, 0))
1030 int add_matches_for_user_unit(sd_journal *j, const char *unit, uid_t uid) {
1032 char *m1, *m2, *m3, *m4;
1033 char muid[sizeof("_UID=") + DECIMAL_STR_MAX(uid_t)];
1038 m1 = strappenda("_SYSTEMD_USER_UNIT=", unit);
1039 m2 = strappenda("USER_UNIT=", unit);
1040 m3 = strappenda("COREDUMP_USER_UNIT=", unit);
1041 m4 = strappenda("OBJECT_SYSTEMD_USER_UNIT=", unit);
1042 sprintf(muid, "_UID=%lu", (unsigned long) uid);
1045 /* Look for messages from the user service itself */
1046 (r = sd_journal_add_match(j, m1, 0)) ||
1047 (r = sd_journal_add_match(j, muid, 0)) ||
1049 /* Look for messages from systemd about this service */
1050 (r = sd_journal_add_disjunction(j)) ||
1051 (r = sd_journal_add_match(j, m2, 0)) ||
1052 (r = sd_journal_add_match(j, muid, 0)) ||
1054 /* Look for coredumps of the service */
1055 (r = sd_journal_add_disjunction(j)) ||
1056 (r = sd_journal_add_match(j, m3, 0)) ||
1057 (r = sd_journal_add_match(j, muid, 0)) ||
1058 (r = sd_journal_add_match(j, "_UID=0", 0)) ||
1060 /* Look for messages from authorized daemons about this service */
1061 (r = sd_journal_add_disjunction(j)) ||
1062 (r = sd_journal_add_match(j, m4, 0)) ||
1063 (r = sd_journal_add_match(j, muid, 0)) ||
1064 (r = sd_journal_add_match(j, "_UID=0", 0))
1069 int add_match_this_boot(sd_journal *j) {
1070 char match[9+32+1] = "_BOOT_ID=";
1076 r = sd_id128_get_boot(&boot_id);
1078 log_error("Failed to get boot id: %s", strerror(-r));
1082 sd_id128_to_string(boot_id, match + 9);
1083 r = sd_journal_add_match(j, match, strlen(match));
1085 log_error("Failed to add match: %s", strerror(-r));
1089 r = sd_journal_add_conjunction(j);
1096 int show_journal_by_unit(
1108 _cleanup_journal_close_ sd_journal*j = NULL;
1110 int jflags = SD_JOURNAL_LOCAL_ONLY | system * SD_JOURNAL_SYSTEM;
1113 assert(mode < _OUTPUT_MODE_MAX);
1119 r = sd_journal_open(&j, jflags);
1123 r = add_match_this_boot(j);
1128 r = add_matches_for_unit(j, unit);
1130 r = add_matches_for_user_unit(j, unit, uid);
1134 if (_unlikely_(log_get_max_level() >= LOG_PRI(LOG_DEBUG))) {
1135 _cleanup_free_ char *filter;
1137 filter = journal_make_match_string(j);
1138 log_debug("Journal filter: %s", filter);
1141 return show_journal(f, j, mode, n_columns, not_before, how_many, flags, ellipsized);
1144 static const char *const output_mode_table[_OUTPUT_MODE_MAX] = {
1145 [OUTPUT_SHORT] = "short",
1146 [OUTPUT_SHORT_ISO] = "short-iso",
1147 [OUTPUT_SHORT_PRECISE] = "short-precise",
1148 [OUTPUT_SHORT_MONOTONIC] = "short-monotonic",
1149 [OUTPUT_VERBOSE] = "verbose",
1150 [OUTPUT_EXPORT] = "export",
1151 [OUTPUT_JSON] = "json",
1152 [OUTPUT_JSON_PRETTY] = "json-pretty",
1153 [OUTPUT_JSON_SSE] = "json-sse",
1154 [OUTPUT_CAT] = "cat"
1157 DEFINE_STRING_TABLE_LOOKUP(output_mode, OutputMode);