Browse Source

xlog(k): two new functions xlogl and xdbgl

- xlogl is similar to xlog, in adition prints the config line number at
  the beninning of message
- xdbgl is similar to xdbg, in adition prints the config line number at
  the beninning of message
- (c) updated to reflect current content
- handy for troubleshooting as same messages might be used across
  config, making easier to follow the execution path
Elena-Ramona Modroiu 15 years ago
parent
commit
7ae36a9070
5 changed files with 212 additions and 98 deletions
  1. 21 9
      modules_k/xlog/README
  2. 1 5
      modules_k/xlog/doc/xlog.xml
  3. 20 2
      modules_k/xlog/doc/xlog_admin.xml
  4. 0 1
      modules_k/xlog/xl_lib.c
  5. 170 81
      modules_k/xlog/xlog.c

+ 21 - 9
modules_k/xlog/README

@@ -13,9 +13,7 @@ Elena-Ramona Modroiu
 
    Copyright © 2003 FhG FOKUS
 
-   Copyright © 2005 voice-system.ro
-
-   Copyright © 2008 Elena-Ramona Modroiu
+   Copyright © 2008-2010 Elena-Ramona Modroiu
    Revision History
    Revision $Revision$ $Date$
      __________________________________________________________________
@@ -41,6 +39,8 @@ Elena-Ramona Modroiu
 
               5.1. xlog([level,] format)
               5.2. xdbg(format)
+              5.3. xlogl([level],format)
+              5.4. xdbgl(format)
 
    List of Examples
 
@@ -71,6 +71,8 @@ Chapter 1. Admin Guide
 
         5.1. xlog([level,] format)
         5.2. xdbg(format)
+        5.3. xlogl([level],format)
+        5.4. xdbgl(format)
 
 1. Overview
 
@@ -156,8 +158,10 @@ modparam("xlog", "prefix", "-xlog: ")
 
    5.1. xlog([level,] format)
    5.2. xdbg(format)
+   5.3. xlogl([level],format)
+   5.4. xdbgl(format)
 
-5.1. xlog([level,] format)
+5.1.  xlog([level,] format)
 
    Print a formated message using LOG function.
 
@@ -179,8 +183,7 @@ modparam("xlog", "prefix", "-xlog: ")
        If this parameter is missing, the implicit log level is 'L_ERR'.
      * format - The formatted string to be printed.
 
-   This function can be used from REQUEST_ROUTE, FAILURE_ROUTE,
-   ONREPLY_ROUTE, BRANCH_ROUTE.
+   This function can be used from ANY_ROUTE.
 
    Example 1.4. xlog usage
 ...
@@ -192,17 +195,26 @@ $var(loglevel) = 2;
 xlog("$var(loglevel)", "time [$Tf] method ($rm) r-uri ($ru)\n");
 ...
 
-5.2. xdbg(format)
+5.2.  xdbg(format)
 
    Print a formatted message using DBG function.
 
    Meaning of the parameters is as follows:
      * format - The formatted string to be printed.
 
-   This function can be used from REQUEST_ROUTE, FAILURE_ROUTE,
-   ONREPLY_ROUTE, BRANCH_ROUTE.
+   This function can be used from ANY_ROUTE.
 
    Example 1.5. xdbg usage
 ...
 xdbg("time $Cbx[$Tf]$Cxx method ($rm) r-uri ($ru)\n");
 ...
+
+5.3.  xlogl([level],format)
+
+   Similar to xlog(), in addition is printing cfg line number at the
+   beginning of message.
+
+5.4.  xdbgl(format)
+
+   Similar to xdbg(), in addition is printing cfg line number at the
+   beginning of message.

+ 1 - 5
modules_k/xlog/doc/xlog.xml

@@ -35,11 +35,7 @@
 	    <holder>&fhg;</holder>
 	</copyright>
 	<copyright>
-	    <year>2005</year>
-	    <holder>&voicesystem;</holder>
-	</copyright>
-	<copyright>
-	    <year>2008</year>
+	    <year>2008-2010</year>
 	    <holder>Elena-Ramona Modroiu</holder>
 	</copyright>
 	<revhistory>

+ 20 - 2
modules_k/xlog/doc/xlog_admin.xml

@@ -229,7 +229,7 @@ modparam("xlog", "prefix", "-xlog: ")
 		</listitem>
 		</itemizedlist>
 		<para>
