Skip to contentSkip to conversation

logging refactoring

PATCH Superseded Colin McCabe 3 participants 10 messages Dec 8, 2009 Library code

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

cover logging refactoring

This patch series creates a common.h and populates it with some function prototypes and logging stuff.

The changes here are fairly minor and mostly consist of switching over to use the new logging macros. Probably the most helpful one is CLD_DEBUG, which only evaluates its arguments if log->verbose is set. CLD_INFO, CLD_WARN, CLD_ERR and CLD_CRIT simply call the log function with the appropriate log level, in a shorter amount of code.

I also eliminated some cases where we had to pass both a logging function and a verbosity level. We only need to pass a pointer to a cld_log struct now. This should facilitate some code sharing and hopefully improve stuff.

regards,
C.

#1 lore raw

PATCH 1/6 cld: Declare common.c functions in common.h

From: Colin McCabe <[email protected]>

---
 include/cld_msg.h |   10 ----------
 include/cldc.h    |    1 +
 include/common.h  |   11 +++++++++++
 3 files changed, 12 insertions(+), 10 deletions(-)
 create mode 100644 include/common.h
include/cld_msg.h +0 -10
index 641b857..01837b7 100644
@@ -250,14 +250,4 @@ struct cld_msg_event {
 	uint8_t			res[4];
 };
 
-/*
- * function prototypes for lib/common.c;
- * ideally these should not be in cld_msg.h
- */
-
-extern unsigned long long cld_sid2llu(const uint8_t *sid);
-extern void __cld_rand64(void *p);
-extern const char *cld_errstr(enum cle_err_codes ecode);
-extern int cld_readport(const char *fname);
-
 #endif /* __CLD_MSG_H__ */
include/cldc.h +1 -0
index 9ae6dfe..3f0deb5 100644
@@ -5,6 +5,7 @@
 #include <stdbool.h>
 #include <glib.h>
 #include <cld_msg.h>
+#include <common.h>
 
 struct cldc_session;
 
include/common.h new file +11 -0
new file mode 100644
index 0000000..52e5603
@@ -0,0 +1,11 @@
+#ifndef __CLD_COMMON_H__
+#define __CLD_COMMON_H__
+
+#include <stdint.h>
+
+unsigned long long cld_sid2llu(const uint8_t *sid);
+void __cld_rand64(void *p);
+const char *cld_errstr(enum cle_err_codes ecode);
+int cld_readport(const char *fname);
+
+#endif /* __CLD_COMMON_H__ */
Signature · 1 line
1.6.2.5
#2 lore raw

PATCH 2/6 cld: create logging macros

From: Colin McCabe <[email protected]>

---
 include/common.h |   27 +++++++++++++++++++++++++++
 1 files changed, 27 insertions(+), 0 deletions(-)
include/common.h +27 -0
index 52e5603..30610d8 100644
@@ -8,4 +8,31 @@ void __cld_rand64(void *p);
 const char *cld_errstr(enum cle_err_codes ecode);
 int cld_readport(const char *fname);
 
+/** Print out a debug message if 'verbose' is enabled */
+#define CLD_DEBUG(cld_log, ...) \
+	if ((cld_log)->verbose) { \
+		(cld_log)->func(LOG_DEBUG, __VA_ARGS__); \
+	}
+
+/** Print out an informational log message */
+#define CLD_INFO(cld_log, ...) \
+	(cld_log)->func(LOG_INFO, __VA_ARGS__);
+
+/** Print out a warning message */
+#define CLD_WARN(cld_log, ...) \
+	(cld_log)->func(LOG_WARNING, __VA_ARGS__);
+
+/** Print out an error message */
+#define CLD_ERR(cld_log, ...) \
+	(cld_log)->func(LOG_ERR, __VA_ARGS__);
+
+/** Print out a critical warning message */
+#define CLD_CRIT(cld_log, ...) \
+	(cld_log)->func(LOG_CRIT, __VA_ARGS__);
+
+struct cld_log {
+	void (*func)(int prio, const char *fmt, ...);
+	bool verbose;
+};
+
 #endif /* __CLD_COMMON_H__ */
