[sr-dev] git:master: xlog: remove code duplication between xlog and xlogl functions

Klaus Darilion klaus.mailinglists at pernau.at
Sun Feb 26 23:24:47 CET 2012


Module: sip-router
Branch: master
Commit: 224c6c69817315c56590648ea626d91b6ac225e8
URL:    http://git.sip-router.org/cgi-bin/gitweb.cgi/sip-router/?a=commit;h=224c6c69817315c56590648ea626d91b6ac225e8

Author: Klaus Darilion <klaus.mailinglists at pernau.at>
Committer: Klaus Darilion <klaus.mailinglists at pernau.at>
Date:   Sun Feb 26 19:13:22 2012 +0100

xlog: remove code duplication between xlog and xlogl functions

- xlog and xlogl functions are more or less duplicates. Use a helper function
  to remove code duplicates for better readability
- preparations for setting the syslog facility per xlog request

---

 modules_k/xlog/xlog.c |   77 ++++++++++++++++++++++--------------------------
 1 files changed, 35 insertions(+), 42 deletions(-)

diff --git a/modules_k/xlog/xlog.c b/modules_k/xlog/xlog.c
index 28e6926..93cecd7 100644
--- a/modules_k/xlog/xlog.c
+++ b/modules_k/xlog/xlog.c
@@ -39,6 +39,7 @@
 
 #include "../../pvar.h"
 
+#define NOFACILITY -1
 
 MODULE_VERSION
 
@@ -159,7 +160,7 @@ static int mod_init(void)
 }
 
 static inline int xlog_helper(struct sip_msg* msg, xl_msg_t *xm,
-		int level, int line)
+		int level, int line, int facility)
 {
 	str txt;
 
@@ -168,17 +169,22 @@ static inline int xlog_helper(struct sip_msg* msg, xl_msg_t *xm,
 	if(xl_print_log(msg, xm->m, _xlog_buf, &txt.len)<0)
 		return -1;
 	txt.s = _xlog_buf;
+	/* if facility is not explicitely defined use the xlog default facility */
+	if (facility==NOFACILITY) {
+		facility = xlog_facility;
+	} 
+
 	if(line>0)
 		if(long_format==1)
-			LOG_(xlog_facility, level, _xlog_prefix,
+			LOG_(facility, level, _xlog_prefix,
 				"%s:%d:%.*s",
 				(xm->a)?(((xm->a->cfile)?xm->a->cfile:"")):"",
 				(xm->a)?xm->a->cline:0, txt.len, txt.s);
 		else
-			LOG_(xlog_facility, level, _xlog_prefix,
+			LOG_(facility, level, _xlog_prefix,
 				"%d:%.*s", (xm->a)?xm->a->cline:0, txt.len, txt.s);
 	else
-		LOG_(xlog_facility, level, _xlog_prefix,
+		LOG_(facility, level, _xlog_prefix,
 			"%.*s", txt.len, txt.s);
 	return 1;
 }
@@ -186,12 +192,16 @@ static inline int xlog_helper(struct sip_msg* msg, xl_msg_t *xm,
 /**
  * print log message to L_ERR level
  */
-static int xlog_1(struct sip_msg* msg, char* frm, char* str2)
+static int xlog_1_helper(struct sip_msg* msg, char* frm, char* str2, int mode, int facility)
 {
 	if(!is_printable(L_ERR))
 		return 1;
 
-	return xlog_helper(msg, (xl_msg_t*)frm, L_ERR, 0);
+	return xlog_helper(msg, (xl_msg_t*)frm, L_ERR, mode, facility);
+}
+static int xlog_1(struct sip_msg* msg, char* frm, char* str2)
+{
+	return xlog_1_helper(msg, frm, str2, 0, NOFACILITY);
 }
 
 /**
@@ -199,16 +209,10 @@ static int xlog_1(struct sip_msg* msg, char* frm, char* str2)
  */
 static int xlogl_1(struct sip_msg* msg, char* frm, char* str2)
 {
-	if(!is_printable(L_ERR))
-		return 1;
-
-	return xlog_helper(msg, (xl_msg_t*)frm, L_ERR, 1);
+	return xlog_1_helper(msg, frm, str2, 1, NOFACILITY);
 }
 
-/**
- * print log message to level given in parameter
- */
-static int xlog_2(struct sip_msg* msg, char* lev, char* frm)
+static int xlog_2_helper(struct sip_msg* msg, char* lev, char* frm, int mode, int facility)
 {
 	long level;
 	xl_level_p xlp;
@@ -231,7 +235,15 @@ static int xlog_2(struct sip_msg* msg, char* lev, char* frm)
 	if(!is_printable((int)level))
 		return 1;
 
-	return xlog_helper(msg, (xl_msg_t*)frm, (int)level, 0);
+	return xlog_helper(msg, (xl_msg_t*)frm, (int)level, mode, facility);
+}
+
+/**
+ * print log message to level given in parameter
+ */
+static int xlog_2(struct sip_msg* msg, char* lev, char* frm)
+{
+	return xlog_2_helper(msg, lev, frm, 0, NOFACILITY);
 }
 
 /**
@@ -239,28 +251,14 @@ static int xlog_2(struct sip_msg* msg, char* lev, char* frm)
  */
 static int xlogl_2(struct sip_msg* msg, char* lev, char* frm)
 {
-	long level;
-	xl_level_p xlp;
-	pv_value_t value;
-
-	xlp = (xl_level_p)lev;
-	if(xlp->type==1)
-	{
-		if(pv_get_spec_value(msg, &xlp->v.sp, &value)!=0
-			|| value.flags&PV_VAL_NULL || !(value.flags&PV_VAL_INT))
-		{
-			LM_ERR("invalid log level value [%d]\n", value.flags);
-			return -1;
-		}
-		level = (long)value.ri;
-	} else {
-		level = xlp->v.level;
-	}
+	return xlog_2_helper(msg, lev, frm, 1, NOFACILITY);
+}
 
-	if(!is_printable((int)level))
+static int xdbg_helper(struct sip_msg* msg, char* frm, char* str2, int mode, int facility)
+{
+	if(!is_printable(L_DBG))
 		return 1;
-
-	return xlog_helper(msg, (xl_msg_t*)frm, (int)level, 1);
+	return xlog_helper(msg, (xl_msg_t*)frm, L_DBG, mode, facility);
 }
 
 /**
@@ -268,9 +266,7 @@ static int xlogl_2(struct sip_msg* msg, char* lev, char* frm)
  */
 static int xdbg(struct sip_msg* msg, char* frm, char* str2)
 {
-	if(!is_printable(L_DBG))
-		return 1;
-	return xlog_helper(msg, (xl_msg_t*)frm, L_DBG, 0);
+	return xdbg_helper(msg, frm, str2, 0, NOFACILITY);
 }
 
 /**
@@ -278,12 +274,9 @@ static int xdbg(struct sip_msg* msg, char* frm, char* str2)
  */
 static int xdbgl(struct sip_msg* msg, char* frm, char* str2)
 {
-	if(!is_printable(L_DBG))
-		return 1;
-	return xlog_helper(msg, (xl_msg_t*)frm, L_DBG, 1);
+	return xdbg_helper(msg, frm, str2, 1, NOFACILITY);
 }
 
-
 /**
  * module destroy function
  */




More information about the sr-dev mailing list