curl: support repeated use of the verbose option; -vv etc

- make mentioning `-v` on the curl command line increase the
  verbosity of the trace output
- related discussion https://github.com/curl/curl/discussions/13810
- make a single -v revert all previous -v+ changes
- make --no-verbose also reset all trace configs

Closes #13977
This commit is contained in:
Stefan Eissing 2024-08-06 11:44:23 +02:00 committed by Daniel Stenberg
parent 53146dd262
commit 06c5829dab
No known key found for this signature in database
GPG key ID: 5CC908FDB71E12C2
5 changed files with 175 additions and 56 deletions

View file

@ -316,6 +316,7 @@ struct GlobalConfig {
bool silent; /* do not show messages, --silent given */
bool noprogress; /* do not show progress bar */
bool isatty; /* Updated internally if output is a tty */
unsigned char verbosity; /* How verbose we should be */
char *trace_dump; /* file to dump the network trace to */
FILE *trace_stream;
bool trace_fopened;

View file

@ -1029,6 +1029,7 @@ ParameterError getparameter(const char *flag, /* f or -long-flag */
time_t now;
bool longopt = FALSE;
bool singleopt = FALSE; /* when true means '-o foo' used '-ofoo' */
size_t nopts = 0; /* options processed in `flag`*/
ParameterError err = PARAM_OK;
bool toggle = TRUE; /* how to switch boolean options, on or off. Controlled
by using --OPTION or --no-OPTION */
@ -2490,8 +2491,27 @@ ParameterError getparameter(const char *flag, /* f or -long-flag */
cleanarg(clearthis);
break;
case C_VERBOSE: /* --verbose */
if(toggle) {
/* the '%' thing here will cause the trace get sent to stderr */
/* This option is a super-boolean with side effect when applied
* more than once in the same argument flag, like `-vvv`. */
if(!toggle) {
global->verbosity = 0;
if(set_trace_config(global, "-all"))
err = PARAM_NO_MEM;
global->tracetype = TRACE_NONE;
break;
}
else if(!nopts) {
/* fist `-v` in an argument resets to base verbosity */
global->verbosity = 0;
if(set_trace_config(global, "-all")) {
err = PARAM_NO_MEM;
break;
}
}
/* the '%' thing here will cause the trace get sent to stderr */
switch(global->verbosity) {
case 0:
global->verbosity = 1;
Curl_safefree(global->trace_dump);
global->trace_dump = strdup("%");
if(!global->trace_dump)
@ -2499,13 +2519,30 @@ ParameterError getparameter(const char *flag, /* f or -long-flag */
else {
if(global->tracetype && (global->tracetype != TRACE_PLAIN))
warnf(global,
"-v, --verbose overrides an earlier trace/verbose option");
"-v, --verbose overrides an earlier trace option");
global->tracetype = TRACE_PLAIN;
}
break;
case 1:
global->verbosity = 2;
if(set_trace_config(global, "ids,time,protocol"))
err = PARAM_NO_MEM;
break;
case 2:
global->verbosity = 3;
global->tracetype = TRACE_ASCII;
if(set_trace_config(global, "ssl,read,write"))
err = PARAM_NO_MEM;
break;
case 3:
global->verbosity = 4;
if(set_trace_config(global, "network"))
err = PARAM_NO_MEM;
break;
default:
/* no effect for now */
break;
}
else
/* verbose is disabled here */
global->tracetype = TRACE_NONE;
break;
case C_VERSION: /* --version */
if(toggle) /* --no-version yields no output! */
@ -2634,7 +2671,7 @@ ParameterError getparameter(const char *flag, /* f or -long-flag */
break;
}
a = NULL;
++nopts; /* processed one option from `flag` input, loop for more */
} while(!longopt && !singleopt && *++parse && !*usedarg && !err);
error: