Module Name:    src
Committed By:   roy
Date:           Wed Mar 22 17:52:37 UTC 2017

Modified Files:
        src/lib/libc/gen: syslog.3 xsyslog.c
        src/sys/sys: syslog.h

Log Message:
openlog(3): add LOG_PTRIM and LOG_NLOG log options.

syslog(3) is the one stop method of logging system events and diagnostics.
When debugging a daemon in the foreground on a terminal, each line is
prefixed with tag[pid]: which is very repetative and can take up valuable
screen estate.
LOG_PTRIM solves this by removing this prefix from stderr output.

There is also the case where the debugging could involve a dry-run and
syslog(3) calls would pollute the system log with incorrect data.
LOG_NLOG solves this by not writing the the system log, but allowing
LOG_PERROR to operate as before.

Initially discussed here:
https://mail-index.netbsd.org/tech-userlevel/2016/10/06/msg010330.html


To generate a diff of this commit:
cvs rdiff -u -r1.30 -r1.31 src/lib/libc/gen/syslog.3
cvs rdiff -u -r1.2 -r1.3 src/lib/libc/gen/xsyslog.c
cvs rdiff -u -r1.40 -r1.41 src/sys/sys/syslog.h

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/lib/libc/gen/syslog.3
diff -u src/lib/libc/gen/syslog.3:1.30 src/lib/libc/gen/syslog.3:1.31
--- src/lib/libc/gen/syslog.3:1.30	Tue Feb 21 18:22:15 2017
+++ src/lib/libc/gen/syslog.3	Wed Mar 22 17:52:36 2017
@@ -1,4 +1,4 @@
-.\"	$NetBSD: syslog.3,v 1.30 2017/02/21 18:22:15 abhinav Exp $
+.\"	$NetBSD: syslog.3,v 1.31 2017/03/22 17:52:36 roy Exp $
 .\"	$OpenBSD: syslog.3,v 1.25 2005/07/22 03:16:58 jaredy Exp $
 .\"
 .\" Copyright (c) 1985, 1991, 1993
@@ -30,7 +30,7 @@
 .\"
 .\"     @(#)syslog.3	8.1 (Berkeley) 6/4/93
 .\"
-.Dd May 3, 2010
+.Dd March 22, 2017
 .Dt SYSLOG 3
 .Os
 .Sh NAME
@@ -295,6 +295,10 @@ immediately.
 Normally the open is delayed until the first message is logged.
 Useful for programs that need to manage the order in which file
 descriptors are allocated.
+.It Dv LOG_NLOG
+Stops syslog from writing to the system log.
+Only useful with
+.Dv LOG_PERROR .
 .It Dv LOG_PERROR
 Write the message to standard error output as well to the system log.
 .It Dv LOG_PID
@@ -302,6 +306,9 @@ Log the process id with each message: us
 instantiations of daemons.
 (This PID is placed within brackets
 between the ident and the message.)
+.It Dv LOG_PTRIM
+Trim anything syslog added to the message before writing to 
+standard error output.
 .El
 .Pp
 The

Index: src/lib/libc/gen/xsyslog.c
diff -u src/lib/libc/gen/xsyslog.c:1.2 src/lib/libc/gen/xsyslog.c:1.3
--- src/lib/libc/gen/xsyslog.c:1.2	Thu Jan 12 01:58:39 2017
+++ src/lib/libc/gen/xsyslog.c	Wed Mar 22 17:52:36 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: xsyslog.c,v 1.2 2017/01/12 01:58:39 christos Exp $	*/
+/*	$NetBSD: xsyslog.c,v 1.3 2017/03/22 17:52:36 roy Exp $	*/
 
 /*
  * Copyright (c) 1983, 1988, 1993
@@ -34,7 +34,7 @@
 #if 0
 static char sccsid[] = "@(#)syslog.c	8.5 (Berkeley) 4/29/95";
 #else
-__RCSID("$NetBSD: xsyslog.c,v 1.2 2017/01/12 01:58:39 christos Exp $");
+__RCSID("$NetBSD: xsyslog.c,v 1.3 2017/03/22 17:52:36 roy Exp $");
 #endif
 #endif /* LIBC_SCCS and not lint */
 
@@ -301,11 +301,24 @@ _vxsyslogp_r(int pri, struct syslog_fun 
 
 	/* Output to stderr if requested. */
 	if (data->log_stat & LOG_PERROR) {
+		struct iovec *piov;
+		int piovcnt;
+
 		iov[iovcnt].iov_base = __UNCONST(CRLF + 1);
 		iov[iovcnt].iov_len = 1;
-		(void)writev(STDERR_FILENO, iov, iovcnt + 1);
+		if (data->log_stat & LOG_PTRIM) {
+			piov = &iov[iovcnt - 1];
+			piovcnt = 2;
+		} else {
+			piov = iov;
+			piovcnt = iovcnt + 1;
+		}
+		(void)writev(STDERR_FILENO, piov, piovcnt);
 	}
 
+	if (data->log_stat & LOG_NLOG)
+		goto out;
+
 	/* Get connected, output the message to the local logger. */
 	(*fun->lock)(data);
 	opened = !data->log_opened;
@@ -345,6 +358,7 @@ _vxsyslogp_r(int pri, struct syslog_fun 
 		(void)close(fd);
 	}
 
+out:
 	if (!(*fun->unlock)(data) && opened)
 		_closelog_unlocked_r(data);
 }

Index: src/sys/sys/syslog.h
diff -u src/sys/sys/syslog.h:1.40 src/sys/sys/syslog.h:1.41
--- src/sys/sys/syslog.h:1.40	Tue Mar 21 11:54:46 2017
+++ src/sys/sys/syslog.h	Wed Mar 22 17:52:36 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: syslog.h,v 1.40 2017/03/21 11:54:46 roy Exp $	*/
+/*	$NetBSD: syslog.h,v 1.41 2017/03/22 17:52:36 roy Exp $	*/
 
 /*
  * Copyright (c) 1982, 1986, 1988, 1993
@@ -168,6 +168,8 @@ CODE facilitynames[] = {
 #define	LOG_NDELAY	0x08	/* don't delay open */
 #define	LOG_NOWAIT	0x10	/* don't wait for console forks: DEPRECATED */
 #define	LOG_PERROR	0x20	/* log to stderr as well */
+#define LOG_PTRIM	0x40	/* trim tag and pid from messages to stderr */
+#define LOG_NLOG	0x80	/* don't write to the system log */
 
 #ifndef _KERNEL
 

Reply via email to