From mboxrd@z Thu Jan 1 00:00:00 1970 Received: by passt.top (Postfix, from userid 1000) id AA5005A026A; Thu, 23 Feb 2023 18:35:55 +0100 (CET) From: Stefano Brivio To: passt-dev@passt.top Subject: [PATCH] log: Send identifier string in log messages, openlog() won't work for us Date: Thu, 23 Feb 2023 18:35:55 +0100 Message-Id: <20230223173555.3896768-1-sbrivio@redhat.com> X-Mailer: git-send-email 2.39.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Message-ID-Hash: NGDT7D5YLLNPZHTHYCUGEGYT5YOZGINM X-Message-ID-Hash: NGDT7D5YLLNPZHTHYCUGEGYT5YOZGINM X-MailFrom: sbrivio@passt.top 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: Andrea Bolognani 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: openlog() can be used to set "ident" and have all the log messages prefixed by it, but only if we call syslog() -- this is implemented by C libraries. We don't log messages with syslog(), though, as we have a custom implementation to ensure we don't need dynamic memory allocation. This means that it's perfectly useless to call openlog(), and that we have to prefix every message we log by the identifier on our own. Reported-by: Andrea Bolognani Signed-off-by: Stefano Brivio --- log.c | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/log.c b/log.c index f23e8ef..bfde6ce 100644 --- a/log.c +++ b/log.c @@ -106,7 +106,7 @@ void trace_init(int enable) } /** - * __openlog() - Non-optional openlog() wrapper, to allow custom vsyslog() + * __openlog() - Non-optional openlog() implementation, for custom vsyslog() * @ident: openlog() identity (program name) * @option: openlog() options * @facility: openlog() facility (LOG_DAEMON) @@ -136,8 +136,6 @@ void __openlog(const char *ident, int option, int facility) log_mask |= facility; strncpy(log_ident, ident, sizeof(log_ident) - 1); log_opt = option; - - openlog(ident, option, facility); } /** @@ -158,11 +156,11 @@ void __setlogmask(int mask) */ void passt_vsyslog(int pri, const char *format, va_list ap) { + int prefix_len, n; char buf[BUFSIZ]; - int n; - /* Send without name and timestamp, the system logger should add them */ - n = snprintf(buf, BUFSIZ, "<%i> ", pri); + /* Send without timestamp, the system logger should add it */ + n = prefix_len = snprintf(buf, BUFSIZ, "<%i> %s: ", pri, log_ident); n += vsnprintf(buf + n, BUFSIZ - n, format, ap); @@ -170,7 +168,7 @@ void passt_vsyslog(int pri, const char *format, va_list ap) n += snprintf(buf + n, BUFSIZ - n, "\n"); if (log_opt & LOG_PERROR) - fprintf(stderr, "%s", buf + sizeof("<0>")); + fprintf(stderr, "%s", buf + prefix_len); if (send(log_sock, buf, n, 0) != n) fprintf(stderr, "Failed to send %i bytes to syslog\n", n); -- 2.39.1