-		This function can be used from REQUEST_ROUTE, FAILURE_ROUTE, ONREPLY_ROUTE, BRANCH_ROUTE.
+		This function can be used from ANY_ROUTE.
 		</para>
 		<example>
 		<title><function>xlog</function> usage</title>
@@ -261,7 +261,7 @@ xlog("$var(loglevel)", "time [$Tf] method ($rm) r-uri ($ru)\n");
 		</listitem>
 		</itemizedlist>
 		<para>
-		This function can be used from REQUEST_ROUTE, FAILURE_ROUTE, ONREPLY_ROUTE, BRANCH_ROUTE.
+		This function can be used from ANY_ROUTE.
 		</para>
 
 		<example>
@@ -273,6 +273,24 @@ xdbg("time $Cbx[$Tf]$Cxx method ($rm) r-uri ($ru)\n");
 </programlisting>
 		</example>
 	</section>
+	<section>
+		<title>
+		<function moreinfo="none">xlogl([level],format)</function>
+		</title>
+		<para>
+		Similar to xlog(), in addition is printing cfg line number
+		at the beginning of message.
+		</para>
+	</section>
+	<section>
+		<title>
+		<function moreinfo="none">xdbgl(format)</function>
+		</title>
+		<para>
+		Similar to xdbg(), in addition is printing cfg line number
+		at the beginning of message.
+		</para>
+	</section>
 	</section>
 </chapter>
 

+ 0 - 1
modules_k/xlog/xl_lib.c

@@ -4,7 +4,6 @@
  * XLOG module
  *
  * Copyright (C) 2001-2003 FhG Fokus
- * Copyright (C) 2005 Voice Sistem SRL
  * 
  * This file is part of Kamailio, a free SIP server.
  *

+ 170 - 81
modules_k/xlog/xlog.c

@@ -46,8 +46,8 @@ char *_xlog_buf = NULL;
 char *_xlog_prefix = "<script>: ";
 
 /** parameters */
-int buf_size=4096;
-int force_color=0;
+static int buf_size=4096;
+static int force_color=0;
 
 /** module functions */
 static int mod_init(void);
@@ -56,8 +56,14 @@ static int xlog_1(struct sip_msg*, char*, char*);
 static int xlog_2(struct sip_msg*, char*, char*);
 static int xdbg(struct sip_msg*, char*, char*);
 
-static int xlog_fixup(void** param, int param_no); 
-static int xdbg_fixup(void** param, int param_no); 
+static int xlogl_1(struct sip_msg*, char*, char*);
+static int xlogl_2(struct sip_msg*, char*, char*);
+static int xdbgl(struct sip_msg*, char*, char*);
+
+static int xlog_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 xdbgl_fixup(void** param, int param_no);
 
 static void destroy(void);
 
@@ -74,6 +80,12 @@ typedef struct _xl_level
 	} v;
 } xl_level_t, *xl_level_p;
 
