Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 1 | /* $Id$ */ |
| 2 | /* |
| 3 | * Copyright (C)2003-2006 Benny Prijono <benny@prijono.org> |
| 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/log.h> |
| 20 | #include <pj/os.h> |
| 21 | #include <pj/compat/stdfileio.h> |
| 22 | |
| 23 | #define CLR_FATAL (PJ_TERM_COLOR_BRIGHT | PJ_TERM_COLOR_R) |
| 24 | #define CLR_WARNING (PJ_TERM_COLOR_BRIGHT | PJ_TERM_COLOR_R | PJ_TERM_COLOR_G) |
| 25 | #define CLR_INFO (PJ_TERM_COLOR_BRIGHT | PJ_TERM_COLOR_R | PJ_TERM_COLOR_G | \ |
| 26 | PJ_TERM_COLOR_B) |
| 27 | #define CLR_DEFAULT (PJ_TERM_COLOR_R | PJ_TERM_COLOR_G | PJ_TERM_COLOR_B) |
| 28 | |
| 29 | static void term_set_color(int level) |
| 30 | { |
| 31 | #if defined(PJ_TERM_HAS_COLOR) && PJ_TERM_HAS_COLOR != 0 |
| 32 | unsigned attr = 0; |
| 33 | switch (level) { |
| 34 | case 0: |
| 35 | case 1: attr = CLR_FATAL; |
| 36 | break; |
| 37 | case 2: attr = CLR_WARNING; |
| 38 | break; |
| 39 | case 3: attr = CLR_INFO; |
| 40 | break; |
| 41 | default: |
| 42 | attr = CLR_DEFAULT; |
| 43 | break; |
| 44 | } |
| 45 | |
| 46 | pj_term_set_color(attr); |
| 47 | #endif |
| 48 | } |
| 49 | |
| 50 | static void term_restore_color(void) |
| 51 | { |
| 52 | #if defined(PJ_TERM_HAS_COLOR) && PJ_TERM_HAS_COLOR != 0 |
| 53 | pj_term_set_color(CLR_DEFAULT); |
| 54 | #endif |
| 55 | } |
| 56 | |
| 57 | |
| 58 | PJ_DEF(void) pj_log_write(int level, const char *buffer, int len) |
| 59 | { |
| 60 | PJ_CHECK_STACK(); |
| 61 | PJ_UNUSED_ARG(len); |
| 62 | |
| 63 | /* Copy to terminal/file. */ |
| 64 | term_set_color(level); |
| 65 | fputs(buffer, stdout); |
| 66 | term_restore_color(); |
| 67 | |
| 68 | fflush(stdout); |
| 69 | } |
| 70 | |