Browse code

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]"

Daniel-Constantin Mierla authored on 10/09/2014 22:16:53
Showing 7 changed files
... ...
@@ -81,4 +81,6 @@ int run_actions_safe(struct run_act_ctx* c, struct action* a,
81 81
 #define run_actions_safe(c, a, m) run_actions(c, a, m)
82 82
 #endif /* USE_LONGJMP */
83 83
 
84
+void log_prefix_set(sip_msg_t *msg);
85
+
84 86
 #endif
... ...
@@ -346,6 +346,7 @@ LOGSTDERROR	log_stderror
346 346
 LOGFACILITY	log_facility
347 347
 LOGNAME		log_name
348 348
 LOGCOLOR	log_color
349
+LOGPREFIX	log_prefix
349 350
 LISTEN		listen
350 351
 ADVERTISE	advertise|ADVERTISE
351 352
 ALIAS		alias
... ...
@@ -709,6 +710,7 @@ IMPORTFILE      "import_file"
709 709
 <INITIAL>{LOGFACILITY}	{ yylval.strval=yytext; return LOGFACILITY; }
710 710
 <INITIAL>{LOGNAME}	{ yylval.strval=yytext; return LOGNAME; }
711 711
 <INITIAL>{LOGCOLOR}	{ yylval.strval=yytext; return LOGCOLOR; }
712
+<INITIAL>{LOGPREFIX}	{ yylval.strval=yytext; return LOGPREFIX; }
712 713
 <INITIAL>{LISTEN}	{ count(); yylval.strval=yytext; return LISTEN; }
713 714
 <INITIAL>{ADVERTISE}	{ count(); yylval.strval=yytext; return ADVERTISE; }
714 715
 <INITIAL>{ALIAS}	{ count(); yylval.strval=yytext; return ALIAS; }
... ...
@@ -395,6 +395,7 @@ extern char *default_routename;
395 395
 %token LOGFACILITY
396 396
 %token LOGNAME
397 397
 %token LOGCOLOR
398
+%token LOGPREFIX
398 399
 %token LISTEN
399 400
 %token ADVERTISE
400 401
 %token ALIAS
... ...
@@ -843,6 +844,8 @@ assign_stm:
843 843
 	| LOGNAME EQUAL error { yyerror("string value expected"); }
844 844
 	| LOGCOLOR EQUAL NUMBER { log_color=$3; }
845 845
 	| LOGCOLOR EQUAL error { yyerror("boolean value expected"); }
846
+	| LOGPREFIX EQUAL STRING { log_prefix_fmt=$3; }
847
+	| LOGPREFIX EQUAL error { yyerror("string value expected"); }
846 848
 	| DNS EQUAL NUMBER   { received_dns|= ($3)?DO_DNS:0; }
847 849
 	| DNS EQUAL error { yyerror("boolean value expected"); }
848 850
 	| REV_DNS EQUAL NUMBER { received_dns|= ($3)?DO_REV_DNS:0; }
... ...
@@ -38,6 +38,7 @@
38 38
  
39 39
 #include "globals.h"
40 40
 #include "dprint.h"
41
+#include "pvar.h"
41 42
  
42 43
 #include <stdarg.h>
43 44
 #include <stdio.h>
... ...
@@ -368,3 +369,43 @@ void dprint_color_update(int level, char f, char b)
368 368
 	if(f && f!='0') _log_level_colors[level - L_MIN].f = f;
369 369
 	if(b && b!='0') _log_level_colors[level - L_MIN].b = b;
370 370
 }
