From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from gandalf.ozlabs.org (gandalf.ozlabs.org [150.107.74.76]) by passt.top (Postfix) with ESMTPS id A40505A026F for ; Fri, 13 Oct 2023 06:50:35 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gibson.dropbear.id.au; s=201602; t=1697172633; bh=eAzMsfQcGcp2191GS0f2efP2Ugt+46Amm2YX4nf5R3c=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=BPDust5XWBaRQHO7XXfvf9SG5M2y+/VIGTgBlQRuIOEn2Jwn04hsDrI+8UJLWSp+s DEieAvKXvhCUyvQycDPSwhMCZmh6LVqxIQE8JCUOE1KzbrhhcR2f0+kR68+9ArpD6f dN+FR9mcu/2fEbH6Iaewt8a2QYJgyySBRU2JV8XY= Received: by gandalf.ozlabs.org (Postfix, from userid 1007) id 4S6DbF0mGkz4xWb; Fri, 13 Oct 2023 15:50:33 +1100 (AEDT) From: David Gibson To: passt-dev@passt.top, Stefano Brivio Subject: [PATCH v2 1/3] log: Don't define logging function 4 times Date: Fri, 13 Oct 2023 15:50:28 +1100 Message-ID: <20231013045030.85235-2-david@gibson.dropbear.id.au> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20231013045030.85235-1-david@gibson.dropbear.id.au> References: <20231013045030.85235-1-david@gibson.dropbear.id.au> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Message-ID-Hash: BA7ASNLPATJSQTNKEOW3K3S5BL4QDCC5 X-Message-ID-Hash: BA7ASNLPATJSQTNKEOW3K3S5BL4QDCC5 X-MailFrom: dgibson@gandalf.ozlabs.org X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header CC: David Gibson X-Mailman-Version: 3.3.8 Precedence: list List-Id: Development discussion and patches for passt Archived-At: Archived-At: List-Archive: List-Archive: List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: 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 --- 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 + #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 { \ -- 2.41.0