Skip to content
Permalink
Browse files

Added "xlog_level" global parameter

This acts as log_level, but for the logs generated via xlog. This gives you separately control the verbosity level for the xlog() calls.
Added possibility to globally change the xlog_level during runtime (to be hooked later into an MI function) or to change the value per process (to be hooked later to the $xlog_level variable)
  • Loading branch information
bogdan-iancu committed Mar 8, 2019
1 parent b2640e6 commit 87893b6dfb6694ccfdca463acf5d74d1c46a053e
Showing with 99 additions and 22 deletions.
  1. +3 −0 cfg.lex
  2. +3 −0 cfg.y
  3. +1 −1 pvar.c
  4. +87 −20 xlog.c
  5. +5 −1 xlog.h
@@ -186,6 +186,7 @@ XDBG "xdbg"
XLOG_BUF_SIZE "xlog_buf_size"
XLOG_FORCE_COLOR "xlog_force_color"
XLOG_PRINT_LEVEL "xlog_print_level"
XLOG_LEVEL "xlog_level"
XLOG "xlog"
RAISE_EVENT "raise_event"
SUBSCRIBE_EVENT "subscribe_event"
@@ -496,6 +497,8 @@ SPACE [ ]
return XLOG_FORCE_COLOR;}
<INITIAL>{XLOG_PRINT_LEVEL} { count(); yylval.strval=yytext;
return XLOG_PRINT_LEVEL;}
<INITIAL>{XLOG_LEVEL} { count(); yylval.strval=yytext;
return XLOG_LEVEL;}
<INITIAL>{RAISE_EVENT} { count(); yylval.strval=yytext;
return RAISE_EVENT;}
<INITIAL>{SUBSCRIBE_EVENT} { count(); yylval.strval=yytext;
3 cfg.y
@@ -307,6 +307,7 @@ static struct multi_str *tmp_mod;
%token XLOG_BUF_SIZE
%token XLOG_FORCE_COLOR
%token XLOG_PRINT_LEVEL
%token XLOG_LEVEL
%token RAISE_EVENT
%token SUBSCRIBE_EVENT
%token CONSTRUCT_URI
@@ -1055,6 +1056,8 @@ assign_stm: DEBUG EQUAL snumber
| XLOG_BUF_SIZE EQUAL error { yyerror("number expected"); }
| XLOG_FORCE_COLOR EQUAL error { yyerror("boolean value expected"); }
| XLOG_PRINT_LEVEL EQUAL error { yyerror("number expected"); }
| XLOG_LEVEL EQUAL NUMBER { *xlog_level = $3; }
| XLOG_LEVEL EQUAL error { yyerror("number expected"); }
| LISTEN EQUAL listen_def {
if (add_listener($3)!=0){
LM_CRIT("cfg. parser: failed"
2 pvar.c
@@ -3409,7 +3409,7 @@ int pv_get_xlog_level(struct sip_msg *msg, pv_param_t *param, pv_value_t *res)
return -1;
}

switch(xlog_level) {
switch(1) {
case L_ALERT:
_set_static_string( res->rs, DP_ALERT_TEXT);
break;
107 xlog.c
@@ -46,23 +46,77 @@ char *log_buf = NULL;

int xlog_buf_size = 4096;
int xlog_force_color = 0;

/* the log level used when printing xlog messages */
int xlog_print_level = L_ERR;

/* this variable is used by the xlog_level to print (inside an xlog)
* the current logging level of that xlog() ; it has no meaning outside
* the scope of an xlog() ! */
int xlog_level = INT_MAX;
/* the logging level/threshold for filtering the xlog messages for printing */
static int xlog_level_default = L_NOTICE;
static int xlog_level_local = L_NOTICE;
static int *xlog_level_shared = NULL;

/* current logging level for this process.
* During init it points the 'xlog_level_default' in order to store the
* original configured value
* During runtime it may point to:
* - xlog_level_shared - the shared xlog level between all procs
* - &xlog_level_local - for a per-proc changed xlog level
*/
int *xlog_level = &xlog_level_default;

/* id with which xlog will be identified by siptrace module
* and will identify an xlog tracing packet */
int xlog_proto_id;
/* tracing module api */
static trace_proto_t tprot;


/* xlog string identifier */
static const char* xlog_id_s="xlog";

#define is_xlog_printable(_level) \
(((int)(*xlog_level)) >= ((int)(_level)))


void set_shared_xlog_level(int new_level)
{
/* do not accept setting as time the xlog_level still points to the
* starting/default holder as we will loose the original value */
if (xlog_level==&xlog_level_default)
return;

*xlog_level_shared = new_level;
}


void set_local_xlog_level(int new_level)
{
/* do not accept setting as time the xlog_level still points to the
* starting/default holder as we will loose the original value */
if (xlog_level==&xlog_level_default)
return;

xlog_level_local = new_level;
xlog_level = &xlog_level_local;
}


void reset_xlog_level(void)
{
if (xlog_level==&xlog_level_default)
return; /* still init, very unlikely */

if (xlog_level==&xlog_level_local) {
/* points a local/per-proc xlog level hodler,
* so reset it to the shared value */
xlog_level = xlog_level_shared;
return;
}

/* points to the shared holder, so reset the shred value */
*xlog_level_shared = xlog_level_default;
}


static int buf_init(void)
{
LM_DBG("initializing...\n");
@@ -75,6 +129,7 @@ static int buf_init(void)
return 0;
}


int init_xlog(void)
{
if (log_buf == NULL) {
@@ -84,6 +139,13 @@ int init_xlog(void)
}
}

xlog_level_shared = (int*)shm_malloc(sizeof(int));
if (xlog_level_shared==NULL) {
LM_ERR("failed to allocate shared holder for xlog\n");
return -1;
}
xlog_level = xlog_level_shared;

if (register_trace_type)
xlog_proto_id = register_trace_type((char *)xlog_id_s);

@@ -107,8 +169,7 @@ static inline void add_xlog_data(trace_message message, void* param)
xl_trace_t* xtrace_param = param;


/* FIXME FIXME */
switch (xlog_level) {
switch (xlog_print_level) {
case L_ALERT:
str_level.s = DP_ALERT_TEXT; break;
case L_CRIT:
@@ -126,7 +187,7 @@ static inline void add_xlog_data(trace_message message, void* param)
str_level.len = sizeof(DP_DBG_TEXT) - 2;
break;
default:
LM_BUG("Unexpected log level [%d]\n", xlog_level);
LM_BUG("Unexpected log level [%d]\n", xlog_print_level);
return;
}

@@ -212,23 +273,25 @@ int xlog_2(struct sip_msg* msg, char* lev, char* frm)
level = xlp->v.level;
}

if(!is_printable((int)level))
if(!is_xlog_printable((int)level))
return 1;

log_len = xlog_buf_size;

xlog_level = level;
ret = xl_print_log(msg, (pv_elem_t*)frm, &log_len);
if (ret == -1) {
LM_ERR("global print buffer too small, increase 'xlog_buf_size'\n");
xlog_level = INT_MAX;
return -1;
}
xlog_level = INT_MAX;

/* set the xlog as log level to trick "LM_GEN" */
set_proc_log_level( *xlog_level );

/* log_buf[log_len] = '\0'; */
LM_GEN1((int)level, "%.*s", log_len, log_buf);

reset_proc_log_level();

return ret;
}

@@ -237,23 +300,25 @@ int xlog_1(struct sip_msg* msg, char* frm, char* str2)
{
int log_len, ret;

if(!is_printable(L_ERR))
if(!is_xlog_printable(xlog_print_level))
return 1;

log_len = xlog_buf_size;

xlog_level = xlog_print_level;
ret = xl_print_log(msg, (pv_elem_t*)frm, &log_len);
if (ret == -1) {
LM_ERR("global print buffer too small, increase 'xlog_buf_size'\n");
xlog_level = INT_MAX;
return -1;
}
xlog_level = INT_MAX;

/* set the xlog as log level to trick "LM_GEN" */
set_proc_log_level( *xlog_level );

/* log_buf[log_len] = '\0'; */
LM_GEN1(xlog_print_level, "%.*s", log_len, log_buf);

reset_proc_log_level();

return ret;
}

@@ -263,23 +328,25 @@ int xdbg(struct sip_msg* msg, char* frm, char* str2)
{
int log_len, ret;

if(!is_printable(L_DBG))
if(!is_xlog_printable(L_DBG))
return 1;

log_len = xlog_buf_size;

xlog_level = L_DBG;
ret = xl_print_log(msg, (pv_elem_t*)frm, &log_len);
if (ret == -1) {
LM_ERR("global print buffer too small, increase 'xlog_buf_size'\n");
xlog_level = INT_MAX;
return -1;
}
xlog_level = INT_MAX;

/* set the xlog as log level to trick "LM_GEN" */
set_proc_log_level( *xlog_level );

/* log_buf[log_len] = '\0'; */
LM_GEN1(L_DBG, "%.*s", log_len, log_buf);

reset_proc_log_level();

return ret;
}

6 xlog.h
@@ -43,7 +43,7 @@ typedef struct _xl_trace
extern int xlog_buf_size;
extern int xlog_force_color;
extern int xlog_print_level;
extern int xlog_level;
extern int *xlog_level;

int xlog_1(struct sip_msg*, char*, char*);
int xlog_2(struct sip_msg*, char*, char*);
@@ -55,5 +55,9 @@ int pv_get_color(struct sip_msg *msg, pv_param_t *param,

int init_xlog(void);

void set_shared_xlog_level(int new_level);
void set_local_xlog_level(int new_level);
void reset_xlog_level(void);

#endif

0 comments on commit 87893b6

Please sign in to comment.
You can’t perform that action at this time.