]> git.gsnw.org Git - fping.git/commitdiff
Add JSON support for --timestamp-format
authorGerman Service Network <support@gsnw.de>
Wed, 30 Apr 2025 17:19:25 +0000 (19:19 +0200)
committerSebastian <176771227+gsnw-sebast@users.noreply.github.com>
Sat, 12 Jul 2025 05:43:18 +0000 (07:43 +0200)
ci/test-16-json-output.pl
src/fping.c

index 1fd21913ad0beef0a6842fdc4793737bc0192f11..84fa3a187da2accbb8482e5ba8eba2e49f87fb29 100644 (file)
@@ -1,6 +1,6 @@
 #!/usr/bin/perl -w
 
-use Test::Command tests => 54;
+use Test::Command tests => 63;
 use Test::More;
 
 # fping -J -c 2 127.0.0.1
@@ -65,8 +65,35 @@ $cmd->stderr_is_eq("");
 {
 my $cmd = Test::Command->new(cmd => "fping -J -c 2 -D 127.0.0.1");
 $cmd->exit_is_num(0);
-$cmd->stdout_like(qr/^\{"resp":\s\{"timestamp":\s\d+.\d+,\s"host":\s"127\.0\.0\.1",\s"seq":\s0,\s"size":\s\d+,\s"rtt":\s\d+\.\d+,\s"rttAvg":\s\d+\.\d+,\s"loss":\s\d+\}\}
-\{"resp":\s\{"timestamp":\s\d+.\d+,\s"host":\s"127\.0\.0\.1",\s"seq":\s1,\s"size":\s\d+,\s"rtt":\s\d+\.\d+,\s"rttAvg":\s\d+\.\d+,\s"loss":\s\d+\}\}
+$cmd->stdout_like(qr/^\{"resp":\s\{"timestamp":\s"\d+.\d+",\s"host":\s"127\.0\.0\.1",\s"seq":\s0,\s"size":\s\d+,\s"rtt":\s\d+\.\d+,\s"rttAvg":\s\d+\.\d+,\s"loss":\s\d+\}\}
+\{"resp":\s\{"timestamp":\s"\d+.\d+",\s"host":\s"127\.0\.0\.1",\s"seq":\s1,\s"size":\s\d+,\s"rtt":\s\d+\.\d+,\s"rttAvg":\s\d+\.\d+,\s"loss":\s\d+\}\}
+\{"summary":\s\{"host":\s"127\.0\.0\.1",\s"xmt":\s\d+,\s"rcv":\s\d+,\s"loss":\s\d+,\s"rttMin":\s\d+\.\d+,\s"rttAvg":\s\d+\.\d+,\s"rttMax":\s\d+\.\d+\}\}\n?$/);
+$cmd->stderr_is_eq("");
+}
+
+# fping -J -c 1 -D --timestamp-format=ctime 127.0.0.1
+{
+my $cmd = Test::Command->new(cmd => "fping -J -c 1 -D --timestamp-format=ctime 127.0.0.1");
+$cmd->exit_is_num(0);
+$cmd->stdout_like(qr/^\{"resp":\s\{"timestamp":\s"\w+\s\w+\s+\d+\s[\d+:]+\s\d+",\s"host":\s"127\.0\.0\.1",\s"seq":\s0,\s"size":\s\d+,\s"rtt":\s\d+\.\d+,\s"rttAvg":\s\d+\.\d+,\s"loss":\s\d+\}\}
+\{"summary":\s\{"host":\s"127\.0\.0\.1",\s"xmt":\s\d+,\s"rcv":\s\d+,\s"loss":\s\d+,\s"rttMin":\s\d+\.\d+,\s"rttAvg":\s\d+\.\d+,\s"rttMax":\s\d+\.\d+\}\}\n?$/);
+$cmd->stderr_is_eq("");
+}
+
+# fping -J -c 1 -D --timestamp-format=iso 127.0.0.1
+{
+my $cmd = Test::Command->new(cmd => "fping -J -c 1 -D --timestamp-format=iso 127.0.0.1");
+$cmd->exit_is_num(0);
+$cmd->stdout_like(qr/^\{"resp":\s\{"timestamp":\s"[\d+-]+T[\d+:]+\+\d+",\s"host":\s"127\.0\.0\.1",\s"seq":\s0,\s"size":\s\d+,\s"rtt":\s\d+\.\d+,\s"rttAvg":\s\d+\.\d+,\s"loss":\s\d+\}\}
+\{"summary":\s\{"host":\s"127\.0\.0\.1",\s"xmt":\s\d+,\s"rcv":\s\d+,\s"loss":\s\d+,\s"rttMin":\s\d+\.\d+,\s"rttAvg":\s\d+\.\d+,\s"rttMax":\s\d+\.\d+\}\}\n?$/);
+$cmd->stderr_is_eq("");
+}
+
+# fping -J -c 1 -D --timestamp-format=rfc3339 127.0.0.1
+{
+my $cmd = Test::Command->new(cmd => "fping -J -c 1 -D --timestamp-format=rfc3339 127.0.0.1");
+$cmd->exit_is_num(0);
+$cmd->stdout_like(qr/^\{"resp":\s\{"timestamp":\s"[\d+-]+\s[\d+:]+",\s"host":\s"127\.0\.0\.1",\s"seq":\s0,\s"size":\s\d+,\s"rtt":\s\d+\.\d+,\s"rttAvg":\s\d+\.\d+,\s"loss":\s\d+\}\}
 \{"summary":\s\{"host":\s"127\.0\.0\.1",\s"xmt":\s\d+,\s"rcv":\s\d+,\s"loss":\s\d+,\s"rttMin":\s\d+\.\d+,\s"rttAvg":\s\d+\.\d+,\s"rttMax":\s\d+\.\d+\}\}\n?$/);
 $cmd->stderr_is_eq("");
 }
index 4595fa82ea3ece5543b99162c8a1b904c2b07f5d..47f743e318cc85159634dabde27ab5a1287a54cf 100644 (file)
@@ -1715,12 +1715,9 @@ void main_loop()
                 if (json_flag)
                     printf("{\"timeout\": {");
 
-                if (timestamp_flag) {
-                    if (json_flag)
-                        printf("\"timestamp\": %.5f, ", (double)current_time_ns / 1e9);
-                    else
-                        print_timestamp_format(current_time_ns, timestamp_format_flag);
-                }
+                if (timestamp_flag)
+                    print_timestamp_format(current_time_ns, timestamp_format_flag);
+
                 if (json_flag)
                 {
                   printf("\"host\": \"%s\", ", h->host);
@@ -3113,12 +3110,9 @@ int wait_for_reply(int64_t wait_time)
         if (json_flag)
             printf("{\"resp\": {");
 
-        if (timestamp_flag) {
-            if (json_flag)
-                printf("\"timestamp\": %.5f, ", (double)recv_time / 1e9);
-            else
-                print_timestamp_format(recv_time, timestamp_format_flag);
-        }
+        if (timestamp_flag)
+            print_timestamp_format(recv_time, timestamp_format_flag);
+
         avg = h->total_time / h->num_recv;
         if (json_flag) {
             printf("\"host\": \"%s\", ", h->host);
@@ -3659,20 +3653,32 @@ void print_timestamp_format(int64_t current_time_ns, int timestamp_format)
         case 1:
             // timestamp-format ctime
             strftime(time_buffer, sizeof(time_buffer), "%c", local_time);
-            printf("[%s] ", time_buffer);
+            if (json_flag)
+                printf("\"timestamp\": \"%s\", ", time_buffer);
+            else
+                printf("[%s] ", time_buffer);
             break;
         case 2:
             // timestamp-format iso
             strftime(time_buffer, sizeof(time_buffer), "%Y-%m-%dT%T%z", local_time);
-            printf("[%s] ", time_buffer);
+            if (json_flag)
+                printf("\"timestamp\": \"%s\", ", time_buffer);
+            else
+                printf("[%s] ", time_buffer);
             break;
         case 3:
             // timestamp-format rfc3339
             strftime(time_buffer, sizeof(time_buffer), "%Y-%m-%d %H:%M:%S", local_time);
-            printf("[%s] ", time_buffer);
+            if (json_flag)
+                printf("\"timestamp\": \"%s\", ", time_buffer);
+            else
+                printf("[%s] ", time_buffer);
             break;
         default:
-            printf("[%.5f] ", (double)current_time_ns / 1e9);
+            if (json_flag)
+                printf("\"timestamp\": \"%.5f\", ", (double)current_time_ns / 1e9);
+            else
+                printf("[%.5f] ", (double)current_time_ns / 1e9);
     }
 }