+typedef struct _xl_msg
+{
+	pv_elem_t *m;
+	struct action *a;
+} xl_msg_t;
+
 static pv_export_t mod_items[] = {
 	{ {"C", sizeof("C")-1}, PVT_OTHER, pv_get_color, 0,
 		pv_parse_color_name, 0, 0, 0 },
@@ -82,15 +94,12 @@ static pv_export_t mod_items[] = {
 
 
 static cmd_export_t cmds[]={
-	{"xlog",  (cmd_function)xlog_1,  1, xdbg_fixup, 0, 
-		REQUEST_ROUTE | FAILURE_ROUTE |
-		ONREPLY_ROUTE | BRANCH_ROUTE | ERROR_ROUTE | LOCAL_ROUTE},
-	{"xlog",  (cmd_function)xlog_2,  2, xlog_fixup, 0, 
-		REQUEST_ROUTE | FAILURE_ROUTE |
-		ONREPLY_ROUTE | BRANCH_ROUTE | ERROR_ROUTE | LOCAL_ROUTE},
-	{"xdbg",  (cmd_function)xdbg,    1, xdbg_fixup, 0, 
-		REQUEST_ROUTE | FAILURE_ROUTE | 
-		ONREPLY_ROUTE | BRANCH_ROUTE | ERROR_ROUTE | LOCAL_ROUTE},
+	{"xlog",   (cmd_function)xlog_1,   1, xdbg_fixup,  0, ANY_ROUTE},
+	{"xlog",   (cmd_function)xlog_2,   2, xlog_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},
+	{"xdbgl",  (cmd_function)xdbgl,    1, xdbgl_fixup, 0, ANY_ROUTE},
 	{0,0,0,0,0,0}
 };
 
@@ -133,32 +142,41 @@ static int mod_init(void)
 	return 0;
 }
 
+static inline int xlog_helper(struct sip_msg* msg, xl_msg_t *xm,
+		int level, int line)
+{
+	str txt;
+
+	txt.len = buf_size;
+
+	if(xl_print_log(msg, xm->m, _xlog_buf, &txt.len)<0)
+		return -1;
+	txt.s = _xlog_buf;
+	if(line>0)
+		LOG_(DEFAULT_FACILITY, level, _xlog_prefix,
+			"%d:%.*s", (xm->a)?xm->a->cline:0, txt.len, txt.s);
+	else
+		LOG_(DEFAULT_FACILITY, level, _xlog_prefix,
+			"%.*s", txt.len, txt.s);
+	return 1;
+}
+
 /**
+ * print log message to L_ERR level
  */
 static int xlog_1(struct sip_msg* msg, char* frm, char* str2)
 {
-	int log_len;
-
 	if(!is_printable(L_ERR))
 		return 1;
 
-	log_len = buf_size;
-
-	if(xl_print_log(msg, (pv_elem_t*)frm, _xlog_buf, &log_len)<0)
-		return -1;
-
-	/* _xlog_buf[log_len] = '\0'; */
-	LOG_(DEFAULT_FACILITY, L_ERR, _xlog_prefix,
-			"%.*s", log_len, _xlog_buf);
-
-	return 1;
+	return xlog_helper(msg, (xl_msg_t*)frm, L_ERR, 0);
 }
 
 /**
+ * print log message to level given in parameter
  */
 static int xlog_2(struct sip_msg* msg, char* lev, char* frm)
 {
-	int log_len;
 	long level;
 	xl_level_p xlp;
 	pv_value_t value;
@@ -180,41 +198,72 @@ static int xlog_2(struct sip_msg* msg, char* lev, char* frm)
 	if(!is_printable((int)level))
 		return 1;
 
-	log_len = buf_size;
-
-	if(xl_print_log(msg, (pv_elem_t*)frm, _xlog_buf, &log_len)<0)
-		return -1;
-
-	/* _xlog_buf[log_len] = '\0'; */
-	LOG_(DEFAULT_FACILITY, (int)level, _xlog_prefix,
-			"%.*s", log_len, _xlog_buf);
-
-	return 1;
+	return xlog_helper(msg, (xl_msg_t*)frm, (int)level, 0);
 }
 
 /**
+ * print log message to L_DBG level
  */
 static int xdbg(struct sip_msg* msg, char* frm, char* str2)
 {
-	int log_len;
-
 	if(!is_printable(L_DBG))
 		return 1;
+	return xlog_helper(msg, (xl_msg_t*)frm, L_DBG, 0);
+}
 
-	log_len = buf_size;
+/**
+ * print log message to L_ERR level along with cfg line
+ */
+static int xlogl_1(struct sip_msg* msg, char* frm, char* str2)
+{
+	if(!is_printable(L_ERR))
+		return 1;
 
-	if(xl_print_log(msg, (pv_elem_t*)frm, _xlog_buf, &log_len)<0)
-		return -1;
+	return xlog_helper(msg, (xl_msg_t*)frm, L_ERR, 1);
+}
 
-	/* _xlog_buf[log_len] = '\0'; */
-	LOG_(DEFAULT_FACILITY, L_DBG, _xlog_prefix,
-			"%.*s", log_len, _xlog_buf);
+/**
+ * print log message to level given in parameter along with cfg line
+ */
+static int xlogl_2(struct sip_msg* msg, char* lev, char* frm)
+{
+	long level;
+	xl_level_p xlp;
+	pv_value_t value;
 
-	return 1;
+	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;
+	}
+
+	if(!is_printable((int)level))
+		return 1;
+
+	return xlog_helper(msg, (xl_msg_t*)frm, (int)level, 1);
 }
 
 /**
- * destroy function
+ * print log message to L_DBG level along with cfg line
+ */
+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);
+}
+
+
+/**
+ * module destroy function
  */
 static void destroy(void)
 {
@@ -222,7 +271,48 @@ static void destroy(void)
 		pkg_free(_xlog_buf);
 }
 
