Переглянути джерело

implement the LOG_PERROR option in syslog

this is a nonstandard feature, but easy and inexpensive to add. since
the corresponding macro has always been defined in our syslog.h, it
makes sense to actually support it. applications may reasonably be
using the presence of the macro to assume that the feature is
supported.

the behavior of omitting the 'header' part of the log message does not
seem to be well-documented, but matches other implementations (at
least glibc) which have this option.

based on a patch by Clément Vasseur, but simplified using %n.
Rich Felker 10 роки тому
батько
коміт
b8c4cf61cb
1 змінених файлів з 4 додано та 2 видалено
  1. 4 2
      src/misc/syslog.c

+ 4 - 2
src/misc/syslog.c

@@ -80,6 +80,7 @@ static void _vsyslog(int priority, const char *message, va_list ap)
 	int errno_save = errno;
 	int errno_save = errno;
 	int pid;
 	int pid;
 	int l, l2;
 	int l, l2;
+	int hlen;
 
 
 	if (log_fd < 0) {
 	if (log_fd < 0) {
 		__openlog();
 		__openlog();
@@ -93,8 +94,8 @@ static void _vsyslog(int priority, const char *message, va_list ap)
 	strftime(timebuf, sizeof timebuf, "%b %e %T", &tm);
 	strftime(timebuf, sizeof timebuf, "%b %e %T", &tm);
 
 
 	pid = (log_opt & LOG_PID) ? getpid() : 0;
 	pid = (log_opt & LOG_PID) ? getpid() : 0;
-	l = snprintf(buf, sizeof buf, "<%d>%s %s%s%.0d%s: ",
-		priority, timebuf, log_ident, "["+!pid, pid, "]"+!pid);
+	l = snprintf(buf, sizeof buf, "<%d>%s %n%s%s%.0d%s: ",
+		priority, timebuf, &hlen, log_ident, "["+!pid, pid, "]"+!pid);
 	errno = errno_save;
 	errno = errno_save;
 	l2 = vsnprintf(buf+l, sizeof buf - l, message, ap);
 	l2 = vsnprintf(buf+l, sizeof buf - l, message, ap);
 	if (l2 >= 0) {
 	if (l2 >= 0) {
@@ -102,6 +103,7 @@ static void _vsyslog(int priority, const char *message, va_list ap)
 		else l += l2;
 		else l += l2;
 		if (buf[l-1] != '\n') buf[l++] = '\n';
 		if (buf[l-1] != '\n') buf[l++] = '\n';
 		send(log_fd, buf, l, 0);
 		send(log_fd, buf, l, 0);
+		if (log_opt & LOG_PERROR) dprintf(2, "%.*s", l-hlen, buf+hlen);
 	}
 	}
 }
 }