Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

-D, --timestamp with custom time string eq: %Y-%m-%d %H:%M:%S #310

Open
coolshou opened this issue Mar 28, 2024 · 4 comments
Open

-D, --timestamp with custom time string eq: %Y-%m-%d %H:%M:%S #310

coolshou opened this issue Mar 28, 2024 · 4 comments

Comments

@coolshou
Copy link

Currently the --timestamp output string as following, can we custom it in human readable format eq: %Y-%m-%d %H:%M:%S
fping -D -l 192.168.0.21
[1711612145.46829] 192.168.0.21 : [0], 64 bytes, 27.2 ms (27.2 avg, 0% loss)
[1711612146.48808] 192.168.0.21 : [1], 64 bytes, 46.0 ms (36.6 avg, 0% loss)
[1711612147.46816] 192.168.0.21 : [2], 64 bytes, 26.1 ms (33.1 avg, 0% loss)

@schweikert
Copy link
Owner

We can't change the format, so this would mean adding yet another option (and in general we have too many already). We could possibly add --timestamp-format (without a short-option equivalent), if somebody wants to create a pull request for that.

@gsnw
Copy link
Collaborator

gsnw commented May 11, 2024

If I see this correctly, the one function in optparse.c would first have to be created or extended so that the long name of the comman line option can be used without short.
Currently the short is always returned as ASCII int.

@auerswal
Copy link
Collaborator

I did a quick test, a number greater than 255 can be used instead of a character for options that only have a long name:

diff --git a/src/fping.c b/src/fping.c
index c58e20c..f7a3129 100644
--- a/src/fping.c
+++ b/src/fping.c
@@ -552,6 +552,7 @@ int main(int argc, char **argv)
         { "version", 'v', OPTPARSE_NONE },
         { "reachable", 'x', OPTPARSE_REQUIRED },
         { "fast-reachable", 'X', OPTPARSE_REQUIRED },
+        { "long-only-test", 256, OPTPARSE_NONE },
 #if defined(DEBUG) || defined(_DEBUG)
         { NULL, 'z', OPTPARSE_REQUIRED },
 #endif
@@ -872,6 +873,11 @@ int main(int argc, char **argv)
             outage_flag = 1;
             break;
 
+        case 256:
+            puts("long-only option given");
+            exit(0);
+            break;
+
         case '?':
             fprintf(stderr, "%s: %s\n", argv[0], optparse_state.errmsg);
             fprintf(stderr, "see 'fping -h' for usage information\n");

This works as intended for the long option:

$ ./src/fping --long-only-test
long-only option given

#defines or an enum could be used instead of simple numbers.

I do not know if this is the way optparse_long() is intended to be used for options without a short (i.e., one character) name, but it seems to work.

@gsnw
Copy link
Collaborator

gsnw commented May 19, 2024

My quick solution goes in a similar direction. I use the ASCII number 0 and then look at which long-option is set exactly.
I don't know yet whether this is well thought out. But it seems to work for now.

diff --git a/src/fping.c b/src/fping.c
index c58e20c..9177f00 100644
--- a/src/fping.c
+++ b/src/fping.c
@@ -357,6 +357,7 @@ int per_recv_flag, report_all_rtts_flag, name_flag, addr_flag, backoff_flag, rdn
 int multif_flag, timeout_flag, fast_reachable;
 int outage_flag = 0;
 int timestamp_flag = 0;
+int timestamp_format_flag = 0;
 int random_data_flag = 0;
 int cumulative_stats_flag = 0;
 #if defined(DEBUG) || defined(_DEBUG)
@@ -522,6 +523,7 @@ int main(int argc, char **argv)
         { "vcount", 'C', OPTPARSE_REQUIRED },
         { "rdns", 'd', OPTPARSE_NONE },
         { "timestamp", 'D', OPTPARSE_NONE },
+        { "timestamp-format", '0', OPTPARSE_NONE },
         { "elapsed", 'e', OPTPARSE_NONE },
         { "file", 'f', OPTPARSE_REQUIRED },
         { "generate", 'g', OPTPARSE_NONE },
@@ -561,6 +563,13 @@ int main(int argc, char **argv)
     float opt_value_float;
     while ((c = optparse_long(&optparse_state, longopts, NULL)) != EOF) {
         switch (c) {
+        case '0':
+              if(strstr(optparse_state.optlongname, "timestamp-format") != NULL) {
+                timestamp_format_flag = 1;
+                puts("timestamp-format found");
+                exit(0);
+              }
+            break;
         case '4':
 #ifdef IPV6
             if (hints_ai_family != AF_UNSPEC && hints_ai_family != AF_INET) {
diff --git a/src/optparse.c b/src/optparse.c
index 4242bff..63a9f40 100644
--- a/src/optparse.c
+++ b/src/optparse.c
@@ -238,12 +238,14 @@ optparse_long(struct optparse *options,
     /* Parse as long option. */
     options->errmsg[0] = '\0';
     options->optopt = 0;
+    options->optlongname = 0;
     options->optarg = 0;
     option += 2; /* skip "--" */
     options->optind++;
     for (int i = 0; !longopts_end(longopts, i); i++) {
         const char *name = longopts[i].longname;
         if (longopts_match(name, option)) {
+            options->optlongname = option;
             if (longindex)
                 *longindex = i;
             options->optopt = longopts[i].shortname;
diff --git a/src/optparse.h b/src/optparse.h
index f124b75..7a555ed 100644
--- a/src/optparse.h
+++ b/src/optparse.h
@@ -48,6 +48,7 @@ struct optparse {
     int permute;
     int optind;
     int optopt;
+    char *optlongname;
     char *optarg;
     char errmsg[64];
     int subopt;

Later in the execution, you must of course check whether "timestamp_flag = 1" and "timestamp_format_flag = 1", because the longoption can also be set without -D.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants