]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
logs-show: show seqnum info in export+json output mode
authorLennart Poettering <lennart@poettering.net>
Thu, 19 Jan 2023 21:42:03 +0000 (22:42 +0100)
committerLennart Poettering <lennart@poettering.net>
Wed, 8 Feb 2023 12:42:29 +0000 (13:42 +0100)
src/shared/journal-importer.c
src/shared/logs-show.c
test/units/testsuite-04.sh

index d9eabec886fde020635827e0e977cee6897f91b2..83e9834bbf2fb028e40e8bb14522ae5e519f2315 100644 (file)
@@ -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=");
index b72e516c8da04c45044ad855769802738f9c3ba0..6ed9a6890f5e73cba0bfc1a2f01f15350147198e 100644 (file)
@@ -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;
index d10a9afbf7cb992d4cf2df4489268ce40cee870d..5d27df91fbe079732f2bcb64cce154b34ef00a47 100755 (executable)
@@ -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