Benny Prijono | 4766ffe | 2005-11-01 17:56:59 +0000 | [diff] [blame^] | 1 | /* $Id$ |
| 2 | * |
Benny Prijono | dd859a6 | 2005-11-01 16:42:51 +0000 | [diff] [blame] | 3 | */ |
| 4 | #include <pj/log.h> |
| 5 | #include <pj/os.h> |
| 6 | #include <pj/compat/stdfileio.h> |
| 7 | |
| 8 | #define CLR_FATAL (PJ_TERM_COLOR_BRIGHT | PJ_TERM_COLOR_R) |
| 9 | #define CLR_WARNING (PJ_TERM_COLOR_BRIGHT | PJ_TERM_COLOR_R | PJ_TERM_COLOR_G) |
| 10 | #define CLR_INFO (PJ_TERM_COLOR_BRIGHT | PJ_TERM_COLOR_R | PJ_TERM_COLOR_G | \ |
| 11 | PJ_TERM_COLOR_B) |
| 12 | #define CLR_DEFAULT (PJ_TERM_COLOR_R | PJ_TERM_COLOR_G | PJ_TERM_COLOR_B) |
| 13 | |
| 14 | static void term_set_color(int level) |
| 15 | { |
| 16 | #if defined(PJ_TERM_HAS_COLOR) && PJ_TERM_HAS_COLOR != 0 |
| 17 | unsigned attr = 0; |
| 18 | switch (level) { |
| 19 | case 0: |
| 20 | case 1: attr = CLR_FATAL; |
| 21 | break; |
| 22 | case 2: attr = CLR_WARNING; |
| 23 | break; |
| 24 | case 3: attr = CLR_INFO; |
| 25 | break; |
| 26 | default: |
| 27 | attr = CLR_DEFAULT; |
| 28 | break; |
| 29 | } |
| 30 | |
| 31 | pj_term_set_color(attr); |
| 32 | #endif |
| 33 | } |
| 34 | |
| 35 | static void term_restore_color(void) |
| 36 | { |
| 37 | #if defined(PJ_TERM_HAS_COLOR) && PJ_TERM_HAS_COLOR != 0 |
| 38 | pj_term_set_color(CLR_DEFAULT); |
| 39 | #endif |
| 40 | } |
| 41 | |
| 42 | |
| 43 | PJ_DEF(void) pj_log_write(int level, const char *buffer, int len) |
| 44 | { |
| 45 | PJ_CHECK_STACK(); |
| 46 | PJ_UNUSED_ARG(len); |
| 47 | |
| 48 | /* Copy to terminal/file. */ |
| 49 | term_set_color(level); |
| 50 | fputs(buffer, stdout); |
| 51 | term_restore_color(); |
| 52 | |
| 53 | fflush(stdout); |
| 54 | } |
| 55 | |