Signature · 1 line
1.6.2.5
#3 lore raw

PATCH 3/6 cld: modify client code to use logging macros

From: Colin McCabe <[email protected]>

---
 include/cldc.h |    4 +--
 lib/cldc.c     |   98 +++++++++++++++++++++++---------------------------------
 tools/cldcli.c |    2 +-
 3 files changed, 42 insertions(+), 62 deletions(-)
include/cldc.h +1 -3
index 3f0deb5..70062cf 100644
@@ -85,10 +85,8 @@ struct cldc_ops {
 struct cldc_session {
 	uint8_t		sid[CLD_SID_SZ];	/* client id */
 
-	bool		verbose;
-
 	const struct cldc_ops *ops;
-	void		(*act_log)(int prio, const char *fmt, ...);
+	struct		cld_log log;
 	void		*private;
 
 	uint8_t		addr[64];		/* server address */
lib/cldc.c +40 -58

237 lines not on this page · raw message

tools/cldcli.c +1 -1
index 60ab301..23cba99 100644
@@ -823,7 +823,7 @@ static gpointer cld_thread(gpointer dummy)
 		return NULL;
 	}
 
-	thr_udp->sess->verbose = cldcli_verbose;
+	thr_udp->sess->log.verbose = cldcli_verbose;
 
 	pfd[0].fd = thr_udp->fd;
 	pfd[0].events = POLLIN;
Signature · 1 line
1.6.2.5
#4 lore raw

PATCH 4/6 cld: modify server code to use logging macros

From: Colin McCabe <[email protected]>

---
 server/cld.h     |    4 +-
 server/cldb.c    |    6 ++--
 server/msg.c     |   49 +++++++++++++-----------------
 server/server.c  |   86 ++++++++++++++++++++++++------------------------------
 server/session.c |   57 ++++++++++++++----------------------
 server/util.c    |   27 +++++++++--------
 6 files changed, 100 insertions(+), 129 deletions(-)
server/cld.h +2 -2
index 05c93ad..b013901 100644
@@ -26,6 +26,7 @@
 #include <glib.h>
 #include "cldb.h"
 #include <cld_msg.h>
+#include "common.h"
 #include <libtimer.h>
 
 struct client;
@@ -152,7 +153,7 @@ extern int sess_load(GHashTable *ss);
 /* server.c */
 extern const char *opstr(enum cld_msg_ops op);
 extern struct server cld_srv;
-extern int debugging;
+extern struct cld_log srv_log;
 extern struct timeval current_time;
 extern int udp_tx(int sock_fd, struct sockaddr *, socklen_t,
 	    const void *, size_t);
@@ -161,7 +162,6 @@ extern void resp_err(struct session *sess,
 	      const struct cld_msg_hdr *src, enum cle_err_codes errcode);
 extern void resp_ok(struct session *sess, const struct cld_msg_hdr *src);
 extern bool authsign(struct cld_packet *pkt, size_t pkt_len);
-extern void applog(int prio, const char *fmt, ...);
 
 /* util.c */
 extern int write_pid_file(const char *pid_fn);
server/cldb.c +3 -3
index 2f012e5..1954587 100644
@@ -241,7 +241,7 @@ int cldb_init(struct cldb *cldb, const char *db_home, const char *db_password,
 
 	rc = db_env_create(&cldb->env, 0);
 	if (rc) {
-		applog(LOG_WARNING, "cldb->env_create failed: %d", rc);
+		CLD_WARN(&srv_log, "cldb->env_create failed: %d", rc);
 		return rc;
 	}
 
@@ -375,7 +375,7 @@ static int cldb_up(struct cldb *cldb, unsigned int flags)
 
 	cldb->up = true;
 
-	applog(LOG_INFO, "databases up");
+	CLD_INFO(&srv_log, "databases up");
 	return 0;
 
 err_out_handle_idx:
@@ -417,7 +417,7 @@ void cldb_down(struct cldb *cldb)
 	cldb->inodes = NULL;
 	cldb->sessions = NULL;
 
-	applog(LOG_INFO, "databases down");
+	CLD_INFO(&srv_log, "databases down");
 }
 
 void cldb_fini(struct cldb *cldb)
server/msg.c +21 -28

113 lines not on this page · raw message

server/server.c +38 -48

199 lines not on this page · raw message

server/session.c +22 -35

121 lines not on this page · raw message

server/util.c +14 -13
index 36fa219..ffe069f 100644
@@ -47,8 +47,9 @@ int write_pid_file(const char *pid_fn)
 	fd = open(pid_fn, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR);
 	if (fd < 0) {
 		err = errno;
-		applog(LOG_ERR, "Cannot open PID file %s: %s",
-		       pid_fn, strerror(err));
+
+		CLD_ERR(&srv_log, "Cannot open PID file %s: %s",
+			pid_fn, strerror(err));
 		return -err;
 	}
 
@@ -59,11 +60,11 @@ int write_pid_file(const char *pid_fn)
 	if (fcntl(fd, F_SETLK, &lock) != 0) {
 		err = errno;
 		if (err == EAGAIN) {
-			applog(LOG_ERR, "PID file %s is already locked",
-			       pid_fn);
+			CLD_ERR(&srv_log, "PID file %s is already locked",
+				pid_fn);
 		} else {
-			applog(LOG_ERR, "Cannot lock PID file %s: %s",
-			       pid_fn, strerror(err));
+			CLD_ERR(&srv_log, "Cannot lock PID file %s: %s",
+				pid_fn, strerror(err));
 		}
 		close(fd);
 		return -err;
@@ -76,8 +77,8 @@ int write_pid_file(const char *pid_fn)
 		ssize_t rc = write(fd, s, bytes);
 		if (rc < 0) {
 			err = errno;
-			applog(LOG_ERR, "PID number write failed: %s",
-			       strerror(err));
+			CLD_ERR(&srv_log, "PID number write failed: %s",
+				strerror(err));
 			goto err_out;
 		}
 
@@ -88,7 +89,7 @@ int write_pid_file(const char *pid_fn)
 	/* make sure file data is written to disk */
 	if (fsync(fd) < 0) {
 		err = errno;
-		applog(LOG_ERR, "PID file fsync failed: %s", strerror(err));
+		CLD_ERR(&srv_log, "PID file fsync failed: %s", strerror(err));
 		goto err_out;
 	}
 
@@ -102,7 +103,7 @@ err_out:
 
 void syslogerr(const char *prefix)
 {
-	applog(LOG_ERR, "%s: %s", prefix, strerror(errno));
+	CLD_ERR(&srv_log, "%s: %s", prefix, strerror(errno));
 }
 
 int fsetflags(const char *prefix, int fd, int or_flags)
@@ -112,7 +113,7 @@ int fsetflags(const char *prefix, int fd, int or_flags)
 	/* get current flags */
 	old_flags = fcntl(fd, F_GETFL);
 	if (old_flags < 0) {
-		applog(LOG_ERR, "%s F_GETFL: %s", prefix, strerror(errno));
+		CLD_ERR(&srv_log, "%s F_GETFL: %s", prefix, strerror(errno));
 		return -errno;
 	}
 
@@ -123,8 +124,8 @@ int fsetflags(const char *prefix, int fd, int or_flags)
 	/* set new flags */
 	if (flags != old_flags)
 		if (fcntl(fd, F_SETFL, flags) < 0) {
-			applog(LOG_ERR, "%s F_SETFL: %s", prefix,
-			       strerror(errno));
+			CLD_ERR(&srv_log, "%s F_SETFL: %s", prefix,
+				strerror(errno));
 			rc = -errno;
 		}
 
Signature · 1 line
1.6.2.5
#5 lore raw

PATCH 5/6 cld: modify cld-dns to use logging macros

---
 include/cldc.h |    7 ++---
 lib/cldc-dns.c |   80 +++++++++++++++++++++-----------------------------------
 tools/cldcli.c |   12 +++++---
 3 files changed, 41 insertions(+), 58 deletions(-)
include/cldc.h +3 -4
index 70062cf..f0a1b37 100644
@@ -200,14 +200,13 @@ extern int cldc_udp_pkt_send(void *private,
 			  const void *buf, size_t buflen);
 
 /* cldc-dns */
-extern int cldc_getaddr(GList **host_list, const char *thishost, bool verbose,
-		 void (*act_log)(int prio, const char *fmt, ...));
+extern int cldc_getaddr(GList **host_list, const char *thishost,
+			struct cld_log *log);
 extern int cldc_saveaddr(struct cldc_host *hp,
 			 unsigned int priority,
 			 unsigned int weight, unsigned int port,
 			 unsigned int nlen, const char *name,
-			 bool verbose,
-			 void (*act_log)(int prio, const char *fmt, ...));
+			 struct cld_log *log);
 
 static inline bool seqid_after_eq(uint64_t a_, uint64_t b_)
 {
lib/cldc-dns.c +30 -50

190 lines not on this page · raw message

tools/cldcli.c +8 -4
index 23cba99..52ef49a 100644
@@ -97,7 +97,6 @@ struct timer {
 };
 
 static unsigned long thread_running = 1;
-static int debugging;
 static GList *host_list;
 static char clicwd[CLD_PATH_MAX + 1] = "/";
 static int to_thread[2], from_thread[2];
@@ -136,6 +135,11 @@ static void applog(int prio, const char *fmt, ...)
 	va_end(ap);
 }
 
+static struct cld_log cli_log = {
+	.func = applog,
+	.verbose = 0,
+};
+
 static gint timer_cmp(gconstpointer a_, gconstpointer b_)
 {
 	const struct timer *a = a_;
@@ -531,7 +535,7 @@ static void handle_user_command(void)
 
 	read_to_thread(&creq, sizeof(creq));
 
-	if (debugging)
+	if (cli_log.verbose)
 		switch (creq.cmd) {
 		case CREQ_CD:
 		case CREQ_CAT:
@@ -1236,7 +1240,7 @@ static error_t parse_opt (int key, char *arg, struct argp_state *state)
 	switch(key) {
 	case 'D':
 		if (atoi(arg) >= 0 && atoi(arg) <= 2)
-			debugging = atoi(arg);
+			cli_log.verbose = atoi(arg);
 		else {
 			fprintf(stderr, "invalid debug level: '%s'\n", arg);
 			argp_usage(state);
@@ -1304,7 +1308,7 @@ int main (int argc, char *argv[])
 			return 1;
 		}
 		hostb[hostsz-1] = 0;
-		if (cldc_getaddr(&host_list, hostb, debugging, applog)) {
+		if (cldc_getaddr(&host_list, hostb, &cli_log)) {
 			fprintf(stderr, "Unable to find a CLD host\n");
 			return 1;
 		}
Signature · 1 line
1.6.2.5
#6 lore raw

PATCH 6/6 cld: Tweak some log levels in cldc dns

---
 lib/cldc-dns.c |   10 +++++-----
 1 files changed, 5 insertions(+), 5 deletions(-)
lib/cldc-dns.c +5 -5
index 41bcf60..6e0aec0 100644
@@ -46,7 +46,7 @@ int cldc_saveaddr(struct cldc_host *hp,
 
 	rc = getaddrinfo(hostname, portstr, &hints, &res0);
 	if (rc) {
-		CLD_INFO(log, "getaddrinfo(%s,%s) failed: %s",
+		CLD_ERR(log, "getaddrinfo(%s,%s) failed: %s",
 		       hostname, portstr, gai_strerror(rc));
 		rc = -EINVAL;
 		goto err_addr;
@@ -65,7 +65,7 @@ int cldc_saveaddr(struct cldc_host *hp,
 	}
 
 	if (!something_suitable) {
-		CLD_INFO(log, "Host %s port %u has no addresses",
+		CLD_ERR(log, "Host %s port %u has no addresses",
 		       hostname, port);
 		rc = -EINVAL;
 		goto err_suitable;
@@ -196,8 +196,8 @@ do_try_again:
 			/* fall through */
 		case NO_RECOVERY:
 		default:
-			CLD_INFO(log, "cldc_getaddr: res_search error (%d): %s",
-				h_errno, hstrerror(h_errno));
+			CLD_ERR(log, "cldc_getaddr: res_search error "
+				"(%d): %s", h_errno, hstrerror(h_errno));
 			return -1;
 		}
 	}
@@ -210,7 +210,7 @@ do_try_again:
 	}
 
 	if (ns_initparse(resp, rlen, &nsb) < 0) {
-		CLD_INFO(log, "cldc_getaddr: ns_initparse error");
+		CLD_ERR(log, "cldc_getaddr: ns_initparse error");
 		return -1;
 	}
 
Signature · 1 line
1.6.2.5
#7 lore raw

Re: [PATCH 4/6] cld: modify server code to use logging macros

In reply to Colin McCabe

server/cld.h line 30

Quoted 7 lines · On Mon, 7 Dec 2009 16:43:02 -0800 Colin McCabe <[email protected]> wrote:

+++ b/server/cld.h

server/cld.h +1 -0
@@ -26,6 +26,7 @@
 #include <glib.h>
 #include "cldb.h"
 #include <cld_msg.h>
+#include "common.h"
 #include <libtimer.h>

This should be <common.h> since it's common (in include).

server/cldb.c void cldb_down(struct cldb *cldb) · line 420

Quoted 5 lines
server/cldb.c +2 -2
-extern int debugging;
+extern struct cld_log srv_log;
 
-	applog(LOG_INFO, "databases down");
+	CLD_INFO(&srv_log, "databases down");

Great, you took code that was common across Hail and made it specific to CLD again.

-- Pete

#8 lore raw

Re: [PATCH 4/6] cld: modify server code to use logging macros

Quoted 12 lines · Pete Zaitcev

server/cld.h line 30

Quoted 7 lines · On Mon,  7 Dec 2009 16:43:02 -0800 Colin McCabe <[email protected]> wrote:

+++ b/server/cld.h

server/cld.h +1 -0
@@ -26,6 +26,7 @@
  #include <glib.h>
  #include "cldb.h"
  #include <cld_msg.h>
+#include "common.h"
  #include <libtimer.h>

This should be <common.h> since it's common (in include).

Sure

Quoted 9 lines

server/cldb.c void cldb_down(struct cldb *cldb) · line 420

Quoted 6 lines
server/cldb.c +2 -2
-extern int debugging;
+extern struct cld_log srv_log;
 
-     applog(LOG_INFO, "databases down");
+     CLD_INFO(&srv_log, "databases down");

Great, you took code that was common across Hail and made it specific to CLD again.

I guess I could name the log messages HAIL_INFO, etc. I don't know if that would be an improvement or not.

Colin

Quoted 7 lines

-- Pete
--
To unsubscribe from this list: send the line "unsubscribe hail-devel" in the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html

#9 lore raw

Re: [PATCH 4/6] cld: modify server code to use logging macros

In reply to Pete Zaitcev
Quoted 13 lines · Pete Zaitcev

Review line 30

Quoted 7 lines · On Mon, 7 Dec 2009 16:43:02 -0800 Colin McCabe<[email protected]> wrote:

+++ b/server/cld.h

(quoted diff) +1 -0
@@ -26,6 +26,7 @@
  #include<glib.h>
  #include "cldb.h"
  #include<cld_msg.h>
+#include "common.h"
  #include<libtimer.h>

This should be<common.h> since it's common (in include).

Quoted 8 lines

server/cldb.c void cldb_down(struct cldb *cldb) · line 420

Quoted 5 lines
server/cldb.c +2 -2
-extern int debugging;
+extern struct cld_log srv_log;
 
-	applog(LOG_INFO, "databases down");
+	CLD_INFO(&srv_log, "databases down");

Great, you took code that was common across Hail and made it specific to CLD again.

A fair point... we did just finish changing from cldlog() to applog()

Jeff