Skip to contentSkip to conversation

cld: replace "if (verbose) { act_log }" with CLD_DEBUG

RFC In review Colin McCabe 3 participants 4 messages Dec 6, 2009 – Dec 7, 2009 Library code

lore
Share & tools
lore
#0 lore raw

RFC v2 1/1 cld: replace "if (verbose) { act_log }" with CLD_DEBUG

Move prototypes for common.c functions out of cld_msg.h and into a new header file, common.h. Create a structure that represents the current log level and also the function to use for logging.

Create CLD_DEBUG and CLD_LOG macros to print debugging and informational log messages, respectively. CLD_DEBUG will not evaluate its parameters unless log->verbose is true. This patch converts over the client code to use these macros.

Rationale:
1. Some functions in common.c can be called from the server _and_ the client code. If these functions want to print a debug message, they would currently need two extra parameters-- the logging function, and the verbose flag. It's cleaner to condense this into one parameter.

2. The if (verbose) sess->act_log pattern leads to an excessive level of indentation, which tends to make things more unclear.

version 2: add common.h

---
 include/cld_msg.h |   10 -----
 include/cldc.h    |    5 +--
 include/common.h  |   27 ++++++++++++++
 lib/cldc.c        |   98 +++++++++++++++++++++-------------------------------
 tools/cldcli.c    |    2 +-
 5 files changed, 70 insertions(+), 72 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 +2 -3
index 9ae6dfe..70062cf 100644
@@ -5,6 +5,7 @@
 #include <stdbool.h>
 #include <glib.h>
 #include <cld_msg.h>
+#include <common.h>
 
 struct cldc_session;
 
@@ -84,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 */
include/common.h new file +27 -0
new file mode 100644
index 0000000..c98f4ab
@@ -0,0 +1,27 @@
+#ifndef __CLD_COMMON_H__
+#define __CLD_COMMON_H__
+
+#include <stdint.h>
+#include <glib.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);
+
+/** Print out a debug message if 'verbose' is enabled */
+#define CLD_DEBUG(cld_log, ...) \
+	if ((cld_log).verbose) { \
+		(cld_log).fun(LOG_DEBUG, __VA_ARGS__); \
+	}
+
+/** Print out an informational log message */
+#define CLD_LOG(cld_log, ...) \
+	(cld_log).fun(LOG_INFO, __VA_ARGS__);
+
+struct cld_log {
+	void (*fun)(int prio, const char *fmt, ...);
+	bool verbose;
+};
+
+#endif /* __CLD_COMMON_H__ */
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
#1 lore raw

lib/cldc.c static void sess_expire(struct cldc_session *sess) · line 652

Quoted 12 lines · On Sun, 6 Dec 2009 07:41:05 -0800 Colin McCabe <[email protected]> wrote:

Move prototypes for common.c functions out of cld_msg.h and into a new header file, common.h. Create a structure that represents the current log level and also the function to use for logging.

static int sess_send_pkt(struct cldc_session *sess,

lib/cldc.c +1 -1
 			 const struct cld_packet *pkt, size_t pkt_len)
 {
-	if (sess->verbose) {
+	if (sess->log.verbose) {
 		uint32_t flags = le32_to_cpu(pkt->flags);
 		bool first = (flags & CPF_FIRST);
 		bool last = (flags & CPF_LAST);

I'm wondering if we can drop these ifs now, at the cost of some small inefficiency.

-- Pete

#2 lore raw
Quoted 7 lines · Pete Zaitcev
Quoted 3 lines · On Sun, 6 Dec 2009 07:41:05 -0800 Colin McCabe<[email protected]> wrote:

Move prototypes for common.c functions out of cld_msg.h and into a new header file, common.h. Create a structure that represents the current log level and also the function to use for logging.

Quoted 11 lines

lib/cldc.c static void sess_expire(struct cldc_session *sess)

Quoted 8 lines

static int sess_send_pkt(struct cldc_session *sess,
const struct cld_packet *pkt, size_t pkt_len)
{

lib/cldc.c +1 -1
-	if (sess->verbose) {
+	if (sess->log.verbose) {
  		uint32_t flags = le32_to_cpu(pkt->flags);
  		bool first = (flags&  CPF_FIRST);
  		bool last = (flags&  CPF_LAST);

I'm wondering if we can drop these ifs now, at the cost of some small inefficiency.

I would rather keep the if's. The verbose logging is unlikely to be used in production. Therefore, the common case would be to execute unnecessary code for each packet.

Jeff

#3 lore raw
In reply to Colin McCabe
Quoted 93 lines · Colin McCabe

Move prototypes for common.c functions out of cld_msg.h and into a new header file, common.h. Create a structure that represents the current log level and also the function to use for logging.

Create CLD_DEBUG and CLD_LOG macros to print debugging and informational log messages, respectively. CLD_DEBUG will not evaluate its parameters unless log->verbose is true. This patch converts over the client code to use these macros.

Rationale:
1. Some functions in common.c can be called from the server _and_ the client code. If these functions want to print a debug message, they would currently need two extra parameters-- the logging function, and the verbose flag. It's cleaner to condense this into one parameter.

2. The if (verbose) sess->act_log pattern leads to an excessive level of indentation, which tends to make things more unclear.

version 2: add common.h

---
  include/cld_msg.h |   10 -----
  include/cldc.h    |    5 +--
  include/common.h  |   27 ++++++++++++++
  lib/cldc.c        |   98 +++++++++++++++++++++-------------------------------
  tools/cldcli.c    |    2 +-
  5 files changed, 70 insertions(+), 72 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 +2 -3
index 9ae6dfe..70062cf 100644
@@ -5,6 +5,7 @@
  #include<stdbool.h>
  #include<glib.h>
  #include<cld_msg.h>
+#include<common.h>
 
  struct cldc_session;
 
@@ -84,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 */
include/common.h new file +14 -0
new file mode 100644
index 0000000..c98f4ab
@@ -0,0 +1,27 @@
+#ifndef __CLD_COMMON_H__
+#define __CLD_COMMON_H__
+
+#include<stdint.h>
+#include<glib.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);
+
+/** Print out a debug message if 'verbose' is enabled */
+#define CLD_DEBUG(cld_log, ...) \
+	if ((cld_log).verbose) { \

include/common.h line 24

Quoted 10 lines
include/common.h new file +10 -0
+		(cld_log).fun(LOG_DEBUG, __VA_ARGS__); \
+	}
+
+/** Print out an informational log message */
+#define CLD_LOG(cld_log, ...) \
+	(cld_log).fun(LOG_INFO, __VA_ARGS__);
+
+struct cld_log {
+	void (*fun)(int prio, const char *fmt, ...);
+	bool verbose;

No major objections. I would rather call the hook 'cb' or 'func' or something other than 'fun', even though I do consider Project Hail fun :)

And thinking long-term, the ideal target is giving the admin the ability to selectively enable or disable various classes of logging messages. In a very rudimentary form, this means a "log_level" variable where increasing values imply increasing verbosity. In a more refined form, a la ISC's BIND server, the admin may mask log classes A, B, C, G, H and I into /var/log/foobar, and mask log classes B, C, D, E anf F into /var/log/bazbang.

What does that mean for your patch? Probably nothing :) But maybe we might want 'verbose' to be an unsigned integer log_level or log_mask.

But that's optional... this email is more for future readers than present programmers, I think.

Jeff