Skip to contentSkip to conversation

logging refactoring

PATCH Applied Colin McCabe 2 participants 18 messages Dec 15, 2009 Library code

lore
Share & tools
lore
#0 lore raw

v3 cover logging refactoring

This patch series creates some logging macros in hail_log.h and modifies cld to use them. It also converts chunkd and tabled to use the new cld API.

version 3:
* move logging macros out of common.h and into hail_log.h
* add patches for chunkd and tabled

version 2:
* Rename CLD_DEBUG, etc. to HAIL_DEBUG
* use angle brackets for including common.h from include/

cheers.
Colin

#1 lore raw

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

---
 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
Applied
#2 lore raw

Re: [PATCH 1/8 v3] cld: Declare common.c functions in common.h

Quoted 1 line · Colin McCabe

applied, after fixing two problems:

* did not survive 'make distcheck'. common.h was needed in include/Makefile.am.
* introduced build warnings in the server. added #include in server/cld.h.

Each patch should be internally consistent, and needs to survive 'make distcheck' and not introduce new build warnings (or errors!).

I also renamed common.h to cld_common.h, but that is a matter of taste, not a problem with your patch :)

#3 lore raw

PATCH v3 2/8 cld: create logging macros

In reply to Colin McCabe
---
 include/hail_log.h |   31 +++++++++++++++++++++++++++++++
 1 files changed, 31 insertions(+), 0 deletions(-)
 create mode 100644 include/hail_log.h
include/hail_log.h new file +31 -0
new file mode 100644
index 0000000..f825b89
@@ -0,0 +1,31 @@
+#ifndef __HAIL_LOG_H__
+#define __HAIL_LOG_H__
+
+struct hail_log {
+	void (*func)(int prio, const char *fmt, ...);
+	bool verbose;
+};
+
+/** Print out a debug message if 'verbose' is enabled */
+#define HAIL_DEBUG(log, ...) \
+	if ((log)->verbose) { \
+		(log)->func(LOG_DEBUG, __VA_ARGS__); \
+	}
+
+/** Print out an informational log message */
+#define HAIL_INFO(log, ...) \
+	(log)->func(LOG_INFO, __VA_ARGS__);
+
+/** Print out a warning message */
+#define HAIL_WARN(log, ...) \
+	(log)->func(LOG_WARNING, __VA_ARGS__);
+
+/** Print out an error message */
+#define HAIL_ERR(log, ...) \
+	(log)->func(LOG_ERR, __VA_ARGS__);
+
+/** Print out a critical warning message */
+#define HAIL_CRIT(log, ...) \
+	(log)->func(LOG_CRIT, __VA_ARGS__);
+
+#endif /* __HAIL_LOG_H__ */
Signature · 1 line
1.6.2.5
Applied
#4 lore raw

Re: [PATCH 2/8 v3] cld: create logging macros

Quoted 1 line · Colin McCabe

applied, after fixing two minor problems:

* added to include/Makefile.am
* #include'd stdbool.h

#5 lore raw

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

In reply to Colin McCabe
---
 include/cldc.h |    5 +--
 lib/cldc.c     |   98 +++++++++++++++++++++++---------------------------------
 tools/cldcli.c |    2 +-
 3 files changed, 43 insertions(+), 62 deletions(-)
include/cldc.h +2 -3
index 3f0deb5..1382a23 100644
@@ -6,6 +6,7 @@
 #include <glib.h>
 #include <cld_msg.h>
 #include <common.h>
+#include <hail_log.h>
 
 struct cldc_session;
 
@@ -85,10 +86,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		hail_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
Applied
#6 lore raw

Re: [PATCH 3/8 v3] cld: modify client code to use logging macros

Quoted 6 lines · Colin McCabe
---
  include/cldc.h |    5 +--
  lib/cldc.c     |   98 +++++++++++++++++++++++---------------------------------
  tools/cldcli.c |    2 +-
  3 files changed, 43 insertions(+), 62 deletions(-)

applied

#7 lore raw

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

