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 710
 <INITIAL>{LOGFACILITY}	{ yylval.strval=yytext; return LOGFACILITY; }
710 711
 <INITIAL>{LOGNAME}	{ yylval.strval=yytext; return LOGNAME; }
711 712
 <INITIAL>{LOGCOLOR}	{ yylval.strval=yytext; return LOGCOLOR; }
713
+<INITIAL>{LOGPREFIX}	{ yylval.strval=yytext; return LOGPREFIX; }
712 714
 <INITIAL>{LISTEN}	{ count(); yylval.strval=yytext; return LISTEN; }
713 715
 <INITIAL>{ADVERTISE}	{ count(); yylval.strval=yytext; return ADVERTISE; }
714 716
 <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 844
 	| LOGNAME EQUAL error { yyerror("string value expected"); }
844 845
 	| LOGCOLOR EQUAL NUMBER { log_color=$3; }
845 846
 	| LOGCOLOR EQUAL error { yyerror("boolean value expected"); }
847
+	| LOGPREFIX EQUAL STRING { log_prefix_fmt=$3; }
848
+	| LOGPREFIX EQUAL error { yyerror("string value expected"); }
846 849
 	| DNS EQUAL NUMBER   { received_dns|= ($3)?DO_DNS:0; }
847 850
 	| DNS EQUAL error { yyerror("boolean value expected"); }
848 851
 	| 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 369
 	if(f && f!='0') _log_level_colors[level - L_MIN].f = f;
369 370
 	if(b && b!='0') _log_level_colors[level - L_MIN].b = b;
370 371
 }
372
+
373
+
374
+/* log_prefix functionality */
375
+str *log_prefix_val = NULL;
376
+static pv_elem_t *log_prefix_pvs = NULL;
377
+
378
+#define LOG_PREFIX_SIZE	128
379
+static char log_prefix_buf[LOG_PREFIX_SIZE];
380
+static str log_prefix_str;
381
+
382
+void log_prefix_init(void)
383
+{
384
+	str s;
385
+	if(log_prefix_fmt==NULL)
386
+		return;
387
+	s.s = log_prefix_fmt; s.len = strlen(s.s);
388
+
389
+	if(pv_parse_format(&s, &log_prefix_pvs)<0)
390
+	{
391
+		LM_ERR("wrong format[%s]\n", s.s);
392
+		return;
393
+	}
394
+}
395
+
396
+void log_prefix_set(sip_msg_t *msg)
397
+{
398
+	if(log_prefix_pvs == NULL)
399
+		return;
400
+	if(msg==NULL) {
401
+		log_prefix_val = NULL;
402
+		return;
403
+	}
404
+	log_prefix_str.s = log_prefix_buf;
405
+	log_prefix_str.len = LOG_PREFIX_SIZE;
406
+	if(pv_printf(msg, log_prefix_pvs, log_prefix_str.s, &log_prefix_str.len)<0)
407
+		return;
408
+	if(log_prefix_str.len<=0)
409
+		return;
410
+	log_prefix_val = &log_prefix_str;
411
+}
... ...
@@ -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 153
 void dprint_init_colors(void);
152 154
 void dprint_term_color(char f, char b, str *obuf);
153 155
 
156
+void log_prefix_init(void);
157
+
154 158
 /** @brief
155 159
  * General logging macros
156 160
  *
... ...
@@ -271,44 +275,42 @@ void dprint_term_color(char f, char b, str *obuf);
271 275
 			do { \
272 276
 				if (get_debug_level(LOG_MNAME, LOG_MNAME_LEN) >= (level) && \
273 277
 						DPRINT_NON_CRIT) { \
278
+					int __llevel; \
279
+					__llevel = ((level)<L_ALERT)?L_ALERT:(((level)>L_DBG)?L_DBG:level); \
274 280
 					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(); \
281
+					if (unlikely(log_stderr)) { \
282
+						if (unlikely(log_color)) dprint_color(__llevel); \
283
+						if(unlikely(log_prefix_val)) { \
284
+							fprintf(stderr, "%.*s%2d(%d) %s: %s" fmt, \
285
+								log_prefix_val->len, log_prefix_val->s, \
286
+								process_no, my_pid(), \
287
+								(lname)?(lname):LOG_LEVEL2NAME(__llevel), \
288
+								(prefix) , ## args);\
283 289
 						} 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); \
290
+							fprintf(stderr, "%2d(%d) %s: %s" fmt, \
291
+								process_no, my_pid(), \
292
+								(lname)?(lname):LOG_LEVEL2NAME(__llevel), \
293
+								(prefix) , ## args);\
291 294
 						} \
295
+						if (unlikely(log_color)) dprint_color_reset(); \
292 296
 					} 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(); \
297
+						if(unlikely(log_prefix_val)) { \
298
+							syslog(LOG2SYSLOG_LEVEL(__llevel) |\
299
+							   (((facility) != DEFAULT_FACILITY) ? \
300
+								(facility) : \
301
+								cfg_get(core, core_cfg, log_facility)), \
302
+								"%.*s%s: %s" fmt,\
303
+								log_prefix_val->len, log_prefix_val->s, \
304
+								(lname)?(lname):LOG_LEVEL2NAME(__llevel),\
305
+								(prefix) , ## args); \
299 306
 						} 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); \
307
+							syslog(LOG2SYSLOG_LEVEL(__llevel) |\
308
+							   (((facility) != DEFAULT_FACILITY) ? \
309
+								(facility) : \
310
+								cfg_get(core, core_cfg, log_facility)), \
311
+								"%s: %s" fmt,\
312
+								(lname)?(lname):LOG_LEVEL2NAME(__llevel),\
313
+								(prefix) , ## args); \
312 314
 						} \
313 315
 					} \
314 316
 					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 1353
 		 */
1353 1354
 		cfg_main_set_local();
1354 1355
 
1356
+		/* init log prefix format */
1357
+		log_prefix_init();
1358
+
1355 1359
 		/* init childs with rank==PROC_INIT before forking any process,
1356 1360
 		 * this is a place for delayed (after mod_init) initializations
1357 1361
 		 * (e.g. shared vars that depend on the total number of processes
... ...
@@ -1565,6 +1569,9 @@ int main_loop(void)
1565 1569
 		 */
1566 1570
 		cfg_main_set_local();
1567 1571
 
1572
+		/* init log prefix format */
1573
+		log_prefix_init();
1574
+
1568 1575
 		/* init childs with rank==PROC_INIT before forking any process,
1569 1576
 		 * this is a place for delayed (after mod_init) initializations
1570 1577
 		 * (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 303
 #ifdef STATS
302 304
 	if (skipped) STATS_RX_DROPS;
303 305
 #endif
306
+	/* reset log prefix */
307
+	log_prefix_set(NULL);
304 308
 	return 0;
309
+
305 310
 #ifndef NO_ONREPLY_ROUTE_ERROR
306 311
 error_rpl:
307 312
 	/* execute post reply-script callbacks */
... ...
@@ -327,6 +332,8 @@ error02:
327 332
 	pkg_free(msg);
328 333
 error00:
329 334
 	STATS_RX_DROPS;
335
+	/* reset log prefix */
336
+	log_prefix_set(NULL);
330 337
 	return -1;
331 338
 }
332 339