[sr-dev] git:master: xlog: add xlog function with syslog facility as third parameter

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


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

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

xlog: add xlog function with syslog facility as third parameter

- New xlog function with 3 parameters:
    xlog([[facility,] level,] format)
  This allow to specify the syslog facility for every xlog log message.

---

 modules_k/xlog/doc/xlog_admin.xml |   15 ++++-
 modules_k/xlog/xlog.c             |  121 +++++++++++++++++++++++++++++++++++++
 2 files changed, 134 insertions(+), 2 deletions(-)

diff --git a/modules_k/xlog/doc/xlog_admin.xml b/modules_k/xlog/doc/xlog_admin.xml
index ed8704c..2cbd363 100644
--- a/modules_k/xlog/doc/xlog_admin.xml
+++ b/modules_k/xlog/doc/xlog_admin.xml
@@ -194,7 +194,7 @@ modparam("xlog", "log_facility", "LOG_DAEMON")
 	<title>Functions</title>
 	<section>
 		<title>
-		<function moreinfo="none">xlog([level,] format)</function>
+		<function moreinfo="none">xlog([[facility,] level,] format)</function>
 		</title>
 		<para>
 		Print a formated message using LOG function.
@@ -202,6 +202,15 @@ modparam("xlog", "log_facility", "LOG_DAEMON")
 		<para>Meaning of the parameters are as follows:</para>
 		<itemizedlist>
 		<listitem>
+			<para><emphasis>facility</emphasis> - The log facility that will be used for this single log message.
+			</para>
+			<para>
+			<para>
+			If this parameter is missing, the implicit facility is either the facility set with
+			the 'log_facility' module parameter or the core's log facility.
+			</para>
+		</listitem>
+		<listitem>
 			<para><emphasis>level</emphasis> - The level that will be used in LOG function. It can be:
 			</para>
 			<itemizedlist>
@@ -281,6 +290,8 @@ xlog("time [$Tf] method ($rm) r-uri ($ru) 2nd via ($hdr(via[1]))\n");
 $var(loglevel) = 2;
 xlog("$var(loglevel)", "time [$Tf] method ($rm) r-uri ($ru)\n");
 ...
+xlog("LOG_LOCAL3", "L_ERR", "this message will be sent to syslog facility LOG_LOCAL3\n");
+...
 </programlisting>
 		</example>
 	</section>
@@ -314,7 +325,7 @@ xdbg("time $Cbx[$Tf]$Cxx method ($rm) r-uri ($ru)\n");
 	</section>
 	<section>
 		<title>
-		<function moreinfo="none">xlogl([level],format)</function>
+		<function moreinfo="none">xlogl([[facility,] level,] format)</function>
 		</title>
 		<para>
 		Similar to xlog(), in addition is printing cfg line number
diff --git a/modules_k/xlog/xlog.c b/modules_k/xlog/xlog.c
index 93cecd7..1d6091b 100644
--- a/modules_k/xlog/xlog.c
+++ b/modules_k/xlog/xlog.c
@@ -58,15 +58,19 @@ static int mod_init(void);
 
 static int xlog_1(struct sip_msg*, char*, char*);
 static int xlog_2(struct sip_msg*, char*, char*);
+static int xlog_3(struct sip_msg*, char*, char*, char*);
 static int xdbg(struct sip_msg*, char*, char*);
 
 static int xlogl_1(struct sip_msg*, char*, char*);
 static int xlogl_2(struct sip_msg*, char*, char*);
+static int xlogl_3(struct sip_msg*, char*, char*, char*);
 static int xdbgl(struct sip_msg*, char*, char*);
 
 static int xlog_fixup(void** param, int param_no);
+static int xlog3_fixup(void** param, int param_no);
 static int xdbg_fixup(void** param, int param_no);
 static int xlogl_fixup(void** param, int param_no);
+static int xlogl3_fixup(void** param, int param_no);
 static int xdbgl_fixup(void** param, int param_no);
 
 static void destroy(void);
