public inbox for passt-dev@passt.top
 help / color / mirror / code / Atom feed
From: David Gibson <david@gibson.dropbear.id.au>
To: passt-dev@passt.top, Stefano Brivio <sbrivio@redhat.com>
Cc: David Gibson <david@gibson.dropbear.id.au>
Subject: [PATCH v2 3/3] log: Add vlogmsg()
Date: Fri, 13 Oct 2023 15:50:30 +1100	[thread overview]
Message-ID: <20231013045030.85235-4-david@gibson.dropbear.id.au> (raw)
In-Reply-To: <20231013045030.85235-1-david@gibson.dropbear.id.au>

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 <david@gibson.dropbear.id.au>
---
 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)));
 
-- 
@@ -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


  parent reply	other threads:[~2023-10-13  4:50 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-10-13  4:50 [PATCH v2 0/3] Some improvements to log functions David Gibson
2023-10-13  4:50 ` [PATCH v2 1/3] log: Don't define logging function 4 times David Gibson
2023-11-07  8:02   ` Stefano Brivio
2023-10-13  4:50 ` [PATCH v2 2/3] log: Enable format warnings David Gibson
2023-10-13  4:50 ` David Gibson [this message]
2023-11-07 12:45 ` [PATCH v2 0/3] Some improvements to log functions Stefano Brivio

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=20231013045030.85235-4-david@gibson.dropbear.id.au \
    --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).