public inbox for passt-dev@passt.top
 help / color / mirror / code / Atom feed
* [PATCH] conf, log: On -h / --help, print usage to stdout, not stderr
@ 2023-06-04  5:50 Stefano Brivio
  2023-06-05  2:17 ` David Gibson
  0 siblings, 1 reply; 3+ messages in thread
From: Stefano Brivio @ 2023-06-04  5:50 UTC (permalink / raw)
  To: passt-dev; +Cc: Erik Sjölund

Erik suggests that this makes it easier to grep for options, and with
--help we're anyway printing usage information as expected, not as
part of an error report.

Reported-by: Erik Sjölund <erik.sjolund@gmail.com>
Link: https://bugs.passt.top/show_bug.cgi?id=52
Signed-off-by: Stefano Brivio <sbrivio@redhat.com>
---
 conf.c | 4 +++-
 log.c  | 8 +++++---
 log.h  | 1 +
 3 files changed, 9 insertions(+), 4 deletions(-)

diff --git a/conf.c b/conf.c
index ffff235..9eb6992 100644
--- a/conf.c
+++ b/conf.c
@@ -1630,8 +1630,10 @@ void conf(struct ctx *c, int argc, char **argv)
 		case 'U':
 			/* Handle these later, once addresses are configured */
 			break;
-		case '?':
 		case 'h':
+			log_to_stdout = 1;
+			/* Falls through */
+		case '?':
 		default:
 			usage(argv[0]);
 			break;
diff --git a/log.c b/log.c
index 3a3d101..63d7801 100644
--- a/log.c
+++ b/log.c
@@ -43,17 +43,19 @@ static char	log_header[BUFSIZ];	/* File header, written back on cuts */
 
 static time_t	log_start;		/* Start timestamp */
 int		log_trace;		/* --trace mode enabled */
+int		log_to_stdout;		/* Print to stdout instead of stderr */
 
 #define BEFORE_DAEMON		(setlogmask(0) == LOG_MASK(LOG_EMERG))
 
 #define logfn(name, level)						\
 void name(const char *format, ...) {					\
+	FILE *out = log_to_stdout ? stdout : stderr;			\
 	struct timespec tp;						\
 	va_list args;							\
 									\
 	if (setlogmask(0) & LOG_MASK(LOG_DEBUG) && log_file == -1) {	\
 		clock_gettime(CLOCK_REALTIME, &tp);			\
-		fprintf(stderr, "%lli.%04lli: ",			\
+		fprintf(out, "%lli.%04lli: ",				\
 			(long long int)tp.tv_sec - log_start,		\
 			(long long int)tp.tv_nsec / (100L * 1000));	\
 	}								\
@@ -70,10 +72,10 @@ void name(const char *format, ...) {					\
 	if ((setlogmask(0) & LOG_MASK(LOG_DEBUG) && log_file == -1) ||	\
 	    (BEFORE_DAEMON && !(log_opt & LOG_PERROR))) {		\
 		va_start(args, format);					\
-		(void)vfprintf(stderr, format, args); 			\
+		(void)vfprintf(out, format, args); 			\
 		va_end(args);						\
 		if (format[strlen(format)] != '\n')			\
-			fprintf(stderr, "\n");				\
+			fprintf(out, "\n");				\
 	}								\
 }
 
diff --git a/log.h b/log.h
index 3aab29d..a17171a 100644
--- a/log.h
+++ b/log.h
@@ -22,6 +22,7 @@ void debug(const char *format, ...);
 	} while (0)
 
 extern int log_trace;
+extern int log_to_stdout;
 void trace_init(int enable);
 #define trace(...)							\
 	do {								\
-- 
@@ -22,6 +22,7 @@ void debug(const char *format, ...);
 	} while (0)
 
 extern int log_trace;
+extern int log_to_stdout;
 void trace_init(int enable);
 #define trace(...)							\
 	do {								\
-- 
2.39.2


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH] conf, log: On -h / --help, print usage to stdout, not stderr
  2023-06-04  5:50 [PATCH] conf, log: On -h / --help, print usage to stdout, not stderr Stefano Brivio
@ 2023-06-05  2:17 ` David Gibson
  2023-06-05  5:19   ` Stefano Brivio
  0 siblings, 1 reply; 3+ messages in thread
From: David Gibson @ 2023-06-05  2:17 UTC (permalink / raw)
  To: Stefano Brivio; +Cc: passt-dev, Erik Sjölund

[-- Attachment #1: Type: text/plain, Size: 3018 bytes --]

On Sun, Jun 04, 2023 at 07:50:01AM +0200, Stefano Brivio wrote:
> Erik suggests that this makes it easier to grep for options, and with
> --help we're anyway printing usage information as expected, not as
> part of an error report.
> 
> Reported-by: Erik Sjölund <erik.sjolund@gmail.com>
> Link: https://bugs.passt.top/show_bug.cgi?id=52
> Signed-off-by: Stefano Brivio <sbrivio@redhat.com>

Clunky way to accomplish it, but it'll do for now.

Reviewed-by: David Gibson <david@gibson.dropbear.id.au>

> ---
>  conf.c | 4 +++-
>  log.c  | 8 +++++---
>  log.h  | 1 +
>  3 files changed, 9 insertions(+), 4 deletions(-)
> 
> diff --git a/conf.c b/conf.c
> index ffff235..9eb6992 100644
> --- a/conf.c
> +++ b/conf.c
> @@ -1630,8 +1630,10 @@ void conf(struct ctx *c, int argc, char **argv)
>  		case 'U':
>  			/* Handle these later, once addresses are configured */
>  			break;
> -		case '?':
>  		case 'h':
> +			log_to_stdout = 1;
> +			/* Falls through */
> +		case '?':
>  		default:
>  			usage(argv[0]);
>  			break;
> diff --git a/log.c b/log.c
> index 3a3d101..63d7801 100644
> --- a/log.c
> +++ b/log.c
> @@ -43,17 +43,19 @@ static char	log_header[BUFSIZ];	/* File header, written back on cuts */
>  
>  static time_t	log_start;		/* Start timestamp */
>  int		log_trace;		/* --trace mode enabled */
> +int		log_to_stdout;		/* Print to stdout instead of stderr */
>  
>  #define BEFORE_DAEMON		(setlogmask(0) == LOG_MASK(LOG_EMERG))
>  
>  #define logfn(name, level)						\
>  void name(const char *format, ...) {					\
> +	FILE *out = log_to_stdout ? stdout : stderr;			\
>  	struct timespec tp;						\
>  	va_list args;							\
>  									\
>  	if (setlogmask(0) & LOG_MASK(LOG_DEBUG) && log_file == -1) {	\
>  		clock_gettime(CLOCK_REALTIME, &tp);			\
> -		fprintf(stderr, "%lli.%04lli: ",			\
> +		fprintf(out, "%lli.%04lli: ",				\
>  			(long long int)tp.tv_sec - log_start,		\
>  			(long long int)tp.tv_nsec / (100L * 1000));	\
>  	}								\
> @@ -70,10 +72,10 @@ void name(const char *format, ...) {					\
>  	if ((setlogmask(0) & LOG_MASK(LOG_DEBUG) && log_file == -1) ||	\
>  	    (BEFORE_DAEMON && !(log_opt & LOG_PERROR))) {		\
>  		va_start(args, format);					\
> -		(void)vfprintf(stderr, format, args); 			\
> +		(void)vfprintf(out, format, args); 			\
>  		va_end(args);						\
>  		if (format[strlen(format)] != '\n')			\
> -			fprintf(stderr, "\n");				\
> +			fprintf(out, "\n");				\
>  	}								\
>  }
>  
> diff --git a/log.h b/log.h
> index 3aab29d..a17171a 100644
> --- a/log.h
> +++ b/log.h
> @@ -22,6 +22,7 @@ void debug(const char *format, ...);
>  	} while (0)
>  
>  extern int log_trace;
> +extern int log_to_stdout;
>  void trace_init(int enable);
>  #define trace(...)							\
>  	do {								\

-- 
David Gibson			| I'll have my music baroque, and my code
david AT gibson.dropbear.id.au	| minimalist, thank you.  NOT _the_ _other_
				| _way_ _around_!
http://www.ozlabs.org/~dgibson

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] conf, log: On -h / --help, print usage to stdout, not stderr
  2023-06-05  2:17 ` David Gibson
