git » nmdb » commit f2fc630

Implement a simple logging facility.

author Alberto Bertogli
2007-08-08 21:02:37 UTC
committer Alberto Bertogli
2007-08-08 21:02:37 UTC
parent 9876997b840e93e6092a716557197f1fb4101eec

Implement a simple logging facility.

It supports a normal log (wlog(), because log() was already taken), and
an error log like perror() called errlog().

Signed-off-by: Alberto Bertogli <albertito@gmail.com>

nmdb/Makefile +1 -1
nmdb/common.h +1 -0
nmdb/log.c +57 -0
nmdb/log.h +18 -0
nmdb/main.c +19 -5
nmdb/net.c +6 -6

diff --git a/nmdb/Makefile b/nmdb/Makefile
index a0fd3b0..25867ee 100644
--- a/nmdb/Makefile
+++ b/nmdb/Makefile
@@ -29,7 +29,7 @@ endif
 PREFIX=/usr/local
 
 
-OBJS = cache.o dbloop.o queue.o net.o parse.o main.o
+OBJS = cache.o dbloop.o queue.o log.o net.o parse.o main.o
 LIBS = -levent -lpthread -lrt
 
 
diff --git a/nmdb/common.h b/nmdb/common.h
index 2d6cf56..70ce08f 100644
--- a/nmdb/common.h
+++ b/nmdb/common.h
@@ -24,6 +24,7 @@ struct settings {
 	int foreground;
 	int passive;
 	char *dbname;
+	char *logfname;
 };
 extern struct settings settings;
 
diff --git a/nmdb/log.c b/nmdb/log.c
new file mode 100644
index 0000000..a5c0725
--- /dev/null
+++ b/nmdb/log.c
@@ -0,0 +1,57 @@
+
+#include <stdio.h>		/* vsprintf() */
+#include <stdarg.h>
+#include <sys/types.h> 		/* open() */
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <unistd.h> 		/* write() */
+#include <string.h>		/* strcmp(), strerror() */
+#include <errno.h>		/* errno */
+
+#include "log.h"
+#include "common.h"
+
+
+/* Logging file descriptor, -1 if logging is disabled */
+int logfd = -1;
+
+
+int log_init(void)
+{
+	if (settings.logfname == NULL) {
+		logfd = -1;
+		return 1;
+	}
+
+	if (strcmp(settings.logfname, "-") == 0) {
+		logfd = 0;
+	} else {
+		logfd = open(settings.logfname, O_WRONLY | O_APPEND | O_CREAT);
+		if (logfd < 0)
+			return 0;
+	}
+
+	return 1;
+}
+
+void wlog(const char *fmt, ...)
+{
+	int r;
+	va_list ap;
+	char str[MAX_LOG_STR];
+
+	if (logfd == -1)
+		return;
+
+	va_start(ap, fmt);
+	r = vsnprintf(str, MAX_LOG_STR, fmt, ap);
+	va_end(ap);
+
+	write(logfd, str, r);
+}
+
+void errlog(const char *s)
+{
+	wlog("%s: %s\n", s, strerror(errno));
+}
+
diff --git a/nmdb/log.h b/nmdb/log.h
new file mode 100644
index 0000000..b187cc0
--- /dev/null
+++ b/nmdb/log.h
@@ -0,0 +1,18 @@
+
+#ifndef _LOG_H
+#define _LOG_H
+
+/* Maximum string to log */
+#define MAX_LOG_STR 512
+
+int log_init(void);
+
+/* Normal logging, printf()-alike */
+void wlog(const char *fmt, ...);
+
+/* Errno logging, perror()-alike */
+void errlog(const char *s);
+
+#endif
+
+
diff --git a/nmdb/main.c b/nmdb/main.c
index 3980974..65bd70b 100644
--- a/nmdb/main.c
+++ b/nmdb/main.c
@@ -11,6 +11,7 @@
 #include "dbloop.h"
 #include "common.h"
 #include "net-const.h"
+#include "log.h"
 
 #define DEFDBNAME "database"
 