371
+
372
+
373
+/* log_prefix functionality */
374
+str *log_prefix_val = NULL;
375
+static pv_elem_t *log_prefix_pvs = NULL;
376
+
377
+#define LOG_PREFIX_SIZE	128
378
+static char log_prefix_buf[LOG_PREFIX_SIZE];
379
+static str log_prefix_str;
380
+
381
+void log_prefix_init(void)
382
+{
383
+	str s;
384
+	if(log_prefix_fmt==NULL)
385
+		return;
386
+	s.s = log_prefix_fmt; s.len = strlen(s.s);
387
+
388
+	if(pv_parse_format(&s, &log_prefix_pvs)<0)
389
+	{
390
+		LM_ERR("wrong format[%s]\n", s.s);
391
+		return;
392
+	}
393
+}
394
+
395
+void log_prefix_set(sip_msg_t *msg)
396
+{
397
+	if(log_prefix_pvs == NULL)
398
+		return;
399
+	if(msg==NULL) {
400
+		log_prefix_val = NULL;
401
+		return;
402
+	}
403
+	log_prefix_str.s = log_prefix_buf;
404
+	log_prefix_str.len = LOG_PREFIX_SIZE;
405
+	if(pv_printf(msg, log_prefix_pvs, log_prefix_str.s, &log_prefix_str.len)<0)
406
+		return;
407
+	if(log_prefix_str.len<=0)
408
+		return;
409
+	log_prefix_val = &log_prefix_str;
410
+}
... ...
@@ -118,6 +118,8 @@ extern int my_pid(void);
118 118
 extern int log_stderr;
119 119
 
120 120
 extern int log_color;
121
+extern char *log_prefix_fmt;
122
+extern str *log_prefix_val;
121 123
 
122 124
 /** @brief maps log levels to their string name and corresponding syslog level */
123 125
 
... ...
@@ -151,6 +153,8 @@ void dprint_color_update(int level, char f, char b);
151 151
 void dprint_init_colors(void);
152 152
 void dprint_term_color(char f, char b, str *obuf);
153 153
 
154
+void log_prefix_init(void);
155
+
154 156
 /** @brief
155 157
  * General logging macros
156 158
  *
... ...
@@ -271,44 +275,42 @@ void dprint_term_color(char f, char b, str *obuf);
271 271
 			do { \
272 272
 				if (get_debug_level(LOG_MNAME, LOG_MNAME_LEN) >= (level) && \
273 273
 						DPRINT_NON_CRIT) { \
274
+					int __llevel; \
275
+					__llevel = ((level)<L_ALERT)?L_ALERT:(((level)>L_DBG)?L_DBG:level); \
274 276
 					DPRINT_CRIT_ENTER; \
275
-					if (likely(((level) >= L_ALERT) && ((level) <= L_DBG))){ \
276
-						if (unlikely(log_stderr)) { \
277
-							if (unlikely(log_color)) dprint_color(level); \
278
-							fprintf(stderr, "%2d(%d) %s: %s" fmt, \
279
-									process_no, my_pid(), \
280
-									(lname)?(lname):LOG_LEVEL2NAME(level), \
281
-									(prefix) , ## args);\
282
-							if (unlikely(log_color)) dprint_color_reset(); \
277
+					if (unlikely(log_stderr)) { \
278
+						if (unlikely(log_color)) dprint_color(__llevel); \
279
+						if(unlikely(log_prefix_val)) { \
280
+							fprintf(stderr, "%.*s%2d(%d) %s: %s" fmt, \
281
+								log_prefix_val->len, log_prefix_val->s, \
282
+								process_no, my_pid(), \
283
+								(lname)?(lname):LOG_LEVEL2NAME(__llevel), \
284
+								(prefix) , ## args);\
283 285
 						} else { \
284
-							syslog(LOG2SYSLOG_LEVEL(level) |\
285
-								   (((facility) != DEFAULT_FACILITY) ? \
286
-									(facility) : \
287
-									cfg_get(core, core_cfg, log_facility)), \
288
-									"%s: %s" fmt,\
289
-									(lname)?(lname):LOG_LEVEL2NAME(level),\
290
-									(prefix) , ## args); \
286
+							fprintf(stderr, "%2d(%d) %s: %s" fmt, \
287
+								process_no, my_pid(), \
288
+								(lname)?(lname):LOG_LEVEL2NAME(__llevel), \
289
+								(prefix) , ## args);\
291 290
 						} \
291
+						if (unlikely(log_color)) dprint_color_reset(); \
292 292
 					} else { \
293
-						if (log_stderr) { \
294
-							if (unlikely(log_color)) dprint_color(level); \
295
-							fprintf(stderr, "%2d(%d) %s" fmt, \
296
-										process_no, my_pid(), \
297
-										(prefix) , ## args); \
298
-							if (unlikely(log_color)) dprint_color_reset(); \
293
+						if(unlikely(log_prefix_val)) { \
294
+							syslog(LOG2SYSLOG_LEVEL(__llevel) |\
295
+							   (((facility) != DEFAULT_FACILITY) ? \
296
+								(facility) : \
297
+								cfg_get(core, core_cfg, log_facility)), \
298
+								"%.*s%s: %s" fmt,\
299
+								log_prefix_val->len, log_prefix_val->s, \
300
+								(lname)?(lname):LOG_LEVEL2NAME(__llevel),\
301
+								(prefix) , ## args); \
299 302
 						} else { \
300
-							if ((level)<L_ALERT) \
301
-								syslog(LOG2SYSLOG_LEVEL(L_ALERT) | \
302
-									   (((facility) != DEFAULT_FACILITY) ? \
303
-										(facility) : \
304
-										cfg_get(core, core_cfg, log_facility)),\
305
-										"%s" fmt, (prefix) , ## args); \
306
-							else \
307
-								syslog(LOG2SYSLOG_LEVEL(L_DBG) | \
308
-									   (((facility) != DEFAULT_FACILITY) ? \
309
-										(facility) : \
310
-										cfg_get(core, core_cfg, log_facility)),\
311
-										"%s" fmt, (prefix) , ## args); \
303
+							syslog(LOG2SYSLOG_LEVEL(__llevel) |\
304
+							   (((facility) != DEFAULT_FACILITY) ? \
305
+								(facility) : \
306
+								cfg_get(core, core_cfg, log_facility)), \
307
+								"%s: %s" fmt,\
308
+								(lname)?(lname):LOG_LEVEL2NAME(__llevel),\
309
+								(prefix) , ## args); \
312 310
 						} \
313 311
 					} \
314 312
 					DPRINT_CRIT_EXIT; \
... ...
@@ -380,6 +380,7 @@ int log_stderr = 0;
380 380
 int log_color = 0;
381 381
 /* set custom app name for syslog printing */
