changeset 134:e0d56e9be8a2

rvtdump: time-stamped logging implemented
author Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
date Sat, 02 Nov 2013 23:15:42 +0000
parents 56b53c289785
children e4257294102b
files rvinterf/Makefile rvinterf/log.c rvinterf/rvtdump.c
diffstat 3 files changed, 55 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/rvinterf/Makefile	Sat Nov 02 22:24:14 2013 +0000
+++ b/rvinterf/Makefile	Sat Nov 02 23:15:42 2013 +0000
@@ -3,7 +3,7 @@
 PROGS=	rvtdump
 INSTBIN=/usr/local/bin
 
-RVTDUMP_OBJS=	openport.o packetrx.o rvtdump.o trdump.o
+RVTDUMP_OBJS=	log.o openport.o packetrx.o rvtdump.o trdump.o
 
 all:	${PROGS}
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/rvinterf/log.c	Sat Nov 02 23:15:42 2013 +0000
@@ -0,0 +1,33 @@
+/*
+ * This module implements the logging function
+ */
+
+#include <sys/types.h>
+#include <stdio.h>
+#include <string.h>
+#include <strings.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <time.h>
+
+extern char pr_item[];
+
+extern FILE *logF;
+extern time_t logtime;
+
+static struct tm last_tm;
+
+log_item()
+{
+	struct tm *curtm;
+
+	curtm = gmtime(&logtime);
+	if (curtm->tm_year != last_tm.tm_year ||
+	    curtm->tm_mon != last_tm.tm_mon ||
+	    curtm->tm_mday != last_tm.tm_mday)
+		fprintf(logF, "%d-%02d-%02d (gmtime):\n", curtm->tm_year + 1900,
+			curtm->tm_mon+1, curtm->tm_mday);
+	fprintf(logF, "[%02d:%02d:%02d] %s\n", curtm->tm_hour, curtm->tm_min,
+		curtm->tm_sec, pr_item);
+	bcopy(curtm, &last_tm, sizeof(struct tm));
+}
--- a/rvinterf/rvtdump.c	Sat Nov 02 22:24:14 2013 +0000
+++ b/rvinterf/rvtdump.c	Sat Nov 02 23:15:42 2013 +0000
@@ -8,12 +8,17 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <unistd.h>
+#include <time.h>
 
 extern int target_fd;
 extern char *baudrate_name;
 
 extern char pr_item[];
 
+char *logfname;
+FILE *logF;
+time_t logtime;
+
 main(argc, argv)
 	char **argv;
 {
@@ -22,7 +27,7 @@
 	int c;
 	fd_set fds;
 
-	while ((c = getopt(argc, argv, "b:d:")) != EOF)
+	while ((c = getopt(argc, argv, "b:d:l:")) != EOF)
 		switch (c) {
 		case 'b':
 			baudrate_name = optarg;
@@ -30,6 +35,9 @@
 		case 'd':
 			target_fd = atoi(optarg);
 			continue;
+		case 'l':
+			logfname = optarg;
+			continue;
 		case '?':
 		default:
 usage:			fprintf(stderr,
@@ -44,10 +52,20 @@
 
 	set_serial_nonblock(0);
 	setlinebuf(stdout);
+	if (logfname) {
+		logF = fopen(logfname, "w");
+		if (!logF) {
+			perror(logfname);
+			exit(1);
+		}
+		fprintf(logF, "*** Log of decoded RVT output ***\n");
+		setlinebuf(logF);
+	}
 	for (;;) {
 		FD_ZERO(&fds);
 		FD_SET(target_fd, &fds);
 		c = select(target_fd+1, &fds, 0, 0, 0);
+		time(&logtime);
 		if (c < 0) {
 			if (errno == EINTR)
 				continue;
@@ -67,4 +85,6 @@
 print_item()
 {
 	printf("%s\n", pr_item);
+	if (logF)
+		log_item();
 }