@ 2023-06-05  5:19   ` Stefano Brivio
  0 siblings, 0 replies; 3+ messages in thread
From: Stefano Brivio @ 2023-06-05  5:19 UTC (permalink / raw)
  To: David Gibson; +Cc: passt-dev, Erik Sjölund

On Mon, 5 Jun 2023 12:17:35 +1000
David Gibson <david@gibson.dropbear.id.au> wrote:

> On Sun, Jun 04, 2023 at 07:50:01AM +0200, Stefano Brivio wrote:
> > Erik suggests that this makes it easier to grep for options, and with
> > --help we're anyway printing usage information as expected, not as
> > part of an error report.
> > 
> > Reported-by: Erik Sjölund <erik.sjolund@gmail.com>
> > Link: https://bugs.passt.top/show_bug.cgi?id=52
> > Signed-off-by: Stefano Brivio <sbrivio@redhat.com>  
> 
> Clunky way to accomplish it, but it'll do for now.

Oops, wait until you see v2...

-- 
Stefano


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2023-06-05  5:19 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-06-04  5:50 [PATCH] conf, log: On -h / --help, print usage to stdout, not stderr Stefano Brivio
2023-06-05  2:17 ` David Gibson
2023-06-05  5:19   ` Stefano Brivio

Code repositories for project(s) associated with this public inbox

	https://passt.top/passt

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for IMAP folder(s).