Debian Codegraph @

Xpjproject/pjlib/src/pj/log.c

raw symbols SCIP
1 /*
2 * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
3 * Copyright (C) 2003-2008 Benny Prijono <[email protected]>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19 #include <pj/types.h>
20 #include <pj/log.h>
21 #include <pj/string.h>
22 #include <pj/os.h>
23 #include <pj/compat/stdarg.h>
24
25 #if PJ_LOG_MAX_LEVEL >= 1
26
27 #if 0
28 PJ_DEF_DATA(int) pj_log_max_level = PJ_LOG_MAX_LEVEL;
29 #else
30 static int pj_log_max_level = PJ_LOG_MAX_LEVEL;
31 #endif
32
33 static void *g_last_thread;
34
35 #if PJ_HAS_THREADS
36 static long thread_suspended_tls_id = -1;
37 # if PJ_LOG_ENABLE_INDENT
38 static long thread_indent_tls_id = -1;
39 # endif
40 #endif
41
42 #if !PJ_LOG_ENABLE_INDENT || !PJ_HAS_THREADS
43 static int log_indent;
44 #endif
45
46 static pj_log_func *log_writer = &pj_log_write;
47 static unsigned log_decor = PJ_LOG_HAS_TIME | PJ_LOG_HAS_MICRO_SEC |
48 PJ_LOG_HAS_SENDER | PJ_LOG_HAS_NEWLINE |
49 PJ_LOG_HAS_SPACE | PJ_LOG_HAS_THREAD_SWC |
50 PJ_LOG_HAS_INDENT
51 #if (defined(PJ_WIN32) && PJ_WIN32!=0) || \
52 (defined(PJ_WIN64) && PJ_WIN64!=0)
53 | PJ_LOG_HAS_COLOR
54 #endif
55 ;
56
57 static pj_color_t PJ_LOG_COLOR_0 = PJ_TERM_COLOR_BRIGHT | PJ_TERM_COLOR_R;
58 static pj_color_t PJ_LOG_COLOR_1 = PJ_TERM_COLOR_BRIGHT | PJ_TERM_COLOR_R;
59 static pj_color_t PJ_LOG_COLOR_2 = PJ_TERM_COLOR_BRIGHT |
60 PJ_TERM_COLOR_R |
61 PJ_TERM_COLOR_G;
62 static pj_color_t PJ_LOG_COLOR_3 = PJ_TERM_COLOR_BRIGHT |
63 PJ_TERM_COLOR_R |
64 PJ_TERM_COLOR_G |
65 PJ_TERM_COLOR_B;
66 static pj_color_t PJ_LOG_COLOR_4 = PJ_TERM_COLOR_R |
67 PJ_TERM_COLOR_G |
68 PJ_TERM_COLOR_B;
69 static pj_color_t PJ_LOG_COLOR_5 = PJ_TERM_COLOR_R |
70 PJ_TERM_COLOR_G |
71 PJ_TERM_COLOR_B;
72 static pj_color_t PJ_LOG_COLOR_6 = PJ_TERM_COLOR_R |
73 PJ_TERM_COLOR_G |
74 PJ_TERM_COLOR_B;
75 /* Default terminal color */
76 static pj_color_t PJ_LOG_COLOR_77 = PJ_TERM_COLOR_R |
77 PJ_TERM_COLOR_G |
78 PJ_TERM_COLOR_B;
79
80 #if PJ_LOG_USE_STACK_BUFFER==0
81 static char log_buffer[PJ_LOG_MAX_SIZE];
82 #endif
83
84 #define LOG_MAX_INDENT 80
85
86 #if PJ_HAS_THREADS
87 static void logging_shutdown(void)
88 {
89 if (thread_suspended_tls_id != -1) {
90 pj_thread_local_free(thread_suspended_tls_id);
91 thread_suspended_tls_id = -1;
92 }
93 # if PJ_LOG_ENABLE_INDENT
94 if (thread_indent_tls_id != -1) {
95 pj_thread_local_free(thread_indent_tls_id);
96 thread_indent_tls_id = -1;
97 }
98 # endif
99 }
100 #endif /* PJ_HAS_THREADS */
101
102 #if PJ_LOG_ENABLE_INDENT && PJ_HAS_THREADS
103 PJ_DEF(void) pj_log_set_indent(int indent)
104 {
105 if (indent < 0) indent = 0;
106 pj_thread_local_set(thread_indent_tls_id, (void*)(pj_ssize_t)indent);
107 }
108
109 static int log_get_raw_indent(void)
110 {
111 return (long)(pj_ssize_t)pj_thread_local_get(thread_indent_tls_id);
112 }
113
114 #else
115 PJ_DEF(void) pj_log_set_indent(int indent)
116 {
117 log_indent = indent;
118 if (log_indent < 0) log_indent = 0;
119 }
120
121 static int log_get_raw_indent(void)
122 {
123 return log_indent;
124 }
125 #endif /* PJ_LOG_ENABLE_INDENT && PJ_HAS_THREADS */
126
127 PJ_DEF(int) pj_log_get_indent(void)
128 {
129 int indent = log_get_raw_indent();
130 return indent > LOG_MAX_INDENT ? LOG_MAX_INDENT : indent;
131 }
132
133 PJ_DEF(void) pj_log_add_indent(int indent)
134 {
135 pj_log_set_indent(log_get_raw_indent() + indent);
136 }
137
138 PJ_DEF(void) pj_log_push_indent(void)
139 {
140 pj_log_add_indent(PJ_LOG_INDENT_SIZE);
141 }
142
143 PJ_DEF(void) pj_log_pop_indent(void)
144 {
145 pj_log_add_indent(-PJ_LOG_INDENT_SIZE);
146 }
147
148 pj_status_t pj_log_init(void)
149 {
150 #if PJ_HAS_THREADS
151 if (thread_suspended_tls_id == -1) {
152 pj_status_t status;
153 status = pj_thread_local_alloc(&thread_suspended_tls_id);
154 if (status != PJ_SUCCESS) {
155 thread_suspended_tls_id = -1;
156 return status;
157 }
158
159 # if PJ_LOG_ENABLE_INDENT
160 status = pj_thread_local_alloc(&thread_indent_tls_id);
161 if (status != PJ_SUCCESS) {
162 pj_thread_local_free(thread_suspended_tls_id);
163 thread_suspended_tls_id = -1;
164 thread_indent_tls_id = -1;
165 return status;
166 }
167 # endif
168 pj_atexit(&logging_shutdown);
169 }
170 #endif
171 g_last_thread = NULL;
172
173 /* Normalize log decor, e.g: unset thread flags when threading is
174 * disabled.
175 */
176 pj_log_set_decor(pj_log_get_decor());
177
178 return PJ_SUCCESS;
179 }
180
181 PJ_DEF(void) pj_log_set_decor(unsigned decor)
182 {
183 log_decor = decor;
184
185 #if !PJ_HAS_THREADS
186 /* Unset thread related flags */
187 if (log_decor & PJ_LOG_HAS_THREAD_ID) {
188 log_decor &= ~(PJ_LOG_HAS_THREAD_ID);
189 }
190 if (log_decor & PJ_LOG_HAS_THREAD_SWC) {
191 log_decor &= ~(PJ_LOG_HAS_THREAD_SWC);
192 }
193 #endif
194 }
195
196 PJ_DEF(unsigned) pj_log_get_decor(void)
197 {
198 return log_decor;
199 }
200
201 PJ_DEF(void) pj_log_set_color(int level, pj_color_t color)
202 {
203 switch (level)
204 {
205 case 0: PJ_LOG_COLOR_0 = color;
206 break;
207 case 1: PJ_LOG_COLOR_1 = color;
208 break;
209 case 2: PJ_LOG_COLOR_2 = color;
210 break;
211 case 3: PJ_LOG_COLOR_3 = color;
212 break;
213 case 4: PJ_LOG_COLOR_4 = color;
214 break;
215 case 5: PJ_LOG_COLOR_5 = color;
216 break;
217 case 6: PJ_LOG_COLOR_6 = color;
218 break;
219 /* Default terminal color */
220 case 77: PJ_LOG_COLOR_77 = color;
221 break;
222 default:
223 /* Do nothing */
224 break;
225 }
226 }
227
228 PJ_DEF(pj_color_t) pj_log_get_color(int level)
229 {
230 switch (level) {
231 case 0:
232 return PJ_LOG_COLOR_0;
233 case 1:
234 return PJ_LOG_COLOR_1;
235 case 2:
236 return PJ_LOG_COLOR_2;
237 case 3:
238 return PJ_LOG_COLOR_3;
239 case 4:
240 return PJ_LOG_COLOR_4;
241 case 5:
242 return PJ_LOG_COLOR_5;
243 case 6:
244 return PJ_LOG_COLOR_6;
245 default:
246 /* Return default terminal color */
247 return PJ_LOG_COLOR_77;
248 }
249 }
250
251 PJ_DEF(void) pj_log_set_level(int level)
252 {
253 pj_log_max_level = level;
254 }
255
256 #if 1
257 PJ_DEF(int) pj_log_get_level(void)
258 {
259 return pj_log_max_level;
260 }
261 #endif
262
263 PJ_DEF(void) pj_log_set_log_func( pj_log_func *func )
264 {
265 log_writer = func;
266 }
267
268 PJ_DEF(pj_log_func*) pj_log_get_log_func(void)
269 {
270 return log_writer;
271 }
272
273 /* Temporarily suspend logging facility for this thread.
274 * If thread local storage/variable is not used or not initialized, then
275 * we can only suspend the logging globally across all threads. This may
276 * happen e.g. when log function is called before PJLIB is fully initialized
277 * or after PJLIB is shutdown.
278 */
279 static void suspend_logging(int *saved_level)
280 {
281 /* Save the level regardless, just in case PJLIB is shutdown
282 * between suspend and resume.
283 */
284 *saved_level = pj_log_max_level;
285
286 #if PJ_HAS_THREADS
287 if (thread_suspended_tls_id != -1)
288 {
289 static pj_ssize_t suspend_log = PJ_TRUE;
290 pj_thread_local_set(thread_suspended_tls_id,
291 (void*)&suspend_log);
292 }
293 else
294 #endif
295 {
296 pj_log_max_level = 0;
297 }
298 }
299
300 /* Resume logging facility for this thread */
301 static void resume_logging(int *saved_level)
302 {
303 #if PJ_HAS_THREADS
304 if (thread_suspended_tls_id != -1)
305 {
306 static pj_ssize_t suspend_log = PJ_FALSE;
307 pj_thread_local_set(thread_suspended_tls_id,
308 (void*)&suspend_log);
309 }
310 else
311 #endif
312 {
313 /* Only revert the level if application doesn't change the
314 * logging level between suspend and resume.
315 */
316 if (pj_log_max_level==0 && *saved_level)
317 pj_log_max_level = *saved_level;
318 }
319 }
320
321 /* Is logging facility suspended for this thread? */
322 static pj_bool_t is_logging_suspended(void)
323 {
324 #if PJ_HAS_THREADS
325 if (thread_suspended_tls_id != -1)
326 {
327 pj_ssize_t *ils;
328 ils = (pj_ssize_t *)pj_thread_local_get(thread_suspended_tls_id);
329 return (ils? (*ils == PJ_TRUE): PJ_FALSE);
330 }
331 else
332 #endif
333 {
334 return pj_log_max_level == 0;
335 }
336 }
337
338 PJ_DEF(void) pj_log( const char *sender, int level,
339 PJ_PRINT_PARAM_DECOR const char *format, va_list marker)
340 {
341 pj_time_val now;
342 pj_parsed_time ptime;
343 char *pre;
344 #if PJ_LOG_USE_STACK_BUFFER
345 char log_buffer[PJ_LOG_MAX_SIZE];
346 #endif
347 int saved_level, len, print_len;
348
349 PJ_CHECK_STACK();
350
351 if (level > pj_log_max_level)
352 return;
353
354 if (is_logging_suspended())
355 return;
356
357 /* Temporarily disable logging for this thread. Some of PJLIB APIs that
358 * this function calls below will recursively call the logging function
359 * back, hence it will cause infinite recursive calls if we allow that.
360 */
361 suspend_logging(&saved_level);
362
363 /* Get current date/time. */
364 pj_gettimeofday(&now);
365 pj_time_decode(&now, &ptime);
366
367 pre = log_buffer;
368 if (log_decor & PJ_LOG_HAS_LEVEL_TEXT) {
369 static const char *ltexts[] = { "FATAL:", "ERROR:", " WARN:",
370 " INFO:", "DEBUG:", "TRACE:", "DETRC:"};
371 pj_ansi_strxcpy(pre, ltexts[level], PJ_LOG_MAX_SIZE);
372 pre += 6;
373 }
374 if (log_decor & PJ_LOG_HAS_DAY_NAME) {
375 static const char *wdays[] = { "Sun", "Mon", "Tue", "Wed",
376 "Thu", "Fri", "Sat"};
377 pj_ansi_strxcpy(pre, wdays[ptime.wday], PJ_LOG_MAX_SIZE-6);
378 pre += 3;
379 }
380 if (log_decor & PJ_LOG_HAS_YEAR) {
381 if (pre!=log_buffer) *pre++ = ' ';
382 pre += pj_utoa(ptime.year, pre);
383 }
384 if (log_decor & PJ_LOG_HAS_MONTH) {
385 *pre++ = '-';
386 pre += pj_utoa_pad(ptime.mon+1, pre, 2, '0');
387 }
388 if (log_decor & PJ_LOG_HAS_DAY_OF_MON) {
389 *pre++ = '-';
390 pre += pj_utoa_pad(ptime.day, pre, 2, '0');
391 }
392 if (log_decor & PJ_LOG_HAS_TIME) {
393 if (pre!=log_buffer) *pre++ = ' ';
394 pre += pj_utoa_pad(ptime.hour, pre, 2, '0');
395 *pre++ = ':';
396 pre += pj_utoa_pad(ptime.min, pre, 2, '0');
397 *pre++ = ':';
398 pre += pj_utoa_pad(ptime.sec, pre, 2, '0');
399 }
400 if (log_decor & PJ_LOG_HAS_MICRO_SEC) {
401 *pre++ = '.';
402 pre += pj_utoa_pad(ptime.msec, pre, 3, '0');
403 }
404 if (log_decor & PJ_LOG_HAS_SENDER) {
405 enum { SENDER_WIDTH = PJ_LOG_SENDER_WIDTH };
406 pj_size_t sender_len = strlen(sender);
407 if (pre!=log_buffer) *pre++ = ' ';
408 if (sender_len <= SENDER_WIDTH) {
409 while (sender_len < SENDER_WIDTH)
410 *pre++ = ' ', ++sender_len;
411 while (*sender)
412 *pre++ = *sender++;
413 } else {
414 int i;
415 for (i=0; i<SENDER_WIDTH; ++i)
416 *pre++ = *sender++;
417 }
418 }
419 if (log_decor & PJ_LOG_HAS_THREAD_ID) {
420 enum { THREAD_WIDTH = PJ_LOG_THREAD_WIDTH };
421 const char *thread_name = pj_thread_get_name(pj_thread_this());
422 pj_size_t thread_len = strlen(thread_name);
423 *pre++ = ' ';
424 if (thread_len <= THREAD_WIDTH) {
425 while (thread_len < THREAD_WIDTH)
426 *pre++ = ' ', ++thread_len;
427 while (*thread_name)
428 *pre++ = *thread_name++;
429 } else {
430 int i;
431 for (i=0; i<THREAD_WIDTH; ++i)
432 *pre++ = *thread_name++;
433 }
434 }
435
436 if (log_decor != 0 && log_decor != PJ_LOG_HAS_NEWLINE)
437 *pre++ = ' ';
438
439 if (log_decor & PJ_LOG_HAS_THREAD_SWC) {
440 void *current_thread = (void*)pj_thread_this();
441 if (current_thread != g_last_thread) {
442 *pre++ = '!';
443 g_last_thread = current_thread;
444 } else {
445 *pre++ = ' ';
446 }
447 } else if (log_decor & PJ_LOG_HAS_SPACE) {
448 *pre++ = ' ';
449 }
450
451 #if PJ_LOG_ENABLE_INDENT
452 if (log_decor & PJ_LOG_HAS_INDENT) {
453 int indent = pj_log_get_indent();
454 if (indent > 0) {
455 pj_memset(pre, PJ_LOG_INDENT_CHAR, indent);
456 pre += indent;
457 }
458 }
459 #endif
460
461 len = (int)(pre - log_buffer);
462
463 /* Print the whole message to the string log_buffer. */
464 print_len = pj_ansi_vsnprintf(pre, sizeof(log_buffer)-len, format,
465 marker);
466 if (print_len < 0) {
467 level = 1;
468 print_len = pj_ansi_snprintf(pre, sizeof(log_buffer)-len,
469 "<logging error: msg too long>");
470 }
471 if (print_len < 0 || print_len >= (int)(sizeof(log_buffer)-len)) {
472 print_len = sizeof(log_buffer) - len - 1;
473 }
474 len = len + print_len;
475 if (len >= 0 && len < (int)sizeof(log_buffer)-2) {
476 if (log_decor & PJ_LOG_HAS_CR) {
477 log_buffer[len++] = '\r';
478 }
479 if (log_decor & PJ_LOG_HAS_NEWLINE) {
480 log_buffer[len++] = '\n';
481 }
482 log_buffer[len] = '\0';
483 } else {
484 len = sizeof(log_buffer)-1;
485 if (log_decor & PJ_LOG_HAS_CR) {
486 log_buffer[sizeof(log_buffer)-3] = '\r';
487 }
488 if (log_decor & PJ_LOG_HAS_NEWLINE) {
489 log_buffer[sizeof(log_buffer)-2] = '\n';
490 }
491 log_buffer[sizeof(log_buffer)-1] = '\0';
492 }
493
494 /* It should be safe to resume logging at this point. Application can
495 * recursively call the logging function inside the callback.
496 */
497 resume_logging(&saved_level);
498
499 if (log_writer)
500 (*log_writer)(level, log_buffer, len);
501 }
502
503 /*
504 PJ_DEF(void) pj_log_0(const char *obj, const char *format, ...)
505 {
506 va_list arg;
507 va_start(arg, format);
508 pj_log(obj, 0, format, arg);
509 va_end(arg);
510 }
511 */
512
513 PJ_DEF(void) pj_log_1(const char *obj,
514 PJ_PRINT_PARAM_DECOR const char *format, ...)
515 {
516 va_list arg;
517 va_start(arg, format);
518 pj_log(obj, 1, format, arg);
519 va_end(arg);
520 }
521 #endif /* PJ_LOG_MAX_LEVEL >= 1 */
522
523 #if PJ_LOG_MAX_LEVEL >= 2
524 PJ_DEF(void) pj_log_2(const char *obj,
525 PJ_PRINT_PARAM_DECOR const char *format, ...)
526 {
527 va_list arg;
528 va_start(arg, format);
529 pj_log(obj, 2, format, arg);
530 va_end(arg);
531 }
532 #endif
533
534 #if PJ_LOG_MAX_LEVEL >= 3
535 PJ_DEF(void) pj_log_3(const char *obj,
536 PJ_PRINT_PARAM_DECOR const char *format, ...)
537 {
538 va_list arg;
539 va_start(arg, format);
540 pj_log(obj, 3, format, arg);
541 va_end(arg);
542 }
543 #endif
544
545 #if PJ_LOG_MAX_LEVEL >= 4
546 PJ_DEF(void) pj_log_4(const char *obj,
547 PJ_PRINT_PARAM_DECOR const char *format, ...)
548 {
549 va_list arg;
550 va_start(arg, format);
551 pj_log(obj, 4, format, arg);
552 va_end(arg);
553 }
554 #endif
555
556 #if PJ_LOG_MAX_LEVEL >= 5
557 PJ_DEF(void) pj_log_5(const char *obj,
558 PJ_PRINT_PARAM_DECOR const char *format, ...)
559 {
560 va_list arg;
561 va_start(arg, format);
562 pj_log(obj, 5, format, arg);
563 va_end(arg);
564 }
565 #endif
566
567 #if PJ_LOG_MAX_LEVEL >= 6
568 PJ_DEF(void) pj_log_6(const char *obj,
569 PJ_PRINT_PARAM_DECOR const char *format, ...)
570 {
571 va_list arg;
572 va_start(arg, format);
573 pj_log(obj, 6, format, arg);
574 va_end(arg);
575 }
576 #endif
577