-
Notifications
You must be signed in to change notification settings - Fork 582
/
dprint.c
991 lines (820 loc) · 23.7 KB
/
dprint.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
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
/*
* debug print
*
* 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
*/
/*!
* \file
* \brief OpenSIPS Debug console print functions
*/
#include "dprint.h"
#include "log_interface.h"
#include "globals.h"
#include "pt.h"
#include <stdarg.h>
#include <stdio.h>
#include <strings.h>
#if !defined(HOST_NAME_MAX)
#define HOST_NAME_MAX 255
#endif
/* used internally by the log interface */
typedef void (*log_print_pre_fmt_f)(log_print_f gen_print_func, int log_level,
int facility, const char *module, const char *func,
char *stderr_plain_fmt, char *syslog_plain_fmt, char *format, va_list ap);
struct log_consumer_t {
str name;
log_print_f gen_print_func;
log_print_pre_fmt_f pre_fmt_print_func;
int level_filter;
int muted;
};
static int log_level_holder = L_NOTICE;
enum log_format stderr_log_format = LOG_FORMAT_PLAIN;
enum log_format syslog_log_format = LOG_FORMAT_PLAIN;
/* shared holder with the globally set log_level */
static int *log_level_global = NULL;
/* current logging level for this process */
int *log_level = &log_level_holder;
char *log_prefix = "";
/* used when resetting the logging level of this process */
static int *default_log_level;
static char *log_json_buf = NULL;
int log_json_buf_size = 6144;
static char *log_msg_buf = NULL;
int log_msg_buf_size = 4096;
str log_cee_hostname;
static void stderr_dprint(int log_level, int facility, const char *module, const char *func,
char *format, va_list ap);
static void syslog_dprint(int log_level, int facility, const char *module, const char *func,
char *format, va_list ap);
static void stderr_pre_fmt_func(log_print_f gen_print_func, int log_level,
int facility, const char *module, const char *func,
char *stderr_plain_fmt, char *syslog_plain_fmt, char *format, va_list ap);
static void syslog_pre_fmt_func(log_print_f gen_print_func, int log_level,
int facility, const char *module, const char *func,
char *stderr_plain_fmt, char *syslog_plain_fmt, char *format, va_list ap);
/* static consumer table to be used until a shm one is alloc'ed;
* only stderror is enabled initially */
static struct log_consumer_t default_log_consumers[2] ={
{str_init(STDERR_CONSUMER_NAME), stderr_dprint, stderr_pre_fmt_func, 0, 0},
{str_init(SYSLOG_CONSUMER_NAME), syslog_dprint, syslog_pre_fmt_func, 0, 1}
};
struct log_consumer_t *log_consumers = default_log_consumers;
int log_consumers_no = 2;
int log_event_enabled = 0;
int log_event_level_filter = 0;
static str evi_log_name = str_init("E_CORE_LOG");
static event_id_t evi_log_id;
static char* str_fac[]={"LOG_AUTH","LOG_CRON","LOG_DAEMON",
"LOG_KERN","LOG_LOCAL0","LOG_LOCAL1",
"LOG_LOCAL2","LOG_LOCAL3","LOG_LOCAL4","LOG_LOCAL5",
"LOG_LOCAL6","LOG_LOCAL7","LOG_LPR","LOG_MAIL",
"LOG_NEWS","LOG_USER","LOG_UUCP",
#ifndef __OS_solaris
"LOG_AUTHPRIV","LOG_FTP","LOG_SYSLOG",
#endif
0};
static int int_fac[]={LOG_AUTH , LOG_CRON , LOG_DAEMON ,
LOG_KERN , LOG_LOCAL0 , LOG_LOCAL1 ,
LOG_LOCAL2 , LOG_LOCAL3 , LOG_LOCAL4 , LOG_LOCAL5 ,
LOG_LOCAL6 , LOG_LOCAL7 , LOG_LPR , LOG_MAIL ,
LOG_NEWS , LOG_USER , LOG_UUCP
#ifndef __OS_solaris
,LOG_AUTHPRIV,LOG_FTP,LOG_SYSLOG
#endif
};
char ctime_buf[256];
int str2facility(char *s)
{
int i;
for( i=0; str_fac[i] ; i++) {
if (!strcasecmp(s,str_fac[i]))
return int_fac[i];
}
return -1;
}
int dp_my_pid(void)
{
return my_pid();
}
int parse_log_format(str *format)
{
if (str_match(format, (&str_init(LOG_PLAIN_NAME))))
return LOG_FORMAT_PLAIN;
else if (str_match(format, (&str_init(LOG_JSON_NAME))))
return LOG_FORMAT_JSON;
else if (str_match(format, (&str_init(LOG_JSON_CEE_NAME))))
return LOG_FORMAT_JSON_CEE;
else
return -1;
}
void stderr_dprint_tmp(char *format, ...)
{
va_list ap;
va_start(ap, format);
vfprintf(stderr,format,ap);
fflush(stderr);
va_end(ap);
}
int init_log_cee_hostname(void)
{
struct addrinfo hints, *info = NULL;
char hostname[HOST_NAME_MAX+1];
int rc;
str cname;
if (log_cee_hostname.s)
return 0;
gethostname (hostname, HOST_NAME_MAX);
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_CANONNAME;
rc = getaddrinfo(hostname, 0, &hints, &info);
if (!rc && info) {
init_str(&cname, info->ai_canonname);
if (pkg_str_dup(&log_cee_hostname, &cname) < 0) {
LM_ERR("no more pkg memory\n");
freeaddrinfo(info);
return -1;
}
}
if (info)
freeaddrinfo(info);
return 0;
}
#define append_string(_d,_s,_len) \
do{\
memcpy((_d),(_s),(_len));\
(_d) += (_len);\
len += (_len);\
}while(0)
#define append_string_st(_d,_s) \
do{\
memcpy((_d),(_s),sizeof(_s) - 1);\
(_d) += sizeof(_s) - 1;\
len += sizeof(_s) - 1;\
}while(0)
#define S_LEN(_s) (sizeof(_s) - 1)
#define DP_JSON_TIME_KEY "{\"time\": \""
#define DP_JSON_PID_KEY "\", \"pid\": "
#define DP_JSON_LEVEL_KEY ", \"level\": \""
#define DP_JSON_MODULE_KEY "\", \"module\": \""
#define DP_JSON_FUNC_KEY "\", \"function\": \""
#define DP_JSON_PREFIX_KEY "\", \"prefix\": \""
#define DP_JSON_MSG_KEY "\", \"message\": \""
#define DP_JSON_CEE_AT_PREFIX "@cee: "
#define DP_JSON_CEE_TIME_KEY "{\"time\": \""
#define DP_JSON_CEE_PID_KEY "\", \"proc\": {\"id\": \""
#define DP_JSON_CEE_LEVEL_KEY "\"}, \"pri\": \""
#define DP_JSON_CEE_MODULE_KEY "\", \"subsys\": \""
#define DP_JSON_CEE_FUNC_KEY "\", \"native\": {\"function\": \""
#define DP_JSON_CEE_PREFIX_KEY "\", \"log_prefix\": \""
#define DP_JSON_CEE_PREFIX_O_KEY "\", \"native\": {\"log_prefix\": \""
#define DP_JSON_CEE_MSG_B_KEY "\"}, \"msg\": \""
#define DP_JSON_CEE_MSG_KEY "\", \"msg\": \""
#define DP_JSON_CEE_PNAME_KEY "\", \"pname\": \""
#define DP_JSON_CEE_PNAME_VAL "opensips"
#define DP_JSON_CEE_HOST_KEY "\", \"hostname\": \""
#define DP_JSON_MSG_END "\"}"
static int log_escape_json_buf(char *src, int src_len, char *dst, int dst_max_len)
{
int i, j = 0;
int rlen = 0;
/* calculate the required length in the dst buffer */
for (i=0; i<src_len; i++) {
rlen++;
switch (src[i]) {
case '\\':
case '\"':
case '\b':
case '\f':
case '\n':
case '\r':
case '\t':
rlen++; /* +1 for the backslash */
break;
default:
if (src[i] < 32)
rlen += 6; /* +6 for \uXXXX */
}
}
if (rlen>dst_max_len) {
stderr_dprint_tmp_err("buffer too small! needed: %d\n", rlen);
return -1;
} else if (rlen == src_len) {
/* nothing needs to be escaped */
memcpy(dst, src, src_len);
return src_len;
}
for (i=0; i<src_len; i++) {
if (src[i] > 31 && src[i] != '\\' && src[i] != '\"') {
dst[j++] = src[i];
} else {
dst[j++] = '\\';
switch (src[i]) {
case '\\':
dst[j++] = '\\';
break;
case '\"':
dst[j++] = '\"';
break;
case '\b':
dst[j++] = 'b';
break;
case '\f':
dst[j++] = 'f';
break;
case '\n':
dst[j++] = 'n';
break;
case '\r':
dst[j++] = 'r';
break;
case '\t':
dst[j++] = 't';
break;
default:
/* escape and print as unicode codepoint */
sprintf(dst+j, "u%04x", src[i]);
j += 5;
}
}
}
return j;
}
enum log_json_format {
LOG_JSON_SCHEMA_BASIC,
LOG_JSON_SCHEMA_CEE,
LOG_JSON_SCHEMA_CEE_PREFIX /* for syslog, JSON prefixed with "@cee: " */
};
static int log_print_json(str *buf, enum log_json_format json_fmt, char *time,
int pid, char *prefix, const char *level, const char *module, const char *func,
char *format, va_list ap)
{
char *p, *tmp;
int len = 0, rlen;
int l;
if (json_fmt > LOG_JSON_SCHEMA_BASIC) {
rlen = S_LEN(DP_JSON_CEE_PNAME_KEY) + (log_name ? strlen(log_name) :
S_LEN(DP_JSON_CEE_PNAME_VAL)) + S_LEN(DP_JSON_CEE_HOST_KEY) +
log_cee_hostname.len + S_LEN(DP_JSON_MSG_END) + 1;
if (json_fmt == LOG_JSON_SCHEMA_CEE_PREFIX)
len = S_LEN(DP_JSON_CEE_AT_PREFIX);
len += S_LEN(DP_JSON_CEE_TIME_KEY) + strlen(time) +
S_LEN(DP_JSON_CEE_PID_KEY) + INT2STR_MAX_LEN +
S_LEN(DP_JSON_CEE_LEVEL_KEY) + strlen(level) +
(module && func ?
S_LEN(DP_JSON_CEE_MODULE_KEY) + strlen(module) +
S_LEN(DP_JSON_CEE_FUNC_KEY) + strlen(func) : 0) +
S_LEN(DP_JSON_CEE_PREFIX_O_KEY) + strlen(prefix) +
S_LEN(DP_JSON_MSG_KEY) + rlen;
} else {
rlen = S_LEN(DP_JSON_MSG_END) + 1;
len = S_LEN(DP_JSON_TIME_KEY) + strlen(time) + S_LEN(DP_JSON_PID_KEY) +
INT2STR_MAX_LEN + S_LEN(DP_JSON_LEVEL_KEY) + strlen(level) +
(module && func ? S_LEN(DP_JSON_MODULE_KEY) + strlen(module) +
S_LEN(DP_JSON_FUNC_KEY) + strlen(func) : 0) +
S_LEN(DP_JSON_PREFIX_KEY) + strlen(prefix) +
S_LEN(DP_JSON_CEE_MSG_B_KEY) + rlen;
}
if (len >= buf->len) {
stderr_dprint_tmp_err("buffer too small! needed: %d\n", len);
return -1;
}
len = 0;
p = buf->s;
if (json_fmt > LOG_JSON_SCHEMA_BASIC) {
if (json_fmt == LOG_JSON_SCHEMA_CEE_PREFIX)
append_string_st(p, DP_JSON_CEE_AT_PREFIX);
append_string_st(p, DP_JSON_CEE_TIME_KEY);
append_string(p, time, strlen(time));
append_string_st(p, DP_JSON_CEE_PID_KEY);
tmp = int2str(pid, &l);
append_string(p, tmp, l);
append_string_st(p, DP_JSON_CEE_LEVEL_KEY);
append_string(p, level, strlen(level));
if (module && func) {
append_string_st(p, DP_JSON_CEE_MODULE_KEY);
append_string(p, module, strlen(module));
append_string_st(p, DP_JSON_CEE_FUNC_KEY);
append_string(p, func, strlen(func));
}
if (strlen(prefix) != 0) {
if (module && func)
append_string_st(p, DP_JSON_CEE_PREFIX_KEY);
else
append_string_st(p, DP_JSON_CEE_PREFIX_O_KEY);
append_string(p, prefix, strlen(prefix)-1/*skip the ':'*/);
}
if ((module && func) || strlen(prefix) != 0)
append_string_st(p, DP_JSON_CEE_MSG_B_KEY);
else
append_string_st(p, DP_JSON_CEE_MSG_KEY);
} else {
append_string_st(p, DP_JSON_TIME_KEY);
append_string(p, time, strlen(time));
append_string_st(p, DP_JSON_PID_KEY);
tmp = int2str(pid, &l);
append_string(p, tmp, l);
append_string_st(p, DP_JSON_LEVEL_KEY);
append_string(p, level, strlen(level));
if (module && func) {
append_string_st(p, DP_JSON_MODULE_KEY);
append_string(p, module, strlen(module));
append_string_st(p, DP_JSON_FUNC_KEY);
append_string(p, func, strlen(func));
}
if (strlen(prefix) != 0) {
append_string_st(p, DP_JSON_PREFIX_KEY);
append_string(p, prefix, strlen(prefix)-1/*skip the ':'*/);
}
append_string_st(p, DP_JSON_MSG_KEY);
}
l = vsnprintf(log_msg_buf, log_msg_buf_size, format, ap);
if (l < 0) {
stderr_dprint_tmp_err("vsnprintf() failed!\n");
return -1;
}
if (l >= log_msg_buf_size) {
stderr_dprint_tmp_err("warning: buffer too small, log message truncated\n");
l = log_msg_buf_size;
}
l = log_escape_json_buf(log_msg_buf, l, p,
buf->len - len - rlen - 1);
if (l < 0) {
stderr_dprint_tmp_err("failed to escape log message!\n");
return -1;
}
p += l;
len += l;
if (json_fmt == LOG_JSON_SCHEMA_BASIC) {
append_string_st(p, DP_JSON_MSG_END);
} else {
append_string_st(p, DP_JSON_CEE_PNAME_KEY);
if (log_name)
append_string(p, log_name, strlen(log_name));
else
append_string_st(p, DP_JSON_CEE_PNAME_VAL);
append_string_st(p, DP_JSON_CEE_HOST_KEY);
append_string(p, log_cee_hostname.s, log_cee_hostname.len);
append_string_st(p, DP_JSON_MSG_END);
}
*p = '\0';
return len;
}
static void stderr_dprint(int log_level, int facility, const char *module, const char *func,
char *format, va_list ap)
{
char *time;
int pid;
char *prefix;
int len;
str buf = {log_json_buf, log_json_buf_size};
if (stderr_log_format != LOG_FORMAT_PLAIN) {
time = va_arg(ap, char *);
pid = va_arg(ap, int);
prefix = va_arg(ap, char *);
if (module && func)
va_arg(ap, char *);
if ((len = log_print_json(&buf, stderr_log_format==LOG_FORMAT_JSON_CEE ?
LOG_JSON_SCHEMA_CEE : LOG_JSON_SCHEMA_BASIC,
time, pid, prefix, dp_log_level_str(log_level), module, func,
format, ap)) < 0) {
stderr_dprint_tmp_err("failed to print JSON log!\n");
return;
}
fprintf(stderr, "%.*s\n", len, log_json_buf);
fflush(stderr);
} else {
vfprintf(stderr,format,ap);
fflush(stderr);
}
}
static void syslog_dprint(int log_level, int facility, const char *module, const char *func,
char *format, va_list ap)
{
int level;
char *time;
int pid;
char *prefix;
int len;
str buf = {log_json_buf, log_json_buf_size};
switch (log_level) {
case L_ALERT:
level = LOG_ALERT;
break;
case L_CRIT:
level = LOG_CRIT;
break;
case L_ERR:
level = LOG_ERR;
break;
case L_WARN:
level = LOG_WARNING;
break;
case L_NOTICE:
level = LOG_NOTICE;
break;
case L_INFO:
level = LOG_INFO;
break;
case L_DBG:
default:
level = LOG_DEBUG;
}
if (syslog_log_format != LOG_FORMAT_PLAIN) {
time = va_arg(ap, char *);
pid = va_arg(ap, int);
prefix = va_arg(ap, char *);
if (module && func)
va_arg(ap, char *);
if ((len = log_print_json(&buf, syslog_log_format==LOG_FORMAT_JSON_CEE ?
LOG_JSON_SCHEMA_CEE_PREFIX : LOG_JSON_SCHEMA_BASIC,
time, pid, prefix, dp_log_level_str(log_level), module, func,
format, ap)) < 0) {
stderr_dprint_tmp_err("failed to print JSON log!\n");
return;
}
syslog(level|facility, "%.*s\n", len, log_json_buf);
} else {
/* skip the time and pid arguments from va_list */
va_arg(ap, char *);
va_arg(ap, int);
vsyslog(level|facility, format, ap);
}
}
static str evi_time_str = str_init("time");
static str evi_pid_str = str_init("pid");
static str evi_level_str = str_init("level");
static str evi_module_str = str_init("module");
static str evi_func_str = str_init("function");
static str evi_prefix_str = str_init("prefix");
static str evi_msg_str = str_init("message");
static void event_dprint(int level, int facility, const char *module, const char *func,
char *format, va_list ap)
{
evi_params_p list = NULL;
str s;
int n;
int suppressed;
suppressed = pt[process_no].suppress_log_event;
if (suppressed)
return;
pt[process_no].suppress_log_event = 1;
if (!evi_probe_event(evi_log_id)) {
pt[process_no].suppress_log_event = suppressed;
return;
}
if (!(list = evi_get_params())) {
pt[process_no].suppress_log_event = suppressed;
return;
}
init_str(&s, dp_time());
if (evi_param_add_str(list, &evi_time_str, &s)) {
LM_ERR("unable to add event parameter\n");
goto end_free;
}
n = dp_my_pid();
if (evi_param_add_int(list, &evi_pid_str, &n)) {
LM_ERR("unable to add event parameter\n");
goto end_free;
}
init_str(&s, dp_log_level_str(level));
if (evi_param_add_str(list, &evi_level_str, &s)) {
LM_ERR("unable to add event parameter\n");
goto end_free;
}
if (module && func) {
init_str(&s, module);
if (evi_param_add_str(list, &evi_module_str, &s)) {
LM_ERR("unable to add event parameter\n");
goto end_free;
}
init_str(&s, func);
if (evi_param_add_str(list, &evi_func_str, &s)) {
LM_ERR("unable to add event parameter\n");
goto end_free;
}
}
init_str(&s, log_prefix);
if (s.len) {
if (evi_param_add_str(list, &evi_prefix_str, &s)) {
LM_ERR("unable to add event parameter\n");
goto end_free;
}
}
s.len = vsnprintf(log_msg_buf, log_msg_buf_size, format, ap);
if (s.len < 0) {
LM_ERR("vsnprintf() failed!\n");
goto end_free;
}
if (s.len>=log_msg_buf_size) {
LM_WARN("log message truncated\n");
s.len = log_msg_buf_size;
}
/* try to strip \n from the end of the "message" param */
if (log_msg_buf[s.len-1] == '\n') {
log_msg_buf[s.len-1] = '\0';
s.len--;
}
s.s = log_msg_buf;
if (evi_param_add_str(list, &evi_msg_str, &s)) {
LM_ERR("unable to add event parameter\n");
goto end_free;
}
if (evi_raise_event(evi_log_id, list)) {
LM_ERR("unable to raise '%.*s' event\n",
evi_log_name.len, evi_log_name.s);
}
pt[process_no].suppress_log_event = suppressed;
return;
end_free:
evi_free_params(list);
pt[process_no].suppress_log_event = suppressed;
}
/* generic consumer that registers to the log interface */
static void gen_consumer_pre_fmt_func(log_print_f gen_print_func, int log_level,
int facility, const char *module, const char *func,
char *stderr_plain_fmt, char *syslog_plain_fmt, char *format, va_list ap)
{
/* skip the time, pid, prefix and function arguments from va_list */
va_arg(ap, char *);
va_arg(ap, int);
va_arg(ap, char *);
if (module && func)
va_arg(ap, char *);
gen_print_func(log_level, facility, module, func, format, ap);
}
static void stderr_pre_fmt_func(log_print_f gen_print_func, int log_level,
int facility, const char *module, const char *func,
char *stderr_plain_fmt, char *syslog_plain_fmt, char *format, va_list ap)
{
char *fmt = stderr_log_format == LOG_FORMAT_PLAIN ? stderr_plain_fmt : format;
gen_print_func(log_level, facility, module, func, fmt, ap);
}
static void syslog_pre_fmt_func(log_print_f gen_print_func, int log_level,
int facility, const char *module, const char *func,
char *stderr_plain_fmt, char *syslog_plain_fmt, char *format, va_list ap)
{
char *fmt = syslog_log_format == LOG_FORMAT_PLAIN ? syslog_plain_fmt : format;
gen_print_func(log_level, facility, module, func, fmt, ap);
}
void dprint(int log_level, int facility, const char *module, const char *func,
char *stderr_fmt, char *syslog_fmt, char *format, ...)
{
va_list ap, ap_copy;
int i;
va_start(ap, format);
for (i=0; i<log_consumers_no; i++)
if (!log_consumers[i].muted && (!log_consumers[i].level_filter ||
log_consumers[i].level_filter >= log_level)) {
va_copy(ap_copy, ap);
log_consumers[i].pre_fmt_print_func(log_consumers[i].gen_print_func,
log_level, facility, module, func, stderr_fmt, syslog_fmt,
format, ap_copy);
va_end(ap_copy);
}
va_end(ap);
}
int register_log_consumer(char *name, log_print_f print_func,
int level_filter, int muted)
{
if (log_consumers_no == MAX_LOG_CONS_NO) {
LM_ERR("Maximum number of logging consumers already registered\n");
return -1;
}
init_str(&log_consumers[log_consumers_no].name, name);
log_consumers[log_consumers_no].gen_print_func = print_func;
log_consumers[log_consumers_no].pre_fmt_print_func = gen_consumer_pre_fmt_func;
log_consumers[log_consumers_no].level_filter = level_filter;
log_consumers[log_consumers_no].muted = muted;
log_consumers_no++;
return 0;
}
int init_log_json_buf(int realloc)
{
if (realloc && log_json_buf) {
log_json_buf = pkg_realloc(log_json_buf, log_json_buf_size+1);
if (!log_json_buf) {
LM_ERR("no pkg memory left\n");
return -1;
}
} else if (!log_json_buf) {
log_json_buf = pkg_malloc(log_json_buf_size+1);
if (!log_json_buf) {
LM_ERR("no pkg memory left\n");
return -1;
}
}
return 0;
}
int init_log_msg_buf(int realloc)
{
if (realloc && log_msg_buf) {
log_msg_buf = pkg_realloc(log_msg_buf, log_msg_buf_size+1);
if (!log_msg_buf) {
LM_ERR("no pkg memory left\n");
return -1;
}
} else if (!log_msg_buf) {
log_msg_buf = pkg_malloc(log_msg_buf_size+1);
if (!log_msg_buf) {
LM_ERR("no pkg memory left\n");
return -1;
}
}
return 0;
}
/* replaces the default consumer table with a shm allocated one */
int init_log_cons_shm_table(void)
{
struct log_consumer_t *cons;
cons = shm_malloc(MAX_LOG_CONS_NO * sizeof(struct log_consumer_t));
if (!cons) {
LM_ERR("no more shm memory\n");
return -1;
}
memset(cons, 0, MAX_LOG_CONS_NO * sizeof(struct log_consumer_t));
cons[0] = log_consumers[0];
cons[1] = log_consumers[1];
log_consumers = cons;
return 0;
}
void cleanup_log_cons_shm_table(void)
{
struct log_consumer_t *cons = log_consumers;
log_consumers = default_log_consumers;
log_consumers_no = 2;
/* even if we are reusing the static default_log_consumers table,
* inherit the latest settings for the consumers */
log_consumers[0].level_filter = cons[0].level_filter;
log_consumers[0].muted = cons[0].muted;
log_consumers[1].level_filter = cons[1].level_filter;
log_consumers[1].muted = cons[1].muted;
shm_free(cons);
}
int init_log_event_cons(void)
{
evi_log_id = evi_publish_event(evi_log_name);
if (evi_log_id == EVI_ERROR) {
LM_ERR("cannot register '%.*s' event\n",
evi_log_name.len, evi_log_name.s);
return -1;
}
log_msg_buf = pkg_malloc(log_msg_buf_size+1);
if (!log_msg_buf) {
LM_ERR("no pkg memory left\n");
return -1;
}
if (register_log_consumer(EVENT_CONSUMER_NAME, event_dprint,
log_event_level_filter, 1) < 0) {
LM_ERR("Failed to register 'event' log consumer\n");
return -1;
}
return 0;
}
static struct log_consumer_t *get_log_consumer_by_name(str *name)
{
int i;
for (i=0; i<log_consumers_no; i++)
if (str_match(&log_consumers[i].name, name))
return log_consumers+i;
return NULL;
}
int set_log_consumer_level_filter(str *name, int level)
{
struct log_consumer_t *cons;
cons = get_log_consumer_by_name(name);
if (!cons) {
LM_ERR("Unknown consumer: %.*s\n", name->len, name->s);
return -1;
}
cons->level_filter = level;
return 0;
}
int get_log_consumer_level_filter(str *name, int *level_filter)
{
struct log_consumer_t *cons;
cons = get_log_consumer_by_name(name);
if (!cons) {
LM_ERR("Unknown consumer: %.*s\n", name->len, name->s);
return -1;
}
*level_filter = cons->level_filter;
return 0;
}
int set_log_consumer_mute_state(str *name, int state)
{
struct log_consumer_t *cons;
cons = get_log_consumer_by_name(name);
if (!cons) {
LM_ERR("Unknown consumer: %.*s\n", name->len, name->s);
return -1;
}
cons->muted = state;
return 0;
}
int get_log_consumer_mute_state(str *name, int *state)
{
struct log_consumer_t *cons;
cons = get_log_consumer_by_name(name);
if (!cons) {
LM_ERR("Unknown consumer: %.*s\n", name->len, name->s);
return -1;
}
*state = cons->muted;
return 0;
}
int set_log_event_cons_cfg_state(void)
{
if (set_log_consumer_mute_state(&str_init(EVENT_CONSUMER_NAME),
!log_event_enabled) < 0) {
LM_ERR("Failed to set mute state for event consumer\n");
return -1;
}
return 0;
}
void distroy_log_event_cons(void)
{
set_log_consumer_mute_state(&str_init(EVENT_CONSUMER_NAME), 1);
}
int init_log_level(void)
{
log_level = &pt[process_no].log_level;
default_log_level = &pt[process_no].default_log_level;
if (process_no==0) {
/* this is done only by the first process */
log_level_global = (int*)shm_malloc(sizeof(int));
if (log_level_global==NULL) {
LM_ERR("Failed to allocate shm memory for global log_level\n");
return -1;
}
*log_level_global = log_level_holder;
}
*log_level = *log_level_global;
*default_log_level = *log_level_global;
return 0;
}
/* call before pt is freed */
void cleanup_log_level(void)
{
static int my_log_level;
my_log_level = *log_level;
log_level = &my_log_level;
}
void reset_proc_log_level(void)
{
*log_level = *default_log_level;
}
/*
* set the (default) log level of a given process
*
* Note: the index param is not validated!
*/
void __set_proc_log_level(int proc_idx, int level)
{
pt[proc_idx].log_level = level;
}
void __set_proc_default_log_level(int proc_idx, int level)
{
pt[proc_idx].default_log_level = level;
}
/* set the current and default log levels for all OpenSIPS processes */
void set_global_log_level(int level)
{
int i;
for (i = 0; i < counted_max_processes; i++) {
__set_proc_default_log_level(i, level);
__set_proc_log_level(i, level);
}
*log_level_global = level;
}
/* set the log level of the current process */
void set_proc_log_level(int level)
{
__set_proc_log_level(process_no, level);
}
void suppress_proc_log_event(void)
{
pt[process_no].suppress_log_event = 1;
}
void reset_proc_log_event(void)
{
pt[process_no].suppress_log_event = 0;
}