Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 19 Sep 2002 17:51:49 -0500 (CDT)
From:      "Chris S.J. Peron" <maneo@bsdpro.com>
To:        FreeBSD-gnats-submit@FreeBSD.org
Subject:   bin/42974: [patch] ISO 8601 date format option
Message-ID:  <200209192251.g8JMpn1D062982@xor.sqrt.ca>

next in thread | raw e-mail | index | archive | help

>Number:         42974
>Category:       bin
>Synopsis:       [patch] ISO 8601 date format option
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    freebsd-bugs
>State:          open
>Quarter:        
>Keywords:       
>Date-Required:
>Class:          change-request
>Submitter-Id:   current-users
>Arrival-Date:   Thu Sep 19 16:00:17 PDT 2002
>Closed-Date:
>Last-Modified:
>Originator:     Chris S.J. Peron
>Release:        FreeBSD 4.6-STABLE i386
>Organization:
Seccuris Inc.
>Environment:
System: FreeBSD xor.sqrt.ca 4.6-STABLE FreeBSD 4.6-STABLE #4: Sun Sep 15 12:51:00 CDT 2002 modulus@xor.sqrt.ca:/usr/src/sys/compile/opcode i386


	
>Description:
	The current implementation of syslogd does not have the option to print
	the date of the logging events using the ``ISO 8601'' (YYYY-MM-DD) format.

	This option would be handy for anyone who writes external syslog parsers
	that insert the log entries into some database.
	
>How-To-Repeat:
	N/A
>Fix:


I have written the option ``-D'' to have syslogd log using
the ``ISO 8601'' date format. I have also patched the man page
to reflect the changes.


--- syslogd.o.c	Thu Sep 19 15:44:36 2002
+++ syslogd.c	Thu Sep 19 17:40:09 2002
@@ -129,6 +129,8 @@
 
 #define MAXFUNIX       20
 
+#define MAXSTAMPLEN       32
+
 int nfunix = 1;
 const char *funixn[MAXFUNIX] = { _PATH_LOG };
 int funix[MAXFUNIX];
@@ -174,7 +176,7 @@
 		} f_pipe;
 	} f_un;
 	char	f_prevline[MAXSVLINE];		/* last message logged */
-	char	f_lasttime[16];			/* time of last occurrence */
+	char	f_lasttime[MAXSTAMPLEN];	/* time of last occurrence */
 	char	f_prevhost[MAXHOSTNAMELEN];	/* host from which recd. */
 	int	f_prevpri;			/* pri of f_prevline */
 	int	f_prevlen;			/* length of f_prevline */
@@ -258,6 +260,7 @@
 
 int	Debug;			/* debug flag */
 int	resolve = 1;		/* resolve hostname */
+int	iso8601stamp = 0;	/* date/time printed in YYYY-MM-DD HH:MM:SS format ISO 9601 */
 char	LocalHostName[MAXHOSTNAMELEN];	/* our hostname */
 const char	*LocalDomain;		/* our local domain name */
 int	*finet = NULL;		/* Internet datagram socket */
@@ -333,7 +336,7 @@
 	socklen_t len;
 
 	bindhostname = NULL;
-	while ((ch = getopt(argc, argv, "46Aa:b:cdf:kl:m:np:P:suv")) != -1)
+	while ((ch = getopt(argc, argv, "46Aa:b:cDdf:kl:m:np:P:suv")) != -1)
 		switch (ch) {
 		case '4':
 			family = PF_INET;
@@ -359,6 +362,9 @@
 		case 'd':		/* debug */
 			Debug++;
 			break;
+		case 'D':
+			iso8601stamp = 1;
+			break;
 		case 'f':		/* configuration file */
 			ConfFile = optarg;
 			break;
@@ -761,6 +767,7 @@
 	int flags;
 {
 	struct filed *f;
+	struct tm *t;
 	int i, fac, msglen, omask, prilev;
 	const char *timestamp;
  	char prog[NAME_MAX+1];
@@ -780,12 +787,19 @@
 		flags |= ADDDATE;
 
 	(void)time(&now);
-	if (flags & ADDDATE)
-		timestamp = ctime(&now) + 4;
-	else {
-		timestamp = msg;
-		msg += 16;
-		msglen -= 16;
+	if (iso8601stamp) {
+		timestamp = alloca(MAXSTAMPLEN);
+		memset((char *)timestamp, 0, MAXSTAMPLEN);
+		t = localtime(&now);
+		strftime((char *)timestamp, MAXSTAMPLEN, "%Y-%m-%d %H:%M:%S", t);
+	} else {
+		if (flags & ADDDATE)
+			timestamp = ctime(&now) + 4;
+		else {
+			timestamp = msg;
+			msg += 16;
+			msglen -= 16;
+		}
 	}
 
 	/* skip leading blanks */
@@ -868,7 +882,7 @@
 		    (flags & MARK) == 0 && msglen == f->f_prevlen &&
 		    !strcmp(msg, f->f_prevline) &&
 		    !strcasecmp(from, f->f_prevhost)) {
-			(void)strlcpy(f->f_lasttime, timestamp, 16);
+			(void)strlcpy(f->f_lasttime, timestamp, iso8601stamp ? MAXSTAMPLEN : 16);
 			f->f_prevcount++;
 			dprintf("msg repeated %d times, %ld sec of %d\n",
 			    f->f_prevcount, (long)(now - f->f_time),
@@ -889,7 +903,7 @@
 				fprintlog(f, 0, (char *)NULL);
 			f->f_repeatcount = 0;
 			f->f_prevpri = pri;
-			(void)strlcpy(f->f_lasttime, timestamp, 16);
+			(void)strlcpy(f->f_lasttime, timestamp, iso8601stamp ? MAXSTAMPLEN : 16);
 			(void)strlcpy(f->f_prevhost, from,
 			    sizeof(f->f_prevhost));
 			if (msglen < MAXSVLINE) {
@@ -932,7 +946,7 @@
 		v++;
 	} else {
 		v->iov_base = f->f_lasttime;
-		v->iov_len = 15;
+		v->iov_len = iso8601stamp ? MAXSTAMPLEN : 15;
 		v++;
 		v->iov_base = " ";
 		v->iov_len = 1;


--- syslogd.o.8	Thu Sep 19 16:30:06 2002
+++ syslogd.8	Thu Sep 19 16:33:50 2002
@@ -40,7 +40,7 @@
 .Nd log systems messages
 .Sh SYNOPSIS
 .Nm
-.Op Fl 46Adknsuv
+.Op Fl 46AdDknsuv
 .Op Fl a Ar allowed_peer
 .Op Fl b Ar bind_address
 .Op Fl f Ar config_file
@@ -162,6 +162,13 @@
 .Dq last message repeated N times
 when the output is a pipe to another program.
 If specified twice, disable this compression in all cases.
+.It Fl D
+Instruct
+.Nm
+to use ``ISO 8601'' style date/time formatting when writing log
+records. Dates will show up in ``YYYY-MM-DD'' format. This could take
+the load off of most external syslog parsers that would want to insert
+records into a database.
 .It Fl d
 Put
 .Nm
>Release-Note:
>Audit-Trail:
>Unformatted:

To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-bugs" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?200209192251.g8JMpn1D062982>