In reply to Colin McCabe
---
 server/cld.h     |    4 +-
 server/cldb.c    |    7 ++--
 server/msg.c     |   50 +++++++++++++-----------------
 server/server.c  |   87 ++++++++++++++++++++++++-----------------------------
 server/session.c |   58 ++++++++++++++---------------------
 server/util.c    |   28 +++++++++--------
 6 files changed, 105 insertions(+), 129 deletions(-)
server/cld.h +2 -2
index 05c93ad..efe7ee4 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 hail_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 +4 -3
index 2f012e5..ca5d849 100644
@@ -25,6 +25,7 @@
 #include <glib.h>
 #include <cld-private.h>
 #include "cld.h"
+#include <hail_log.h>
 
 static int cldb_up(struct cldb *cldb, unsigned int flags);
 
@@ -241,7 +242,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);
+		HAIL_WARN(&srv_log, "cldb->env_create failed: %d", rc);
 		return rc;
 	}
 
@@ -375,7 +376,7 @@ static int cldb_up(struct cldb *cldb, unsigned int flags)
 
 	cldb->up = true;
 
-	applog(LOG_INFO, "databases up");
+	HAIL_INFO(&srv_log, "databases up");
 	return 0;
 
 err_out_handle_idx:
@@ -417,7 +418,7 @@ void cldb_down(struct cldb *cldb)
 	cldb->inodes = NULL;
 	cldb->sessions = NULL;
 
-	applog(LOG_INFO, "databases down");
+	HAIL_INFO(&srv_log, "databases down");
 }
 
 void cldb_fini(struct cldb *cldb)
server/msg.c +22 -28

120 lines not on this page · raw message

server/server.c +39 -48

200 lines not on this page · raw message

server/session.c +23 -35

128 lines not on this page · raw message

server/util.c +15 -13
index 36fa219..9fcfe80 100644
@@ -31,6 +31,7 @@
 #include <stdlib.h>
 #include <syslog.h>
 #include "cld.h"
+#include <hail_log.h>
 
 int write_pid_file(const char *pid_fn)
 {
@@ -47,8 +48,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));
+
+		HAIL_ERR(&srv_log, "Cannot open PID file %s: %s",
+			pid_fn, strerror(err));
 		return -err;
 	}
 
@@ -59,11 +61,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);
+			HAIL_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));
+			HAIL_ERR(&srv_log, "Cannot lock PID file %s: %s",
+				pid_fn, strerror(err));
 		}
 		close(fd);
 		return -err;
@@ -76,8 +78,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));
+			HAIL_ERR(&srv_log, "PID number write failed: %s",
+				strerror(err));
 			goto err_out;
 		}
 
@@ -88,7 +90,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));
+		HAIL_ERR(&srv_log, "PID file fsync failed: %s", strerror(err));
 		goto err_out;
 	}
 
@@ -102,7 +104,7 @@ err_out:
 
 void syslogerr(const char *prefix)
 {
-	applog(LOG_ERR, "%s: %s", prefix, strerror(errno));
+	HAIL_ERR(&srv_log, "%s: %s", prefix, strerror(errno));
 }
 
 int fsetflags(const char *prefix, int fd, int or_flags)
@@ -112,7 +114,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));
+		HAIL_ERR(&srv_log, "%s F_GETFL: %s", prefix, strerror(errno));
 		return -errno;
 	}
 
@@ -123,8 +125,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));
+			HAIL_ERR(&srv_log, "%s F_SETFL: %s", prefix,
+				strerror(errno));
 			rc = -errno;
 		}
 
Signature · 1 line
1.6.2.5
Applied
#8 lore raw

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

Quoted 1 line · Colin McCabe

applied, with minor changes:

* include hail_log.h from server/cld.h, the header file common to all server code. remove individual #include's in each .c file.

* fixed up #include of jgarzik-renamed common.h (to cld_commmon.h)

#9 lore raw

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

In reply to Colin McCabe
---
 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 1382a23..5419cc8 100644
@@ -201,14 +201,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 hail_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 hail_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..8eeee72 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 hail_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
#10 lore raw

Re: [PATCH 5/8 v3] cld: modify cld-dns to use logging macros