382 382
 char *log_name = 0;
383
+char *log_prefix_fmt = 0;
383 384
 pid_t creator_pid = (pid_t) -1;
384 385
 int config_check = 0;
385 386
 /* check if reply first via host==us */
... ...
@@ -1352,6 +1353,9 @@ int main_loop(void)
1352 1352
 		 */
1353 1353
 		cfg_main_set_local();
1354 1354
 
1355
+		/* init log prefix format */
1356
+		log_prefix_init();
1357
+
1355 1358
 		/* init childs with rank==PROC_INIT before forking any process,
1356 1359
 		 * this is a place for delayed (after mod_init) initializations
1357 1360
 		 * (e.g. shared vars that depend on the total number of processes
... ...
@@ -1565,6 +1569,9 @@ int main_loop(void)
1565 1565
 		 */
1566 1566
 		cfg_main_set_local();
1567 1567
 
1568
+		/* init log prefix format */
1569
+		log_prefix_init();
1570
+
1568 1571
 		/* init childs with rank==PROC_INIT before forking any process,
1569 1572
 		 * this is a place for delayed (after mod_init) initializations
1570 1573
 		 * (e.g. shared vars that depend on the total number of processes
... ...
@@ -153,6 +153,8 @@ int receive_msg(char* buf, unsigned int len, struct receive_info* rcv_info)
153 153
 	}
154 154
 	DBG("After parse_msg...\n");
155 155
 
156
+	/* set log prefix */
157
+	log_prefix_set(msg);
156 158
 
157 159
 	/* ... clear branches from previous message */
158 160
 	clear_branches();
... ...
@@ -301,7 +303,10 @@ end:
301 301
 #ifdef STATS
302 302
 	if (skipped) STATS_RX_DROPS;
303 303
 #endif
304
+	/* reset log prefix */
305
+	log_prefix_set(NULL);
304 306
 	return 0;
307
+
305 308
 #ifndef NO_ONREPLY_ROUTE_ERROR
306 309
 error_rpl:
307 310
 	/* execute post reply-script callbacks */
... ...
@@ -327,6 +332,8 @@ error02:
327 327
 	pkg_free(msg);
328 328
 error00:
329 329
 	STATS_RX_DROPS;
330
+	/* reset log prefix */
331
+	log_prefix_set(NULL);
330 332
 	return -1;
331 333
 }
332 334