ALT Linux Team development discussions
 help / color / mirror / Atom feed
From: Alexey Gladkov <legion@altlinux.ru>
To: ALT Devel discussion list <devel@lists.altlinux.org>
Subject: [devel] [PATCH 2/6] syslogd: make logerror is printf-like
Date: Tue, 27 Oct 2020 12:33:47 +0100
Message-ID: <20201027113351.3373843-3-legion@altlinux.ru> (raw)
In-Reply-To: <20201027113351.3373843-1-legion@altlinux.ru>

If we make logerror look like a printf-like function, we can remove
intermediate buffers in calling functions.

Signed-off-by: Alexey Gladkov <gladkov.alexey@gmail.com>
---
 syslogd.c | 41 +++++++++++++++++++++++------------------
 1 file changed, 23 insertions(+), 18 deletions(-)

diff --git a/syslogd.c b/syslogd.c
index 55281bd..d4aa9e4 100644
--- a/syslogd.c
+++ b/syslogd.c
@@ -348,7 +348,8 @@ const char *cvtaddr(struct sockaddr_storage *f, int len);
 const char *cvthname(struct sockaddr_storage *f, int len);
 void domark(int);
 void debug_switch(int);
-void logerror(const char *type);
+void logerror(const char *fmt, ...)
+	SYSKLOGD_FORMAT((__printf__, 1, 2)) SYSKLOGD_NONNULL((1));
 void die(int sig);
 void doexit(int sig);
 void init();
@@ -1927,24 +1928,34 @@ void debug_switch(int sig)
 /*
  * Print syslogd errors some place.
  */
-void logerror(const char *type)
+void logerror(const char *fmt, ...)
 {
+	va_list ap;
 	char buf[BUFSIZ];
 	struct sourceinfo source;
+	int sv_errno = errno;
+
+	strncpy(buf, "syslogd: ", sizeof(buf));
+
+	va_start(ap, fmt);
+	vsnprintf(buf + 9, sizeof(buf) - 9, fmt, ap);
+	va_end(ap);
+
+	verbosef("Called logerror: %s\n", buf);
+
+	if (sv_errno != 0) {
+		size_t bufsz = strlen(buf);
+		if (strerror_r(sv_errno, buf + bufsz, sizeof(buf) - bufsz))
+			errno = 0; // ignore
+	}
 
 	memset(&source, '\0', sizeof(source));
 
 	source.flags = SINFO_ISINTERNAL;
 	source.hostname = LocalHostName;
 
-	verbosef("Called logerr, msg: %s\n", type);
-
-	if (errno == 0)
-		(void) snprintf(buf, sizeof(buf), "syslogd: %s", type);
-	else
-		(void) snprintf(buf, sizeof(buf), "syslogd: %s: %s", type, strerror(errno));
-	errno = 0;
 	logmsg(LOG_SYSLOG|LOG_ERR, buf, &source, ADDDATE);
+	errno = 0;
 	return;
 }
 
@@ -2268,7 +2279,6 @@ void cfline(char *line, struct filed *f)
 	struct addrinfo hints, *ai;
 #endif
 	char buf[MAXLINE];
-	char xbuf[BUFSIZ];
 
 	verbosef("cfline(%s)\n", line);
 
@@ -2316,8 +2326,7 @@ void cfline(char *line, struct filed *f)
 		}
 
 		if (pri < 0) {
-			(void) snprintf(xbuf, sizeof(xbuf), "unknown priority name \"%s\"", buf);
-			logerror(xbuf);
+			logerror("unknown priority name \"%s\"", buf);
 			return;
 		}
 
@@ -2363,8 +2372,7 @@ void cfline(char *line, struct filed *f)
 				i = decode(buf, FacNames);
 				if (i < 0) {
 
-					(void) snprintf(xbuf, sizeof(xbuf), "unknown facility name \"%s\"", buf);
-					logerror(xbuf);
+					logerror("unknown facility name \"%s\"", buf);
 					return;
 				}
 
@@ -2457,8 +2465,7 @@ void cfline(char *line, struct filed *f)
 
 		if ( f->f_file < 0 ){
 			f->f_file = -1;
-			verbosef("Error opening log file: %s\n", p);
-			logerror(p);
+			logerror("Error opening log file: %s", p);
 			break;
 		}
 		if (isatty(f->f_file)) {
@@ -2558,7 +2565,6 @@ static void allocate_log(void)
 		Files = (struct filed *) malloc(sizeof(struct filed));
 		if ( Files == (void *) 0 )
 		{
-			verbosef("Cannot initialize log structure.");
 			logerror("Cannot initialize log structure.");
 			return;
 		}
@@ -2570,7 +2576,6 @@ static void allocate_log(void)
 						  sizeof(struct filed));
 		if ( Files == (struct filed *) 0 )
 		{
-			verbosef("Cannot grow log structure.");
 			logerror("Cannot grow log structure.");
 			return;
 		}
-- 
2.25.4



  parent reply	other threads:[~2020-10-27 11:33 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-10-27 11:33 [devel] [PATCH 0/6] sysklogd: implement checkum chains for log entries Alexey Gladkov
2020-10-27 11:33 ` [devel] [PATCH 1/6] Optimize the filling of the record fields Alexey Gladkov
2020-10-27 11:33 ` Alexey Gladkov [this message]
2020-10-27 11:33 ` [devel] [PATCH 3/6] syslogd: Implement customization of log file records Alexey Gladkov
2020-10-27 11:33 ` [devel] [PATCH 4/6] syslogd: no need to try to substitute a field in a record if it's not in the log_format Alexey Gladkov
2020-10-27 11:33 ` [devel] [PATCH 5/6] Makefile: use make variables instead of a hardcoded list Alexey Gladkov
2020-10-27 11:33 ` [devel] [PATCH 6/6] syslogd: implement checksum chains for log entries Alexey Gladkov
2020-10-27 11:44 ` [devel] [PATCH 0/6] sysklogd: implement checkum " Alexey V. Vissarionov
2020-10-27 12:13   ` Alexey Gladkov

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=20201027113351.3373843-3-legion@altlinux.ru \
    --to=legion@altlinux.ru \
    --cc=devel@lists.altlinux.org \
    /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

ALT Linux Team development discussions

This inbox may be cloned and mirrored by anyone:

	git clone --mirror http://lore.altlinux.org/devel/0 devel/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 devel devel/ http://lore.altlinux.org/devel \
		devel@altlinux.org devel@altlinux.ru devel@lists.altlinux.org devel@lists.altlinux.ru devel@linux.iplabs.ru mandrake-russian@linuxteam.iplabs.ru sisyphus@linuxteam.iplabs.ru
	public-inbox-index devel

Example config snippet for mirrors.
Newsgroup available over NNTP:
	nntp://lore.altlinux.org/org.altlinux.lists.devel


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git