From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail.ozlabs.org (gandalf.ozlabs.org [150.107.74.76]) by passt.top (Postfix) with ESMTPS id 9F4D55A031E for ; Tue, 06 Aug 2024 08:18:53 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gibson.dropbear.id.au; s=202312; t=1722925121; bh=gl1IDwVbSfV2D2vv5M/uTdiL1YsVhu7G/G1CKaoEfNM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=khhxy3IugnB4Tjhtet0DnFJkvjSbmbM/+wKqSSrxYqNy0bWM/9hJZXKWUqutp3j4Z aMZaHH6qvnicXij+HYsiNXUge+9yALNelihpyjihv1uBqbfiYOQKFMpvdyH1IBHA+a tiBdWjkFRwFZ1YVUfhm74Edh1/b8X2YAoiaqQ9I3fdFKETqYYFUUiYAtvef9NBVbzO 6sAxYIubgMcs1MzP5GTrgm4clmSGAPOU8ZR4G/oetHteDcm25S9V4q865joKgcgJq2 arXal5AV8n3tES+IgdYRPJktS+bmQ23MJNm+Jw9ITLfEuNPZX1mOPnk/Lx1OowHs92 5jZC2q/0gPynw== Received: by gandalf.ozlabs.org (Postfix, from userid 1007) id 4WdNRP1z4Zz4wcl; Tue, 6 Aug 2024 16:18:41 +1000 (AEST) From: David Gibson To: passt-dev@passt.top, Stefano Brivio Subject: [PATCH v2 4/4] log: Avoid duplicate calls to logtime() Date: Tue, 6 Aug 2024 16:18:39 +1000 Message-ID: <20240806061839.1950144-5-david@gibson.dropbear.id.au> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20240806061839.1950144-1-david@gibson.dropbear.id.au> References: <20240806061839.1950144-1-david@gibson.dropbear.id.au> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Message-ID-Hash: A3ALIQMZM42C4OLVH2RRJLQJAH4USYE5 X-Message-ID-Hash: A3ALIQMZM42C4OLVH2RRJLQJAH4USYE5 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: We use logtime() to get a timestamp for the log in two places: - in vlogmsg(), which is used only for debug_print messages - in logfile_write() which is only used messages to the log file These cases are mutually exclusive, so we don't ever print the same message with different timestamps, but that's not particularly obvious to see. It's possible future tweaks to logging logic could mean we log to two different places with different timestamps, which would be confusing. Refactor to have a single logtime() call in vlogmsg() and use it for all the places we need it. Signed-off-by: David Gibson --- log.c | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/log.c b/log.c index 9f4cb7fc..f3799224 100644 --- a/log.c +++ b/log.c @@ -225,18 +225,16 @@ static int logfile_rotate(int fd, const struct timespec *now) * logfile_write() - Write entry to log file, trigger rotation if full * @newline: Append newline at the end of the message, if missing * @pri: Facility and level map, same as priority for vsyslog() + * @now: Timestamp * @format: Same as vsyslog() format * @ap: Same as vsyslog() ap */ -static void logfile_write(bool newline, int pri, const char *format, va_list ap) +static void logfile_write(bool newline, int pri, const struct timespec *now, + const char *format, va_list ap) { - const struct timespec *now; - struct timespec ts; char buf[BUFSIZ]; int n; - now = logtime(&ts); - n = logtime_fmt(buf, BUFSIZ, now); n += snprintf(buf + n, BUFSIZ - n, ": %s", logfile_prefix[pri]); @@ -262,13 +260,14 @@ static void logfile_write(bool newline, int pri, const char *format, va_list ap) void vlogmsg(bool newline, int pri, const char *format, va_list ap) { bool debug_print = (log_mask & LOG_MASK(LOG_DEBUG)) && log_file == -1; + const struct timespec *now; + struct timespec ts; + + now = logtime(&ts); if (debug_print) { char timestr[LOGTIME_STRLEN]; - const struct timespec *now; - struct timespec ts; - now = logtime(&ts); logtime_fmt(timestr, sizeof(timestr), now); fprintf(stderr, "%s: ", timestr); } @@ -278,7 +277,7 @@ void vlogmsg(bool newline, int pri, const char *format, va_list ap) va_copy(ap2, ap); /* Don't clobber ap, we need it again */ if (log_file != -1) - logfile_write(newline, pri, format, ap2); + logfile_write(newline, pri, now, format, ap2); else if (!(log_mask & LOG_MASK(LOG_DEBUG))) passt_vsyslog(newline, pri, format, ap2); -- 2.45.2