From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from gandalf.ozlabs.org (mail.ozlabs.org [IPv6:2404:9400:2221:ea00::3]) by passt.top (Postfix) with ESMTPS id 1D8165A0272 for ; Fri, 13 Oct 2023 06:50:39 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gibson.dropbear.id.au; s=201602; t=1697172633; bh=+hCkfFjztbLl9yo0G02mMNE4zAV4+NqtKwM91M5MQFs=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=EvMv6HRMnMJVQ+kl+FLhx4wRIVfiu/pm9xPsnedfoUCe7WBGlXyLoRUoWpm7SpJ7s x88HfxOEZnuFPfjFRJHuJLajLd/GmCAaI4eKXaO+lfmT1v/qMcTYz3+28pqFjuLwZC KdYZKGxJSEnDKqsfj8cFyyOLDdSfVVtDQjOGsAOA= Received: by gandalf.ozlabs.org (Postfix, from userid 1007) id 4S6DbF17ylz4xWm; Fri, 13 Oct 2023 15:50:33 +1100 (AEDT) From: David Gibson To: passt-dev@passt.top, Stefano Brivio Subject: [PATCH v2 3/3] log: Add vlogmsg() Date: Fri, 13 Oct 2023 15:50:30 +1100 Message-ID: <20231013045030.85235-4-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: VKNDCY2EPB6ZESPJTEWAPNT7ELU4MYN6 X-Message-ID-Hash: VKNDCY2EPB6ZESPJTEWAPNT7ELU4MYN6 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: Currently logmsg() is only available as a variadic function. This is fine for normal use, but is awkward if we ever want to write wrappers around it which (for example) add standardised prefix information. To allow that, add a vlogmsg() function which takes a va_list instead, and implement logmsg() in terms of it. Signed-off-by: David Gibson --- log.c | 25 ++++++++++++++++--------- log.h | 1 + 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/log.c b/log.c index fd33f64..95c4fa4 100644 --- a/log.c +++ b/log.c @@ -47,11 +47,10 @@ int log_to_stdout; /* Print to stdout instead of stderr */ #define BEFORE_DAEMON (setlogmask(0) == LOG_MASK(LOG_EMERG)) -void logmsg(int pri, const char *format, ...) +void vlogmsg(int pri, const char *format, va_list ap) { 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); @@ -61,24 +60,32 @@ void logmsg(int pri, const char *format, ...) } if ((LOG_MASK(LOG_PRI(pri)) & log_mask) || BEFORE_DAEMON) { - va_start(args, format); + va_list ap2; + + va_copy(ap2, ap); /* Don't clobber ap, we need it again */ if (log_file != -1) - logfile_write(pri, format, args); + logfile_write(pri, format, ap2); else if (!(setlogmask(0) & LOG_MASK(LOG_DEBUG))) - passt_vsyslog(pri, format, args); - va_end(args); + passt_vsyslog(pri, format, ap2); } 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); + (void)vfprintf(out, format, ap); if (format[strlen(format)] != '\n') fprintf(out, "\n"); } } +void logmsg(int pri, const char *format, ...) +{ + va_list ap; + + va_start(ap, format); + vlogmsg(pri, format, ap); + va_end(ap); +} + /* Prefixes for log file messages, indexed by priority */ const char *logfile_prefix[] = { NULL, NULL, NULL, /* Unused: LOG_EMERG, LOG_ALERT, LOG_CRIT */ diff --git a/log.h b/log.h index df72f9a..9c38182 100644 --- a/log.h +++ b/log.h @@ -12,6 +12,7 @@ #define LOGFILE_CUT_RATIO 30 /* When full, cut ~30% size */ #define LOGFILE_SIZE_MIN (5UL * MAX(BUFSIZ, PAGE_SIZE)) +void vlogmsg(int pri, const char *format, va_list ap); void logmsg(int pri, const char *format, ...) __attribute__((format(printf, 2, 3))); -- 2.41.0