@@ -100,9 +104,11 @@ static pv_export_t mod_items[] = {
 static cmd_export_t cmds[]={
 	{"xlog",   (cmd_function)xlog_1,   1, xdbg_fixup,  0, ANY_ROUTE},
 	{"xlog",   (cmd_function)xlog_2,   2, xlog_fixup,  0, ANY_ROUTE},
+	{"xlog",   (cmd_function)xlog_3,   3, xlog3_fixup, 0, ANY_ROUTE},
 	{"xdbg",   (cmd_function)xdbg,     1, xdbg_fixup,  0, ANY_ROUTE},
 	{"xlogl",  (cmd_function)xlogl_1,  1, xdbgl_fixup, 0, ANY_ROUTE},
 	{"xlogl",  (cmd_function)xlogl_2,  2, xlogl_fixup, 0, ANY_ROUTE},
+	{"xlogl",  (cmd_function)xlogl_3,  3, xlogl3_fixup,0, ANY_ROUTE},
 	{"xdbgl",  (cmd_function)xdbgl,    1, xdbgl_fixup, 0, ANY_ROUTE},
 	{0,0,0,0,0,0}
 };
@@ -254,6 +260,52 @@ static int xlogl_2(struct sip_msg* msg, char* lev, char* frm)
 	return xlog_2_helper(msg, lev, frm, 1, NOFACILITY);
 }
 
+static int xlog_3_helper(struct sip_msg* msg, char* fac, char* lev, char* frm, int mode)
+{
+	long level;
+	int facility;
+	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;
+	}
+	facility = *(int*)fac;
+
+	if(!is_printable((int)level))
+		return 1;
+
+	return xlog_helper(msg, (xl_msg_t*)frm, (int)level, mode, facility);
+}
+
+/**
+ * print log message to level given in parameter
+ * add dedicated logfacility
+ */
+static int xlog_3(struct sip_msg* msg, char* fac, char* lev, char* frm)
+{
+	return xlog_3_helper(msg, fac, lev, frm, 0);
+}
+
+/**
+ * print log message to level given in parameter along with cfg line
+ * add dedicated logfacility
+ */
+static int xlogl_3(struct sip_msg* msg, char* fac, char* lev, char* frm)
+{
+	return xlog_3_helper(msg, fac, lev, frm, 1);
+}
+
 static int xdbg_helper(struct sip_msg* msg, char* frm, char* str2, int mode, int facility)
 {
 	if(!is_printable(L_DBG))
@@ -369,6 +421,37 @@ static int xlog_fixup_helper(void** param, int param_no, int mode)
 	return 0;
 }
 
+/*
+ * fixup log facility
+ */
+static int xlog3_fixup_helper(void** param, int param_no)
+{
+	int *facility;
+	str s;
+
+	s.s = (char*)(*param);
+	if(s.s==NULL)
+	{
+		LM_ERR("wrong log facility\n");
+		return E_UNSPEC;
+	}
+	facility = (int*)pkg_malloc(sizeof(int));
+	if(facility == NULL)
+	{
+		LM_ERR("no more memory\n");
+		return E_UNSPEC;
+	}
+	*facility = str2facility(s.s);
+	if (*facility == -1) {
+		LM_ERR("invalid syslog facility %s\n", s.s);
+		return E_UNSPEC;
+	}
+
+	pkg_free(*param);
+	*param = (void*)facility;
+	return 0;
+}
+
 static int xlog_fixup(void** param, int param_no)
 {
 	if(param==NULL || *param==NULL)
@@ -379,6 +462,25 @@ static int xlog_fixup(void** param, int param_no)
 	return xlog_fixup_helper(param, param_no, 0);
 }
 
+static int xlog3_fixup(void** param, int param_no)
+{
+	if(param==NULL || *param==NULL)
+	{
+		LM_ERR("invalid parameter number %d\n", param_no);
+		return E_UNSPEC;
+	}
+	/* fixup loglevel */
+	if (param_no == 2) {
+		return xlog_fixup_helper(param, 1, 0);
+	}
+	/* fixup log message */
+	if (param_no == 3) {
+		return xdbg_fixup_helper(param, 3, 0);
+	}
+	/* fixup facility */
+	return xlog3_fixup_helper(param, param_no);
+}
+
 static int xdbg_fixup(void** param, int param_no)
 {
 	if(param_no!=1 || param==NULL || *param==NULL)
@@ -389,6 +491,25 @@ static int xdbg_fixup(void** param, int param_no)
 	return xdbg_fixup_helper(param, param_no, 0);
 }
 
+static int xlogl3_fixup(void** param, int param_no)
+{
+	if(param==NULL || *param==NULL)
+	{
+		LM_ERR("invalid parameter number %d\n", param_no);
+		return E_UNSPEC;
+	}
+	/* fixup loglevel */
+	if (param_no == 2) {
+		return xlog_fixup_helper(param, 1, 1);
+	}
+	/* fixup log message */
+	if (param_no == 3) {
+		return xdbg_fixup_helper(param, 3, 1);
+	}
+	/* fixup facility */
+	return xlog3_fixup_helper(param, param_no);
+}
+
 static int xlogl_fixup(void** param, int param_no)
 {
 	if(param==NULL || *param==NULL)




More information about the sr-dev mailing list