diff --git a/src/journal/journalctl.c b/src/journal/journalctl.c index 3c4a7c0a7a..4e3d17678e 100644 --- a/src/journal/journalctl.c +++ b/src/journal/journalctl.c @@ -33,6 +33,7 @@ #include "dissect-image.h" #include "fd-util.h" #include "fileio.h" +#include "format-table.h" #include "format-util.h" #include "fs-util.h" #include "fsprg.h" @@ -84,6 +85,7 @@ enum { }; static OutputMode arg_output = OUTPUT_SHORT; +static JsonFormatFlags arg_json_format_flags = JSON_FORMAT_OFF; static bool arg_utc = false; static bool arg_follow = false; static bool arg_full = true; @@ -577,6 +579,8 @@ static int parse_argv(int argc, char *argv[]) { if (IN_SET(arg_output, OUTPUT_EXPORT, OUTPUT_JSON, OUTPUT_JSON_PRETTY, OUTPUT_JSON_SSE, OUTPUT_JSON_SEQ, OUTPUT_CAT)) arg_quiet = true; + if (OUTPUT_MODE_IS_JSON(arg_output)) + arg_json_format_flags = output_mode_to_json_format_flags(arg_output) | JSON_FORMAT_COLOR_AUTO; break; case 'l': @@ -1441,8 +1445,9 @@ finish: } static int list_boots(sd_journal *j) { - int w, i, count; + _cleanup_(table_unrefp) Table *table = NULL; BootId *id, *all_ids; + int count, i, r; assert(j); @@ -1452,23 +1457,30 @@ static int list_boots(sd_journal *j) { if (count == 0) return count; - pager_open(arg_pager_flags); + table = table_new(OUTPUT_MODE_IS_JSON(arg_output) ? "index" : "idx", "boot id", "first entry", "last entry"); + if (!table) + return log_oom(); - /* numbers are one less, but we need an extra char for the sign */ - w = DECIMAL_STR_WIDTH(count - 1) + 1; + if (arg_full) + table_set_width(table, 0); i = 0; LIST_FOREACH(boot_list, id, all_ids) { - char a[FORMAT_TIMESTAMP_MAX], b[FORMAT_TIMESTAMP_MAX]; - - printf("% *i " SD_ID128_FORMAT_STR " %s—%s\n", - w, i - count + 1, - SD_ID128_FORMAT_VAL(id->id), - format_timestamp_maybe_utc(a, sizeof(a), id->first), - format_timestamp_maybe_utc(b, sizeof(b), id->last)); + r = table_add_many(table, + TABLE_INT, i - count + 1, + TABLE_SET_ALIGN_PERCENT, 100, + TABLE_ID128, id->id, + TABLE_TIMESTAMP, id->first, + TABLE_TIMESTAMP, id->last); + if (r < 0) + return table_log_add_error(r); i++; } + r = table_print_with_pager(table, arg_json_format_flags, arg_pager_flags, !arg_quiet); + if (r < 0) + return table_log_print_error(r); + boot_id_free_all(all_ids); return 0;