forked from OpenSIPS/opensips
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxlog.c
554 lines (450 loc) · 11.6 KB
/
xlog.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
/**
* Copyright (C) 2001-2003 FhG Fokus
*
* This file is part of opensips, a free SIP server.
*
* opensips is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version
*
* opensips is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <unistd.h>
#include <fcntl.h>
#include <time.h>
#include <ctype.h>
#include "sr_module.h"
#include "dprint.h"
#include "error.h"
#include "socket_info.h"
#include "mem/mem.h"
#include "xlog.h"
#include "pvar.h"
#include "trace_api.h"
#define XLOG_TRACE_API_MODULE "proto_hep"
#define XLOG_CORRELATION_MAGIC "XLOGCORR"
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_NOTICE;
/* 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");
log_buf = (char*)pkg_malloc((xlog_buf_size+1)*sizeof(char));
if(log_buf==NULL)
{
LM_ERR("no pkg memory left\n");
return -1;
}
return 0;
}
int init_xlog(void)
{
if (log_buf == NULL) {
if (buf_init()) {
LM_ERR("Cannot print message!\n");
return -1;
}
}
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;
*xlog_level = xlog_level_default;
if (register_trace_type)
xlog_proto_id = register_trace_type((char *)xlog_id_s);
memset(&tprot, 0, sizeof(trace_proto_t));
if (global_trace_api) {
memcpy(&tprot, global_trace_api, sizeof(trace_proto_t));
} else {
if (trace_prot_bind(XLOG_TRACE_API_MODULE, &tprot)) {
LM_DBG("failed to load trace protocol!\n");
}
}
return 0;
}
static inline void add_xlog_data(trace_message message, void* param)
{
str str_level;
xl_trace_t* xtrace_param = param;
static str sip_str = str_init("sip");
switch (*xlog_level) {
case L_ALERT:
str_level.s = DP_ALERT_TEXT; break;
case L_CRIT:
str_level.s = DP_CRIT_TEXT; break;
case L_ERR:
str_level.s = DP_ERR_TEXT; break;
case L_WARN:
str_level.s = DP_WARN_TEXT; break;
case L_NOTICE:
str_level.s = DP_NOTICE_TEXT; break;
case L_INFO:
str_level.s = DP_INFO_TEXT; break;
case L_DBG:
str_level.s = DP_DBG_TEXT;
str_level.len = sizeof(DP_DBG_TEXT) - 2;
break;
default:
LM_BUG("Unexpected log level [%d]\n", xlog_print_level);
return;
}
/* remove ':' after each level */
str_level.len = strlen(str_level.s) - 1;
tprot.add_payload_part( message, "Event", &str_level);
if ( !xtrace_param )
return;
tprot.add_payload_part( message, "text", &xtrace_param->buf);
if (xtrace_param->msg && xtrace_param->msg->callid)
tprot.add_extra_correlation( message, &sip_str, &xtrace_param->msg->callid->body );
}
static inline int trace_xlog(struct sip_msg* msg, char* buf, int len)
{
struct modify_trace mod_p;
xl_trace_t xtrace_param;
str correlation_str;
union sockaddr_union su;
if (msg == NULL || buf == NULL) {
LM_ERR("bad input!\n");
return -1;
}
/* xlog not traced; exit... */
if (!check_is_traced || check_is_traced(xlog_proto_id) == 0)
return 0;
mod_p.mod_f = add_xlog_data;
xtrace_param.msg = msg;
xtrace_param.buf.s = buf;
xtrace_param.buf.len = len;
mod_p.param = &xtrace_param;
if (msg->callid && msg->callid->body.len) {
correlation_str = msg->callid->body;
} else {
correlation_str.s = "<null>";
correlation_str.len = 6;
}
if (msg->rcv.bind_address && msg->rcv.bind_address->port_no)
/* coverity[check_return] - CID #211391 */
init_su( &su, &msg->rcv.bind_address->address,
msg->rcv.bind_address->port_no);
else
su.s.sa_family = 0;
if (sip_context_trace(xlog_proto_id,
su.s.sa_family ? &su : NULL /*src*/, su.s.sa_family ? &su : NULL /*dst*/,
0, IPPROTO_TCP,
&correlation_str, &mod_p) < 0) {
LM_ERR("failed to trace xlog message!\n");
return -1;
}
return 0;
}
int xl_print_log(struct sip_msg* msg, pv_elem_p list, int *len)
{
if (pv_printf(msg, list, log_buf, len) < 0)
return -1;
if (trace_xlog(msg, log_buf, *len) < 0) {
LM_ERR("failed to trace xlog message!\n");
return -2;
}
return 1;
}
int xlog_2(struct sip_msg* msg, char* lev, char* frm)
{
int log_len, ret;
long level;
xl_level_p xlp;
pv_value_t value;
xlp = (xl_level_t*)(void*)lev;
if(xlp->type==1)
{
if(pv_get_spec_value(msg, &xlp->v.sp, &value)!=0
|| value.flags&PV_VAL_NULL || !(value.flags&PV_VAL_INT))
{
LM_ERR("invalid log level value [%d]\n", value.flags);
return -1;
}
level = (long)value.ri;
} else {
level = xlp->v.level;
}
if(!is_xlog_printable((int)level))
return 1;
log_len = xlog_buf_size;
ret = xl_print_log(msg, (pv_elem_t*)(void*)frm, &log_len);
if (ret == -1) {
LM_ERR("global print buffer too small, increase 'xlog_buf_size'\n");
return -1;
}
/* 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;
}
int xlog_1(struct sip_msg* msg, char* frm)
{
int log_len, ret;
if(!is_xlog_printable(xlog_print_level))
return 1;
log_len = xlog_buf_size;
ret = xl_print_log(msg, (pv_elem_t*)(void*)frm, &log_len);
if (ret == -1) {
LM_ERR("global print buffer too small, increase 'xlog_buf_size'\n");
return -1;
}
/* 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;
}
/**
*/
int xdbg(struct sip_msg* msg, char* frm)
{
int log_len, ret;
if(!is_xlog_printable(L_DBG))
return 1;
log_len = xlog_buf_size;
ret = xl_print_log(msg, (pv_elem_t*)(void*)frm, &log_len);
if (ret == -1) {
LM_ERR("global print buffer too small, increase 'xlog_buf_size'\n");
return -1;
}
/* 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;
}
int pv_parse_color_name(pv_spec_p sp, const str *in)
{
if(in==NULL || in->s==NULL || sp==NULL)
return -1;
if(in->len != 2)
{
LM_ERR("color name must have two chars\n");
return -1;
}
/* foreground */
switch(in->s[0])
{
case 'x':
case 's': case 'r': case 'g':
case 'y': case 'b': case 'p':
case 'c': case 'w': case 'S':
case 'R': case 'G': case 'Y':
case 'B': case 'P': case 'C':
case 'W':
break;
default:
goto error;
}
/* background */
switch(in->s[1])
{
case 'x':
case 's': case 'r': case 'g':
case 'y': case 'b': case 'p':
case 'c': case 'w':
break;
default:
goto error;
}
sp->pvp.pvn.type = PV_NAME_INTSTR;
sp->pvp.pvn.u.isname.type = AVP_NAME_STR;
sp->pvp.pvn.u.isname.name.s = *in;
sp->getf = pv_get_color;
/* force the color PV type */
sp->type = PVT_COLOR;
return 0;
error:
LM_ERR("invalid color name\n");
return -1;
}
#define COL_BUF 10
#define append_sstring(p, end, s) \
do{\
if ((p)+(sizeof(s)-1)<=(end)){\
memcpy((p), s, sizeof(s)-1); \
(p)+=sizeof(s)-1; \
}else{ \
/* overflow */ \
LM_ERR("append_sstring overflow\n"); \
goto error;\
} \
} while(0)
int pv_get_color(struct sip_msg *msg, pv_param_t *param,
pv_value_t *res)
{
static char color[COL_BUF];
char* p;
char* end;
str s;
if(xlog_force_color==0)
{
s.s = "";
s.len = 0;
return pv_get_strval(msg, param, res, &s);
}
p = color;
end = p + COL_BUF;
/* excape sequenz */
append_sstring(p, end, "\033[");
if(param->pvn.u.isname.name.s.s[0]!='_')
{
if (islower((int)param->pvn.u.isname.name.s.s[0]))
{
/* normal font */
append_sstring(p, end, "0;");
} else {
/* bold font */
append_sstring(p, end, "1;");
param->pvn.u.isname.name.s.s[0] += 32;
}
}
/* foreground */
switch(param->pvn.u.isname.name.s.s[0])
{
case 'x':
append_sstring(p, end, "39;");
break;
case 's':
append_sstring(p, end, "30;");
break;
case 'r':
append_sstring(p, end, "31;");
break;
case 'g':
append_sstring(p, end, "32;");
break;
case 'y':
append_sstring(p, end, "33;");
break;
case 'b':
append_sstring(p, end, "34;");
break;
case 'p':
append_sstring(p, end, "35;");
break;
case 'c':
append_sstring(p, end, "36;");
break;
case 'w':
append_sstring(p, end, "37;");
break;
default:
LM_ERR("invalid foreground\n");
return pv_get_null(msg, param, res);
}
/* background */
switch(param->pvn.u.isname.name.s.s[1])
{
case 'x':
append_sstring(p, end, "49");
break;
case 's':
append_sstring(p, end, "40");
break;
case 'r':
append_sstring(p, end, "41");
break;
case 'g':
append_sstring(p, end, "42");
break;
case 'y':
append_sstring(p, end, "43");
break;
case 'b':
append_sstring(p, end, "44");
break;
case 'p':
append_sstring(p, end, "45");
break;
case 'c':
append_sstring(p, end, "46");
break;
case 'w':
append_sstring(p, end, "47");
break;
default:
LM_ERR("invalid background\n");
return pv_get_null(msg, param, res);
}
/* end */
append_sstring(p, end, "m");
s.s = color;
s.len = p-color;
return pv_get_strval(msg, param, res, &s);
error:
return -1;
}