Quoted 6 lines · Colin McCabe
---
  include/cldc.h |    7 ++---
  lib/cldc-dns.c |   80 +++++++++++++++++++++-----------------------------------
  tools/cldcli.c |   12 +++++---
  3 files changed, 41 insertions(+), 58 deletions(-)

applied 5-6

#11 lore raw

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

In reply to Colin McCabe
---
 lib/cldc-dns.c |   10 +++++-----
 1 files changed, 5 insertions(+), 5 deletions(-)
lib/cldc-dns.c +5 -5
index d58a8ba..1bf6355 100644
@@ -46,7 +46,7 @@ int cldc_saveaddr(struct cldc_host *hp,
 
 	rc = getaddrinfo(hostname, portstr, &hints, &res0);
 	if (rc) {
-		HAIL_INFO(log, "getaddrinfo(%s,%s) failed: %s",
+		HAIL_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) {
-		HAIL_INFO(log, "Host %s port %u has no addresses",
+		HAIL_ERR(log, "Host %s port %u has no addresses",
 		       hostname, port);
 		rc = -EINVAL;
 		goto err_suitable;
@@ -195,8 +195,8 @@ do_try_again:
 			/* fall through */
 		case NO_RECOVERY:
 		default:
-			HAIL_INFO(log, "cldc_getaddr: res_search error (%d): %s",
-				h_errno, hstrerror(h_errno));
+			HAIL_ERR(log, "cldc_getaddr: res_search error "
+				"(%d): %s", h_errno, hstrerror(h_errno));
 			return -1;
 		}
 	}
@@ -209,7 +209,7 @@ do_try_again:
 	}
 
 	if (ns_initparse(resp, rlen, &nsb) < 0) {
-		HAIL_INFO(log, "cldc_getaddr: ns_initparse error");
+		HAIL_ERR(log, "cldc_getaddr: ns_initparse error");
 		return -1;
 	}
 
Signature · 1 line
1.6.2.5
#12 lore raw

PATCH v3 7/8 chunkd: Use hail_log API to call libcldc

In reply to Colin McCabe
---
 server/cldu.c |   12 ++++++++++--
 1 files changed, 10 insertions(+), 2 deletions(-)
server/cldu.c +10 -2
index 0cf687f..da5f55f 100644
@@ -615,8 +615,12 @@ int cld_begin(const char *thishost, const char *thiscell, uint32_t nid,
 	if (!ses.forced_hosts) {
 		GList *tmp, *host_list = NULL;
 		int i;
+		struct hail_log hail_log = {
+			.func = applog,
+			.verbose = debugging,
+		};
 
-		if (cldc_getaddr(&host_list, thishost, debugging, applog)) {
+		if (cldc_getaddr(&host_list, thishost, &hail_log)) {
 			/* Already logged error */
 			goto err_addr;
 		}
@@ -692,6 +696,10 @@ void cldu_add_host(const char *hostname, unsigned int port)
 	static struct cld_session *sp = &ses;
 	struct cld_host *hp;
 	int i;
+	struct hail_log hail_log = {
+		.func = applog,
+		.verbose = debugging,
+	};
 
 	for (i = 0; i < N_CLD; i++) {
 		hp = &sp->cldv[i];
@@ -702,7 +710,7 @@ void cldu_add_host(const char *hostname, unsigned int port)
 		return;
 
 	if (cldc_saveaddr(&hp->h, 100, 100, port, strlen(hostname), hostname,
-			  debugging, applog))
+			  &hail_log))
 		return;
 	hp->known = 1;
 
Signature · 1 line
1.6.2.5
#14 lore raw

PATCH v3 8/8 tabled: Use hail_log API to call libcldc

In reply to Colin McCabe
---
 server/cldu.c   |   14 ++++++++++++--
 server/tabled.h |    1 +
 2 files changed, 13 insertions(+), 2 deletions(-)
server/cldu.c +12 -2
index e66ec02..e256e6a 100644
@@ -918,6 +918,11 @@ int cld_begin(const char *thishost, const char *thiscell)
 {
 	static struct cld_session *sp = &ses;
 
+	const struct hail_log hail_log = {
+		.func = applog,
+		.verbose = debugging,
+	};
+
 	evtimer_set(&ses.tm_retry, cldu_tm_retry, &ses);
 	evtimer_set(&ses.tm_rescan, cldu_tm_rescan, &ses);
 	evtimer_set(&ses.tm_reopen, cldu_tm_reopen, &ses);
@@ -931,7 +936,7 @@ int cld_begin(const char *thishost, const char *thiscell)
 		GList *tmp, *host_list = NULL;
 		int i;
 
-		if (cldc_getaddr(&host_list, thishost, debugging, applog)) {
+		if (cldc_getaddr(&host_list, thishost, &hail_log)) {
 			/* Already logged error */
 			goto err_addr;
 		}
@@ -981,6 +986,11 @@ void cldu_add_host(const char *hostname, unsigned int port)
 	struct cld_host *hp;
 	int i;
 
+	const struct hail_log hail_log = {
+		.func = applog,
+		.verbose = debugging,
+	};
+
 	for (i = 0; i < N_CLD; i++) {
 		hp = &sp->cldv[i];
 		if (!hp->known)
@@ -990,7 +1000,7 @@ void cldu_add_host(const char *hostname, unsigned int port)
 		return;
 
 	if (cldc_saveaddr(&hp->h, 100, 100, port, strlen(hostname), hostname,
-			  debugging, applog))
+			  &hail_log))
 		return;
 	hp->known = 1;
 
server/tabled.h +1 -0
index fd6142e..cc93d3c 100644
@@ -313,6 +313,7 @@ extern bool cli_write_start(struct client *cli);
 extern int cli_req_avail(struct client *cli);
 extern void applog(int prio, const char *fmt, ...);
 extern int stor_update_cb(void);
+extern const struct hail_log hail_log;
 
 /* config.c */
 extern void read_config(void);
Signature · 1 line
1.6.2.5
#15 lore raw

Re: [PATCH 8/8 v3] tabled: Use hail_log API to call libcldc

Quoted 5 lines · Colin McCabe
---
  server/cldu.c   |   14 ++++++++++++--
  server/tabled.h |    1 +
  2 files changed, 13 insertions(+), 2 deletions(-)

ditto last message, for tabled

#16 lore raw

[PATCH 9/8 v3] cld: Export hail_log.h to the include directory

In reply to Colin McCabe

I almost forgot-- Makefile.am needs to change.

---
 include/Makefile.am |    4 ++--
 1 files changed, 2 insertions(+), 2 deletions(-)
include/Makefile.am +2 -2
index f7539dd..4ea11c4 100644
@@ -1,5 +1,5 @@
 
-EXTRA_DIST = cld-private.h libtimer.h
+EXTRA_DIST = cld-private.h cld_msg.h libtimer.h
 
-include_HEADERS = cldc.h cld_msg.h
+include_HEADERS = cldc.h hail_log.h
 
Signature · 1 line
1.6.2.5
#17 lore raw

Re: [PATCH 9/8 v3] cld: Export hail_log.h to the include directory

Quoted 12 lines · Colin McCabe

I almost forgot-- Makefile.am needs to change.

---
  include/Makefile.am |    4 ++--
  1 files changed, 2 insertions(+), 2 deletions(-)
include/Makefile.am +0 -0
index f7539dd..4ea11c4 100644

include/Makefile.am line 4

Quoted 6 lines
include/Makefile.am +2 -2
@@ -1,5 +1,5 @@
 
-EXTRA_DIST = cld-private.h libtimer.h
+EXTRA_DIST = cld-private.h cld_msg.h libtimer.h
 
-include_HEADERS = cldc.h cld_msg.h
+include_HEADERS = cldc.h hail_log.h

skipped -- I integrated this into previous changes.

Also, you moved cld_msg.h out of /usr/include and into EXTRA_DIST, which breaks stuff because cldc.h #include's it. include_HEADERS is the list of installed headers, and we definitely still need to install cld_msg.h.