[syslog-ng][PATCH] Unixtime Macros

Gert Menke gert@menke.za.net
Fri, 29 Mar 2002 10:29:26 +0100


--sm4nu43k4a2Rpi4c
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline

Hi!

The following patch adds the Macros $UNIXTIME, $S_UNIXTIME and $R_UNIXTIME.
They give you the time in the internal unix time format (seconds since 1970
or so). Since it is useful for me, it is probably useful for others, too.

This patch is against syslog-ng-1.5.14 with my sourceip127-patch (which I
posted on 05.Feb.2002), but will also work with the plain syslog-ng-1.5.14
sources (with an offset of -3 lines).

Balazs, please include this patch in the next release if you find it
useful.

Greetings
Gert

--sm4nu43k4a2Rpi4c
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment; filename="syslog-ng-1.5.14-unixtime.patch"

diff -r -u syslog-ng-1.5.14-p1-sourceip127/src/affile.c syslog-ng-1.5.14-p2-unixtime/src/affile.c
--- syslog-ng-1.5.14-p1-sourceip127/src/affile.c	Thu Mar 28 17:28:33 2002
+++ syslog-ng-1.5.14-p2-unixtime/src/affile.c	Thu Mar 28 17:31:19 2002
@@ -461,36 +461,39 @@
 #define M_MIN      10
 #define M_SEC      11
 #define M_WEEKDAY  12
+#define M_UNIXTIME 13
 
-#define M_DATE_RECVD     13
-#define M_FULLDATE_RECVD 14
-#define M_ISODATE_RECVD  15
-#define M_YEAR_RECVD     16
-#define M_MONTH_RECVD    17
-#define M_DAY_RECVD      18
-#define M_HOUR_RECVD     19
-#define M_MIN_RECVD      20
-#define M_SEC_RECVD      21
-#define M_WEEKDAY_RECVD  22
-
-#define M_DATE_STAMP     23
-#define M_FULLDATE_STAMP 24
-#define M_ISODATE_STAMP  25
-#define M_YEAR_STAMP     26
-#define M_MONTH_STAMP    27
-#define M_DAY_STAMP      28
-#define M_HOUR_STAMP     29
-#define M_MIN_STAMP      30
-#define M_SEC_STAMP      31
-#define M_WEEKDAY_STAMP  32
-
-#define M_FULLHOST 33
-#define M_HOST     34
-#define M_PROGRAM  35
+#define M_DATE_RECVD     14
+#define M_FULLDATE_RECVD 15
+#define M_ISODATE_RECVD  16
+#define M_YEAR_RECVD     17
+#define M_MONTH_RECVD    18
+#define M_DAY_RECVD      19
+#define M_HOUR_RECVD     20
+#define M_MIN_RECVD      21
+#define M_SEC_RECVD      22
+#define M_WEEKDAY_RECVD  23
+#define M_UNIXTIME_RECVD 24
+
+#define M_DATE_STAMP     25
+#define M_FULLDATE_STAMP 26
+#define M_ISODATE_STAMP  27
+#define M_YEAR_STAMP     28
+#define M_MONTH_STAMP    29
+#define M_DAY_STAMP      30
+#define M_HOUR_STAMP     31
+#define M_MIN_STAMP      32
+#define M_SEC_STAMP      33
+#define M_WEEKDAY_STAMP  34
+#define M_UNIXTIME_STAMP 35
+
+#define M_FULLHOST 36
+#define M_HOST     37
+#define M_PROGRAM  38
 
-#define M_MESSAGE  36
+#define M_MESSAGE  39
 
-#define M_SOURCE_IP 37
+#define M_SOURCE_IP 40
 
 int append_string(char **dest, int *left, char *str, int length, int escape)
 {
@@ -608,6 +611,7 @@
 	case M_HOUR_RECVD:
 	case M_MIN_RECVD:
 	case M_SEC_RECVD:
+	case M_UNIXTIME_RECVD:
 
 	case M_FULLDATE_STAMP:
 	case M_ISODATE_STAMP:
@@ -619,6 +623,7 @@
 	case M_HOUR_STAMP:
 	case M_MIN_STAMP:
 	case M_SEC_STAMP:
+	case M_UNIXTIME_STAMP:
 
 	case M_FULLDATE:
 	case M_ISODATE:
@@ -629,9 +634,11 @@
 	case M_DAY: 
 	case M_HOUR:
 	case M_MIN:
-	case M_SEC: {
+	case M_SEC:
+	case M_UNIXTIME: {
 		/* year, month, day */
 		struct tm *tm;
+		time_t unixtime;
 
 	       	switch(id) {
 	       	case M_FULLDATE_RECVD:
@@ -644,7 +651,8 @@
 	       	case M_HOUR_RECVD:    
 	       	case M_MIN_RECVD:     
 	       	case M_SEC_RECVD:     
-			tm = localtime(&msg->recvd);
+	       	case M_UNIXTIME_RECVD:     
+			unixtime = msg->recvd;
 			break;
 		case M_FULLDATE_STAMP:
 		case M_ISODATE_STAMP: 
@@ -656,16 +664,19 @@
 		case M_HOUR_STAMP:    
 		case M_MIN_STAMP:     
 		case M_SEC_STAMP:     
-			tm = localtime(&msg->stamp);
+		case M_UNIXTIME_STAMP:     
+			unixtime = msg->stamp;
 	                break;
 		default:
  		        if (cfg->use_time_recvd)
- 		                tm = localtime(&msg->recvd);
+				unixtime = msg->recvd;
  		        else
- 		            	tm = localtime(&msg->stamp);
+				unixtime = msg->stamp;
  		        break;
 		}
 		
+		tm = localtime(&unixtime);
+
 		switch (id) {
 		case M_WEEKDAY:
  	        case M_WEEKDAY_RECVD:
@@ -717,6 +728,11 @@
  	        case M_DATE_STAMP:
 	                length = strftime(*dest, *left - 1, "%h %e %H:%M:%S", tm);
 	                break;
+	        case M_UNIXTIME:
+ 	        case M_UNIXTIME_RECVD:
+ 	        case M_UNIXTIME_STAMP:
+			length = snprintf(*dest, *left, "%d", unixtime);
+	                break;
 		}
 		break;
 	}
@@ -751,6 +767,7 @@
 		{ "MIN", M_MIN },
 		{ "SEC", M_SEC },
 		{ "WEEKDAY", M_WEEKDAY },
+		{ "UNIXTIME", M_UNIXTIME },
 
  		{ "R_DATE", M_DATE_RECVD },
  		{ "R_FULLDATE", M_FULLDATE_RECVD },
@@ -762,6 +779,7 @@
  		{ "R_MIN", M_MIN_RECVD },
  		{ "R_SEC", M_SEC_RECVD },
  		{ "R_WEEKDAY", M_WEEKDAY_RECVD },
+		{ "R_UNIXTIME", M_UNIXTIME_RECVD },
  
  		{ "S_DATE", M_DATE_STAMP },
  		{ "S_FULLDATE", M_FULLDATE_STAMP },
@@ -773,6 +791,7 @@
  		{ "S_MIN", M_MIN_STAMP },
  		{ "S_SEC", M_SEC_STAMP },
  		{ "S_WEEKDAY", M_WEEKDAY_STAMP },
+ 		{ "S_UNIXTIME", M_UNIXTIME_STAMP },
 		
 		{ "HOST", M_HOST },
 		{ "FULLHOST", M_FULLHOST },

--sm4nu43k4a2Rpi4c--