Module: sip-router Branch: master Commit: 35e94c97dfb2f6519c87e566b5158e5b9b9f39ef URL: http://git.sip-router.org/cgi-bin/gitweb.cgi/sip-router/?a=commit;h=35e94c97...
Author: Daniel-Constantin Mierla miconda@gmail.com Committer: Daniel-Constantin Mierla miconda@gmail.com Date: Thu Sep 11 00:16:53 2014 +0200
core: new parameter - log_prefix
- can be used to set what prefix to be printed for each log message while processing a SIP message - can contain variables - example: log_prefix="[$mt $hdr(CSeq) $ci]"
---
action.h | 2 + cfg.lex | 2 + cfg.y | 3 ++ dprint.c | 41 ++++++++++++++++++++++++++++++++++++ dprint.h | 68 +++++++++++++++++++++++++++++++----------------------------- main.c | 7 ++++++ receive.c | 7 ++++++ 7 files changed, 97 insertions(+), 33 deletions(-)
diff --git a/action.h b/action.h index 77f1948..f8a30f6 100644 --- a/action.h +++ b/action.h @@ -81,4 +81,6 @@ int run_actions_safe(struct run_act_ctx* c, struct action* a, #define run_actions_safe(c, a, m) run_actions(c, a, m) #endif /* USE_LONGJMP */
+void log_prefix_set(sip_msg_t *msg); + #endif diff --git a/cfg.lex b/cfg.lex index 874e85f..10766b2 100644 --- a/cfg.lex +++ b/cfg.lex @@ -346,6 +346,7 @@ LOGSTDERROR log_stderror LOGFACILITY log_facility LOGNAME log_name LOGCOLOR log_color +LOGPREFIX log_prefix LISTEN listen ADVERTISE advertise|ADVERTISE ALIAS alias @@ -709,6 +710,7 @@ IMPORTFILE "import_file" <INITIAL>{LOGFACILITY} { yylval.strval=yytext; return LOGFACILITY; } <INITIAL>{LOGNAME} { yylval.strval=yytext; return LOGNAME; } <INITIAL>{LOGCOLOR} { yylval.strval=yytext; return LOGCOLOR; } +<INITIAL>{LOGPREFIX} { yylval.strval=yytext; return LOGPREFIX; } <INITIAL>{LISTEN} { count(); yylval.strval=yytext; return LISTEN; } <INITIAL>{ADVERTISE} { count(); yylval.strval=yytext; return ADVERTISE; } <INITIAL>{ALIAS} { count(); yylval.strval=yytext; return ALIAS; } diff --git a/cfg.y b/cfg.y index 18849b8..1e52289 100644 --- a/cfg.y +++ b/cfg.y @@ -395,6 +395,7 @@ extern char *default_routename; %token LOGFACILITY %token LOGNAME %token LOGCOLOR +%token LOGPREFIX %token LISTEN %token ADVERTISE %token ALIAS @@ -843,6 +844,8 @@ assign_stm: | LOGNAME EQUAL error { yyerror("string value expected"); } | LOGCOLOR EQUAL NUMBER { log_color=$3; } | LOGCOLOR EQUAL error { yyerror("boolean value expected"); } + | LOGPREFIX EQUAL STRING { log_prefix_fmt=$3; } + | LOGPREFIX EQUAL error { yyerror("string value expected"); } | DNS EQUAL NUMBER { received_dns|= ($3)?DO_DNS:0; } | DNS EQUAL error { yyerror("boolean value expected"); } | REV_DNS EQUAL NUMBER { received_dns|= ($3)?DO_REV_DNS:0; } diff --git a/dprint.c b/dprint.c index f65facb..4906001 100644 --- a/dprint.c +++ b/dprint.c @@ -38,6 +38,7 @@
#include "globals.h" #include "dprint.h" +#include "pvar.h"
#include <stdarg.h> #include <stdio.h> @@ -368,3 +369,43 @@ void dprint_color_update(int level, char f, char b) if(f && f!='0') _log_level_colors[level - L_MIN].f = f; if(b && b!='0') _log_level_colors[level - L_MIN].b = b; } + + +/* log_prefix functionality */ +str *log_prefix_val = NULL; +static pv_elem_t *log_prefix_pvs = NULL; + +#define LOG_PREFIX_SIZE 128 +static char log_prefix_buf[LOG_PREFIX_SIZE]; +static str log_prefix_str; + +void log_prefix_init(void) +{ + str s; + if(log_prefix_fmt==NULL) + return; + s.s = log_prefix_fmt; s.len = strlen(s.s); + + if(pv_parse_format(&s, &log_prefix_pvs)<0) + { + LM_ERR("wrong format[%s]\n", s.s); + return; + } +} + +void log_prefix_set(sip_msg_t *msg) +{ + if(log_prefix_pvs == NULL) + return; + if(msg==NULL) { + log_prefix_val = NULL; + return; + } + log_prefix_str.s = log_prefix_buf; + log_prefix_str.len = LOG_PREFIX_SIZE; + if(pv_printf(msg, log_prefix_pvs, log_prefix_str.s, &log_prefix_str.len)<0) + return; + if(log_prefix_str.len<=0) + return; + log_prefix_val = &log_prefix_str; +} diff --git a/dprint.h b/dprint.h index 2d414c3..3c57d9c 100644 --- a/dprint.h +++ b/dprint.h @@ -118,6 +118,8 @@ extern int my_pid(void); extern int log_stderr;
extern int log_color; +extern char *log_prefix_fmt; +extern str *log_prefix_val;
/** @brief maps log levels to their string name and corresponding syslog level */
@@ -151,6 +153,8 @@ void dprint_color_update(int level, char f, char b); void dprint_init_colors(void); void dprint_term_color(char f, char b, str *obuf);
+void log_prefix_init(void); + /** @brief * General logging macros * @@ -271,44 +275,42 @@ void dprint_term_color(char f, char b, str *obuf); do { \ if (get_debug_level(LOG_MNAME, LOG_MNAME_LEN) >= (level) && \ DPRINT_NON_CRIT) { \ + int __llevel; \ + __llevel = ((level)<L_ALERT)?L_ALERT:(((level)>L_DBG)?L_DBG:level); \ DPRINT_CRIT_ENTER; \ - if (likely(((level) >= L_ALERT) && ((level) <= L_DBG))){ \ - if (unlikely(log_stderr)) { \ - if (unlikely(log_color)) dprint_color(level); \ - fprintf(stderr, "%2d(%d) %s: %s" fmt, \ - process_no, my_pid(), \ - (lname)?(lname):LOG_LEVEL2NAME(level), \ - (prefix) , ## args);\ - if (unlikely(log_color)) dprint_color_reset(); \ + if (unlikely(log_stderr)) { \ + if (unlikely(log_color)) dprint_color(__llevel); \ + if(unlikely(log_prefix_val)) { \ + fprintf(stderr, "%.*s%2d(%d) %s: %s" fmt, \ + log_prefix_val->len, log_prefix_val->s, \ + process_no, my_pid(), \ + (lname)?(lname):LOG_LEVEL2NAME(__llevel), \ + (prefix) , ## args);\ } else { \ - syslog(LOG2SYSLOG_LEVEL(level) |\ - (((facility) != DEFAULT_FACILITY) ? \ - (facility) : \ - cfg_get(core, core_cfg, log_facility)), \ - "%s: %s" fmt,\ - (lname)?(lname):LOG_LEVEL2NAME(level),\ - (prefix) , ## args); \ + fprintf(stderr, "%2d(%d) %s: %s" fmt, \ + process_no, my_pid(), \ + (lname)?(lname):LOG_LEVEL2NAME(__llevel), \ + (prefix) , ## args);\ } \ + if (unlikely(log_color)) dprint_color_reset(); \ } else { \ - if (log_stderr) { \ - if (unlikely(log_color)) dprint_color(level); \ - fprintf(stderr, "%2d(%d) %s" fmt, \ - process_no, my_pid(), \ - (prefix) , ## args); \ - if (unlikely(log_color)) dprint_color_reset(); \ + if(unlikely(log_prefix_val)) { \ + syslog(LOG2SYSLOG_LEVEL(__llevel) |\ + (((facility) != DEFAULT_FACILITY) ? \ + (facility) : \ + cfg_get(core, core_cfg, log_facility)), \ + "%.*s%s: %s" fmt,\ + log_prefix_val->len, log_prefix_val->s, \ + (lname)?(lname):LOG_LEVEL2NAME(__llevel),\ + (prefix) , ## args); \ } else { \ - if ((level)<L_ALERT) \ - syslog(LOG2SYSLOG_LEVEL(L_ALERT) | \ - (((facility) != DEFAULT_FACILITY) ? \ - (facility) : \ - cfg_get(core, core_cfg, log_facility)),\ - "%s" fmt, (prefix) , ## args); \ - else \ - syslog(LOG2SYSLOG_LEVEL(L_DBG) | \ - (((facility) != DEFAULT_FACILITY) ? \ - (facility) : \ - cfg_get(core, core_cfg, log_facility)),\ - "%s" fmt, (prefix) , ## args); \ + syslog(LOG2SYSLOG_LEVEL(__llevel) |\ + (((facility) != DEFAULT_FACILITY) ? \ + (facility) : \ + cfg_get(core, core_cfg, log_facility)), \ + "%s: %s" fmt,\ + (lname)?(lname):LOG_LEVEL2NAME(__llevel),\ + (prefix) , ## args); \ } \ } \ DPRINT_CRIT_EXIT; \ diff --git a/main.c b/main.c index bed6ccc..be75335 100644 --- a/main.c +++ b/main.c @@ -380,6 +380,7 @@ int log_stderr = 0; int log_color = 0; /* set custom app name for syslog printing */ char *log_name = 0; +char *log_prefix_fmt = 0; pid_t creator_pid = (pid_t) -1; int config_check = 0; /* check if reply first via host==us */ @@ -1352,6 +1353,9 @@ int main_loop(void) */ cfg_main_set_local();
+ /* init log prefix format */ + log_prefix_init(); + /* init childs with rank==PROC_INIT before forking any process, * this is a place for delayed (after mod_init) initializations * (e.g. shared vars that depend on the total number of processes @@ -1565,6 +1569,9 @@ int main_loop(void) */ cfg_main_set_local();
+ /* init log prefix format */ + log_prefix_init(); + /* init childs with rank==PROC_INIT before forking any process, * this is a place for delayed (after mod_init) initializations * (e.g. shared vars that depend on the total number of processes diff --git a/receive.c b/receive.c index 62ad047..83f72c0 100644 --- a/receive.c +++ b/receive.c @@ -153,6 +153,8 @@ int receive_msg(char* buf, unsigned int len, struct receive_info* rcv_info) } DBG("After parse_msg...\n");
+ /* set log prefix */ + log_prefix_set(msg);
/* ... clear branches from previous message */ clear_branches(); @@ -301,7 +303,10 @@ end: #ifdef STATS if (skipped) STATS_RX_DROPS; #endif + /* reset log prefix */ + log_prefix_set(NULL); return 0; + #ifndef NO_ONREPLY_ROUTE_ERROR error_rpl: /* execute post reply-script callbacks */ @@ -327,6 +332,8 @@ error02: pkg_free(msg); error00: STATS_RX_DROPS; + /* reset log prefix */ + log_prefix_set(NULL); return -1; }