From: Lennart Poettering Date: Thu, 19 Jan 2023 21:42:03 +0000 (+0100) Subject: logs-show: show seqnum info in export+json output mode X-Git-Tag: v254-rc1~1277^2~4 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=2bc70e2e9db13cfbc74e4bf1fd28ecbfef1466c9;p=thirdparty%2Fsystemd.git logs-show: show seqnum info in export+json output mode --- diff --git a/src/shared/journal-importer.c b/src/shared/journal-importer.c index d9eabec886f..83e9834bbf2 100644 --- a/src/shared/journal-importer.c +++ b/src/shared/journal-importer.c @@ -14,6 +14,7 @@ #include "journal-util.h" #include "parse-util.h" #include "string-util.h" +#include "strv.h" #include "unaligned.h" enum { @@ -217,9 +218,8 @@ static int process_special_field(JournalImporter *imp, char *line) { assert(line); - value = startswith(line, "__CURSOR="); - if (value) - /* ignore __CURSOR */ + if (STARTSWITH_SET(line, "__CURSOR=", "__SEQNUM=", "__SEQNUM_ID=")) + /* ignore __CURSOR=, __SEQNUM=, __SEQNUM_ID= which we cannot replicate */ return 1; value = startswith(line, "__REALTIME_TIMESTAMP="); diff --git a/src/shared/logs-show.c b/src/shared/logs-show.c index b72e516c8da..6ed9a6890f5 100644 --- a/src/shared/logs-show.c +++ b/src/shared/logs-show.c @@ -791,11 +791,12 @@ static int output_export( const dual_timestamp *previous_display_ts, const sd_id128_t *previous_boot_id) { + sd_id128_t journal_boot_id, seqnum_id; _cleanup_free_ char *cursor = NULL; + usec_t monotonic, realtime; const void *data; + uint64_t seqnum; size_t length; - usec_t monotonic, realtime; - sd_id128_t journal_boot_id; int r; assert(j); @@ -818,14 +819,22 @@ static int output_export( if (r < 0) return log_error_errno(r, "Failed to get monotonic timestamp: %m"); + r = sd_journal_get_seqnum(j, &seqnum, &seqnum_id); + if (r < 0) + return log_error_errno(r, "Failed to get seqnum: %m"); + fprintf(f, "__CURSOR=%s\n" - "__REALTIME_TIMESTAMP="USEC_FMT"\n" - "__MONOTONIC_TIMESTAMP="USEC_FMT"\n" + "__REALTIME_TIMESTAMP=" USEC_FMT "\n" + "__MONOTONIC_TIMESTAMP=" USEC_FMT "\n" + "__SEQNUM=%" PRIu64 "\n" + "__SEQNUM_ID=%s\n" "_BOOT_ID=%s\n", cursor, realtime, monotonic, + seqnum, + SD_ID128_TO_STRING(seqnum_id), SD_ID128_TO_STRING(journal_boot_id)); JOURNAL_FOREACH_DATA_RETVAL(j, data, length, r) { @@ -1040,15 +1049,16 @@ static int output_json( const dual_timestamp *previous_display_ts, const sd_id128_t *previous_boot_id) { - char usecbuf[DECIMAL_STR_MAX(usec_t)]; + char usecbuf[CONST_MAX(DECIMAL_STR_MAX(usec_t), DECIMAL_STR_MAX(uint64_t))]; _cleanup_(json_variant_unrefp) JsonVariant *object = NULL; + sd_id128_t journal_boot_id, seqnum_id; _cleanup_free_ char *cursor = NULL; + usec_t realtime, monotonic; JsonVariant **array = NULL; struct json_data *d; Hashmap *h = NULL; + uint64_t seqnum; size_t n = 0; - usec_t realtime, monotonic; - sd_id128_t journal_boot_id; int r; assert(j); @@ -1071,6 +1081,10 @@ static int output_json( if (r < 0) return log_error_errno(r, "Failed to get monotonic timestamp: %m"); + r = sd_journal_get_seqnum(j, &seqnum, &seqnum_id); + if (r < 0) + return log_error_errno(r, "Failed to get seqnum: %m"); + h = hashmap_new(&string_hash_ops); if (!h) return log_oom(); @@ -1093,6 +1107,15 @@ static int output_json( if (r < 0) goto finish; + xsprintf(usecbuf, USEC_FMT, seqnum); + r = update_json_data(h, flags, "__SEQNUM", usecbuf, SIZE_MAX); + if (r < 0) + goto finish; + + r = update_json_data(h, flags, "__SEQNUM_ID", SD_ID128_TO_STRING(seqnum_id), SIZE_MAX); + if (r < 0) + goto finish; + for (;;) { const void *data; size_t size; diff --git a/test/units/testsuite-04.sh b/test/units/testsuite-04.sh index d10a9afbf7c..5d27df91fbe 100755 --- a/test/units/testsuite-04.sh +++ b/test/units/testsuite-04.sh @@ -70,7 +70,7 @@ ID=$(journalctl --new-id128 | sed -n 2p) printf $'foo' | systemd-cat -t "$ID" --level-prefix false journalctl --sync journalctl -b -o export --output-fields=MESSAGE,FOO --output-fields=PRIORITY,MESSAGE -t "$ID" >/output -[[ $(grep -c . /output) -eq 6 ]] +[[ $(grep -c . /output) -eq 8 ]] grep -q '^__CURSOR=' /output grep -q '^MESSAGE=foo$' /output grep -q '^PRIORITY=6$' /output