Would be really nice to have this in stretch.

Not sure if there is a safe way to leave the syslog open. The following
patch works for me (--log syslog).
diff --git a/lib/dpkg/log.c b/lib/dpkg/log.c
index 3079f3c..371ec5e 100644
--- a/lib/dpkg/log.c
+++ b/lib/dpkg/log.c
@@ -26,6 +26,7 @@
 #include <unistd.h>
 #include <stdarg.h>
 #include <stdio.h>
+#include <syslog.h>
 
 #include <dpkg/i18n.h>
 #include <dpkg/dpkg.h>
@@ -46,27 +47,35 @@ log_message(const char *fmt, ...)
 	if (!log_file)
 		return;
 
-	if (!logfd) {
-		logfd = fopen(log_file, "a");
-		if (!logfd) {
-			notice(_("could not open log '%s': %s"),
-			       log_file, strerror(errno));
-			log_file = NULL;
-			return;
-		}
-		setlinebuf(logfd);
-		setcloexec(fileno(logfd), log_file);
-	}
-
 	va_start(args, fmt);
 	varbuf_reset(&log);
 	varbuf_vprintf(&log, fmt, args);
 	va_end(args);
 
-	time(&now);
-	strftime(time_str, sizeof(time_str), "%Y-%m-%d %H:%M:%S",
-	         localtime(&now));
-	fprintf(logfd, "%s %s\n", time_str, log.buf);
+	if (strcmp(log_file, "syslog") == 0) {
+		/* log to syslog */
+		openlog("dpkg", LOG_PID, LOG_USER);
+		syslog(LOG_INFO, "%s", log.buf);
+		closelog();
+	} else {
+		/* log to file */
+		if (!logfd) {
+			logfd = fopen(log_file, "a");
+			if (!logfd) {
+				notice(_("could not open log '%s': %s"),
+					   log_file, strerror(errno));
+				log_file = NULL;
+				return;
+			}
+			setlinebuf(logfd);
+			setcloexec(fileno(logfd), log_file);
+		}
+
+		time(&now);
+		strftime(time_str, sizeof(time_str), "%Y-%m-%d %H:%M:%S",
+			     localtime(&now));
+		fprintf(logfd, "%s %s\n", time_str, log.buf);
+	}
 }
 
 struct pipef {

Reply via email to