Browse code

xlog: add prefix_mode

allows pv_format specifier in prefix param

Luis Azedo authored on 07/09/2021 08:45:03
Showing 2 changed files
... ...
@@ -165,6 +165,28 @@ modparam("xlog", "long_format", 1)
165 165
 ...
166 166
 modparam("xlog", "prefix", "-xlog: ")
167 167
 ...
168
+</programlisting>
169
+		</example>
170
+	</section>
171
+	<section id="xlog.p.varname">
172
+		<title><varname>prefix_mode</varname> (str)</title>
173
+		<para>
174
+		control behaviour of <varname>prefix</varname> value.
175
+		if mode = 0 then <varname>prefix</varname> is treated as string (current behaviour).
176
+		if mode = 1 then <varname>prefix</varname> is treated as pv_format specifier and value will be evaluated before output.
177
+		</para>
178
+		<para>
179
+		<emphasis>
180
+			Default value is 0.
181
+		</emphasis>
182
+		</para>
183
+		<example>
184
+		<title>Set <varname>prefix_mode</varname> parameter</title>
185
+		<programlisting format="linespecific">
186
+...
187
+modparam("xlog", "prefix", "$cfg(name):$cfg(line)")
188
+modparam("xlog", "prefix_mode", 1)
189
+...
168 190
 </programlisting>
169 191
 		</example>
170 192
 	</section>
... ...
@@ -60,6 +60,9 @@ MODULE_VERSION
60 60
 
61 61
 char *_xlog_buf = NULL;
62 62
 char *_xlog_prefix = "<script>: ";
63
+int _xlog_prefix_mode = 0;
64
+char *_xlog_prefix_buf = NULL;
65
+pv_elem_t *_xlog_prefix_pvs = NULL;
63 66
 
64 67
 /** parameters */
65 68
 static int buf_size=4096;
... ...
@@ -170,6 +173,7 @@ static param_export_t params[]={
170 173
 	{"log_facility", PARAM_STRING, &xlog_facility_name},
171 174
 	{"log_colors",   PARAM_STRING|USE_FUNC_PARAM, (void*)xlog_log_colors_param},
172 175
 	{"methods_filter",  PARAM_INT, &xlog_default_cfg.methods_filter},
176
+	{"prefix_mode",     INT_PARAM, &_xlog_prefix_mode},
173 177
 	{0,0,0}
174 178
 };
175 179
 
... ...
@@ -215,6 +219,27 @@ static int mod_init(void)
215 219
 		PKG_MEM_ERROR;
216 220
 		return -1;
217 221
 	}
222
+
223
+	if (_xlog_prefix_mode) {
224
+		str s;
225
+		s.s = _xlog_prefix;
226
+		s.len = strlen(s.s);
227
+
228
+		if(pv_parse_format(&s, &_xlog_prefix_pvs)<0)
229
+		{
230
+			LM_ERR("wrong format[%s]\n", s.s);
231
+			return -1;
232
+		}
233
+
234
+		_xlog_prefix_buf = (char*)pkg_malloc((buf_size+1)*sizeof(char));
235
+		if(_xlog_prefix_buf==NULL)
236
+		{
237
+			PKG_MEM_ERROR;
238
+			return -1;
239
+		}
240
+
241
+	}
242
+
218 243
 	return 0;
219 244
 }
220 245
 
... ...
@@ -222,12 +247,23 @@ static inline int xlog_helper(struct sip_msg* msg, xl_msg_t *xm,
222 247
 		int level, int line, int facility)
223 248
 {
224 249
 	str txt;
250
+	char * _xlog_prefix_val = _xlog_prefix;
225 251
 
226 252
 	txt.len = buf_size;
253
+	txt.s = _xlog_buf;
227 254
 
228 255
 	if(xl_print_log(msg, xm->m, _xlog_buf, &txt.len)<0)
229 256
 		return -1;
230
-	txt.s = _xlog_buf;
257
+
258
+	if (_xlog_prefix_mode) {
259
+		str _xlog_prefix_str;
260
+		_xlog_prefix_str.s = _xlog_prefix_buf;
261
+		_xlog_prefix_str.len = buf_size;
262
+		if(pv_printf(msg, _xlog_prefix_pvs, _xlog_prefix_str.s, &_xlog_prefix_str.len) == 0 && _xlog_prefix_str.len > 0) {
263
+			_xlog_prefix_val = _xlog_prefix_buf;
264
+		}
265
+	}
266
+
231 267
 	/* if facility is not explicitely defined use the xlog default facility */
232 268
 	if (facility==NOFACILITY) {
233 269
 		facility = xlog_facility;
... ...
@@ -235,7 +271,7 @@ static inline int xlog_helper(struct sip_msg* msg, xl_msg_t *xm,
235 271
 
236 272
 	if(line>0)
237 273
 		if(long_format==1)
238
-			LOG_FN(facility, level, _xlog_prefix,
274
+			LOG_FN(facility, level, _xlog_prefix_val,
239 275
 				"%s:%d:%.*s",
240 276
 				(xm->a)?(((xm->a->cfile)?xm->a->cfile:"")):"",
241 277
 				(xm->a)?xm->a->cline:0, txt.len, txt.s);
... ...
@@ -243,7 +279,7 @@ static inline int xlog_helper(struct sip_msg* msg, xl_msg_t *xm,
243 279
 			LOG_FN(facility, level, _xlog_prefix,
244 280
 				"%d:%.*s", (xm->a)?xm->a->cline:0, txt.len, txt.s);
245 281
 	else
246
-		LOG_FN(facility, level, _xlog_prefix,
282
+		LOG_FN(facility, level, _xlog_prefix_val,
247 283
 			"%.*s", txt.len, txt.s);
248 284
 	return 1;
249 285
 }
... ...
@@ -488,6 +524,10 @@ static void destroy(void)
488 524
 {
489 525
 	if(_xlog_buf)
490 526
 		pkg_free(_xlog_buf);
527
+	if(_xlog_prefix_buf)
528
+		pkg_free(_xlog_prefix_buf);
529
+	if(_xlog_prefix_pvs)
530
+		pv_elem_free_all(_xlog_prefix_pvs);
491 531
 }
492 532
 
493 533
 static int xdbg_fixup_helper(void** param, int param_no, int mode)