@@ -33,6 +34,7 @@ static void help(void) {
 	  "  -u port	UDP listening port (26010)\n"
 	  "  -U addr	UDP listening address (all local addresses)\n"
 	  "  -c nobj	max. number of objects to be cached, in thousands (128)\n"
+	  "  -o	fname	log to the file 'fname'.\n"
 	  "  -f		don't fork and stay in the foreground\n"
 	  "  -p		enable passive mode, for redundancy purposes (read docs.)\n"
 	  "  -h		show this help\n"
@@ -56,11 +58,12 @@ static int load_settings(int argc, char **argv)
 	settings.numobjs = -1;
 	settings.foreground = 0;
 	settings.passive = 0;
+	settings.logfname = NULL;
 
 	settings.dbname = malloc(strlen(DEFDBNAME) + 1);
 	strcpy(settings.dbname, DEFDBNAME);
 
-	while ((c = getopt(argc, argv, "d:l:L:t:T:u:U:c:fph?")) != -1) {
+	while ((c = getopt(argc, argv, "d:l:L:t:T:u:U:c:o:fph?")) != -1) {
 		switch(c) {
 		case 'd':
 			free(settings.dbname);
@@ -92,6 +95,12 @@ static int load_settings(int argc, char **argv)
 		case 'c':
 			settings.numobjs = atoi(optarg) * 1024;
 			break;
+
+		case 'o':
+			settings.logfname = malloc(strlen(optarg) + 1);
+			strcpy(settings.logfname, optarg);
+			break;
+
 		case 'f':
 			settings.foreground = 1;
 			break;
@@ -148,25 +157,30 @@ int main(int argc, char **argv)
 	if (!load_settings(argc, argv))
 		return 1;
 
+	if (!log_init()) {
+		perror("Error opening log file");
+		return 1;
+	}
+
 	init_stats();
 
 	cd = cache_create(settings.numobjs, 0);
 	if (cd == NULL) {
-		perror("Error creating cache");
+		errlog("Error creating cache");
 		return 1;
 	}
 	cache_table = cd;
 
 	q = queue_create();
 	if (q == NULL) {
-		perror("Error creating queue");
+		errlog("Error creating queue");
 		return 1;
 	}
 	op_queue = q;
 
 	db = db_open(settings.dbname, 0);
 	if (db == NULL) {
-		perror("Error opening DB");
+		errlog("Error opening DB");
 		return 1;
 	}
 
@@ -176,7 +190,7 @@ int main(int argc, char **argv)
 			/* parent exits */
 			return 0;
 		} else if (pid < 0) {
-			perror("Error in fork()");
+			errlog("Error in fork()");
 			return 1;
 		}
 
diff --git a/nmdb/net.c b/nmdb/net.c
index a376c17..821ccb9 100644
--- a/nmdb/net.c
+++ b/nmdb/net.c
@@ -1,6 +1,5 @@
 
 #include <signal.h>		/* signal constants */
-#include <stdio.h>		/* perror() */
 #include <stdlib.h>		/* exit() */
 
 /* Workaround for libevent 1.1a: the header assumes u_char is typedef'ed to an
@@ -14,17 +13,18 @@ typedef unsigned char u_char;
 #include "tcp.h"
 #include "udp.h"
 #include "net.h"
+#include "log.h"
 
 
 static void exit_sighandler(int fd, short event, void *arg)
 {
-	printf("Got signal! Puf!\n");
+	wlog("Got signal! Puf!\n");
 	event_loopexit(NULL);
 }
 
 static void passive_to_active_sighandler(int fd, short event, void *arg)
 {
-	printf("Passive toggle!\n");
+	wlog("Passive toggle!\n");
 	settings.passive = !settings.passive;
 }
 
@@ -44,7 +44,7 @@ void net_loop(void)
 	if (ENABLE_TIPC) {
 		tipc_fd = tipc_init();
 		if (tipc_fd < 0) {
-			perror("Error initializing TIPC");
+			errlog("Error initializing TIPC");
 			exit(1);
 		}
 
@@ -56,7 +56,7 @@ void net_loop(void)
 	if (ENABLE_TCP) {
 		tcp_fd = tcp_init();
 		if (tcp_fd < 0) {
-			perror("Error initializing TCP");
+			errlog("Error initializing TCP");
 			exit(1);
 		}
 
@@ -68,7 +68,7 @@ void net_loop(void)
 	if (ENABLE_UDP) {
 		udp_fd = udp_init();
 		if (udp_fd < 0) {
-			perror("Error initializing UDP");
+			errlog("Error initializing UDP");
 			exit(1);
 		}