blob: b3a4b5ea599ca75b74b8b25afa219dd876637034 [file] [log] [blame]
Benny Prijono9033e312005-11-21 02:08:39 +00001/* $Id$ */
2/*
Benny Prijonoa771a512007-02-19 01:13:53 +00003 * Copyright (C)2003-2007 Benny Prijono <benny@prijono.org>
Benny Prijono9033e312005-11-21 02:08:39 +00004 *
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
29static 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
50static 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
58PJ_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);
Benny Prijonodd710582006-10-05 11:42:57 +000065 printf("%s", buffer);
Benny Prijono9033e312005-11-21 02:08:39 +000066 term_restore_color();
Benny Prijono9033e312005-11-21 02:08:39 +000067}
68