public inbox for passt-dev@passt.top
 help / color / mirror / code / Atom feed
From: David Gibson <david@gibson.dropbear.id.au>
To: Stefano Brivio <sbrivio@redhat.com>, passt-dev@passt.top
Subject: Re: [PATCH] log: Don't define logging function 4 times
Date: Fri, 13 Oct 2023 16:17:14 +1100	[thread overview]
Message-ID: <ZSjS2p/+IRicfEct@zatzit> (raw)
In-Reply-To: <20231012050906.3258198-1-david@gibson.dropbear.id.au>

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

On Thu, Oct 12, 2023 at 04:09:06PM +1100, David Gibson wrote:
> In log.c we use a macro to define logging functions for each of 4 priority
> levels.  The only difference between these is the priority we pass to
> vsyslog() and similar functions.  Because it's done as a macro, however,
> the entire functions code is included in the binary 4 times.
> 
> Rearrange this to take the priority level as a parameter to a regular
> function, then just use macros to define trivial wrappers which pass the
> priority level.
> 
> This saves about 600 bytes of text in the executable (x86, non-AVX2).
> 
> Signed-off-by: David Gibson <david@gibson.dropbear.id.au>

Superseded by a new series with a couple of extra patches.

> ---
>  log.c | 65 +++++++++++++++++++++++++++--------------------------------
>  log.h | 12 +++++++----
>  2 files changed, 38 insertions(+), 39 deletions(-)
> 
> diff --git a/log.c b/log.c
> index c57bc8d..fd33f64 100644
> --- a/log.c
> +++ b/log.c
> @@ -47,36 +47,36 @@ 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(out, "%lli.%04lli: ",				\
> -			(long long int)tp.tv_sec - log_start,		\
> -			(long long int)tp.tv_nsec / (100L * 1000));	\
> -	}								\
> -									\
> -	if ((LOG_MASK(LOG_PRI(level)) & log_mask) || BEFORE_DAEMON) {	\
> -		va_start(args, format);					\
> -		if (log_file != -1)					\
> -			logfile_write(level, format, args);		\
> -		else if (!(setlogmask(0) & LOG_MASK(LOG_DEBUG)))	\
> -			passt_vsyslog(level, format, args);		\
> -		va_end(args);						\
> -	}								\
> -									\
> -	if ((setlogmask(0) & LOG_MASK(LOG_DEBUG) && log_file == -1) ||	\
> -	    (BEFORE_DAEMON && !(log_opt & LOG_PERROR))) {		\
> -		va_start(args, format);					\
> -		(void)vfprintf(out, format, args); 			\
> -		va_end(args);						\
> -		if (format[strlen(format)] != '\n')			\
> -			fprintf(out, "\n");				\
> -	}								\
> +void logmsg(int pri, 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(out, "%lli.%04lli: ",
> +			(long long int)tp.tv_sec - log_start,
> +			(long long int)tp.tv_nsec / (100L * 1000));
> +	}
> +
> +	if ((LOG_MASK(LOG_PRI(pri)) & log_mask) || BEFORE_DAEMON) {
> +		va_start(args, format);
> +		if (log_file != -1)
> +			logfile_write(pri, format, args);
> +		else if (!(setlogmask(0) & LOG_MASK(LOG_DEBUG)))
> +			passt_vsyslog(pri, format, args);
> +		va_end(args);
> +	}
> +
> +	if ((setlogmask(0) & LOG_MASK(LOG_DEBUG) && log_file == -1) ||
> +	    (BEFORE_DAEMON && !(log_opt & LOG_PERROR))) {
> +		va_start(args, format);
> +		(void)vfprintf(out, format, args);
> +		va_end(args);
> +		if (format[strlen(format)] != '\n')
> +			fprintf(out, "\n");
> +	}
>  }
>  
>  /* Prefixes for log file messages, indexed by priority */
> @@ -89,11 +89,6 @@ const char *logfile_prefix[] = {
>  	"         ",		/* LOG_DEBUG */
>  };
>  
> -logfn(err,  LOG_ERR)
> -logfn(warn, LOG_WARNING)
> -logfn(info, LOG_INFO)
> -logfn(debug,LOG_DEBUG)
> -
>  /**
>   * trace_init() - Set log_trace depending on trace (debug) mode
>   * @enable:	Tracing debug mode enabled if non-zero
> diff --git a/log.h b/log.h
> index a17171a..b4fb7e9 100644
> --- a/log.h
> +++ b/log.h
> @@ -6,14 +6,18 @@
>  #ifndef LOG_H
>  #define LOG_H
>  
> +#include <syslog.h>
> +
>  #define LOGFILE_SIZE_DEFAULT		(1024 * 1024UL)
>  #define LOGFILE_CUT_RATIO		30	/* When full, cut ~30% size */
>  #define LOGFILE_SIZE_MIN		(5UL * MAX(BUFSIZ, PAGE_SIZE))
>  
> -void err(const char *format, ...);
> -void warn(const char *format, ...);
> -void info(const char *format, ...);
> -void debug(const char *format, ...);
> +void logmsg(int pri, const char *format, ...);
> +
> +#define err(...)	logmsg(LOG_ERR, __VA_ARGS__)
> +#define warn(...)	logmsg(LOG_WARNING, __VA_ARGS__)
> +#define info(...)	logmsg(LOG_INFO, __VA_ARGS__)
> +#define debug(...)	logmsg(LOG_DEBUG, __VA_ARGS__)
>  
>  #define die(...)							\
>  	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 --]

      reply	other threads:[~2023-10-13  5:17 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-10-12  5:09 [PATCH] log: Don't define logging function 4 times David Gibson
2023-10-13  5:17 ` David Gibson [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=ZSjS2p/+IRicfEct@zatzit \
    --to=david@gibson.dropbear.id.au \
    --cc=passt-dev@passt.top \
    --cc=sbrivio@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).