-static int xlog_fixup(void** param, int param_no)
+/**
+ * get the pointer to action structure
+ * - take cfg line
+ * - cfg file name available, but could be long
+ */
+static struct action *xlog_fixup_get_action(void **param, int param_no)
+{
+	struct action *ac, ac2;
+	action_u_t *au, au2;
+	/* param points to au->u.string, get pointer to au */
+	au = (void*) ((char *)param - ((char *)&au2.u.string-(char *)&au2));
+	au = au - 1 - param_no;
+	ac = (void*) ((char *)au - ((char *)&ac2.val-(char *)&ac2));
+	return ac;
+}
+
+static int xdbg_fixup_helper(void** param, int param_no, int mode)
+{
+	xl_msg_t *xm;
+	str s;
+
+	xm = (xl_msg_t*)pkg_malloc(sizeof(xl_msg_t));
+	if(xm==NULL)
+	{
+		LM_ERR("no more pkg\n");
+		return -1;
+	}
+	memset(xm, 0, sizeof(xl_msg_t));
+	if(mode==1)
+		xm->a = xlog_fixup_get_action(param, param_no);
+	s.s = (char*)(*param); s.len = strlen(s.s);
+
+	if(pv_parse_format(&s, &xm->m)<0)
+	{
+		LM_ERR("wrong format[%s]\n", (char*)(*param));
+		return E_UNSPEC;
+	}
+	*param = (void*)xm;
+	return 0;
+}
+
+static int xlog_fixup_helper(void** param, int param_no, int mode)
 {
 	xl_level_p xlp;
 	str s;
@@ -275,51 +365,50 @@ static int xlog_fixup(void** param, int param_no)
 	}
 
 	if(param_no==2)
-		return xdbg_fixup(param, 1);
+		return xdbg_fixup_helper(param, 2, mode);
 
 	return 0;
 }
 
-static int xdbg_fixup(void** param, int param_no)
+static int xlog_fixup(void** param, int param_no)
 {
-	pv_elem_t *model;
-	str s;
+	if(param==NULL || *param==NULL)
+	{
+		LM_ERR("invalid parameter number %d\n", param_no);
+		return E_UNSPEC;
+	}
+	return xlog_fixup_helper(param, param_no, 0);
+}
 
-	if(param_no==1)
+static int xdbg_fixup(void** param, int param_no)
+{
+	if(param_no!=1 || param==NULL || *param==NULL)
 	{
-		if(*param)
-		{
-			s.s = (char*)(*param); s.len = strlen(s.s);
-			if(log_stderr!=0 || (log_stderr==0 && force_color!=0))
-			{
-				if(pv_parse_format(&s, &model)<0)
-				{
-					LM_ERR("wrong format[%s]\n",
-						(char*)(*param));
-					return E_UNSPEC;
-				}
-			} else {
-				if(pv_parse_format(&s, &model)<0)
-				{
-					LM_ERR("wrong format[%s]!\n",
-						(char*)(*param));
-					return E_UNSPEC;
-				}
-			}
-			
-			*param = (void*)model;
-			return 0;
-		}
-		else
-		{
-			LM_ERR("null format\n");
-			return E_UNSPEC;
-		}
+		LM_ERR("invalid parameter number %d\n", param_no);
+		return E_UNSPEC;
 	}
+	return xdbg_fixup_helper(param, param_no, 0);
+}
 
-	return 0;
+static int xlogl_fixup(void** param, int param_no)
+{
+	if(param==NULL || *param==NULL)
+	{
+		LM_ERR("invalid parameter number %d\n", param_no);
+		return E_UNSPEC;
+	}
+	return xlog_fixup_helper(param, param_no, 1);
 }
 
+static int xdbgl_fixup(void** param, int param_no)
+{
+	if(param_no!=1 || param==NULL || *param==NULL)
+	{
+		LM_ERR("invalid parameter number %d\n", param_no);
+		return E_UNSPEC;
+	}
+	return xdbg_fixup_helper(param, param_no, 1);
+}
 
 int pv_parse_color_name(pv_spec_p sp, str *in)
 {