blob: e14b2dfa8e509a6b087005da8792f52e4e33a528 [file] [log] [blame]
Benny Prijono9033e312005-11-21 02:08:39 +00001/* $Id$ */
2/*
Benny Prijono32177c02008-06-20 22:44:47 +00003 * Copyright (C)2003-2008 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
Benny Prijono9033e312005-11-21 02:08:39 +000023
24static void term_set_color(int level)
25{
26#if defined(PJ_TERM_HAS_COLOR) && PJ_TERM_HAS_COLOR != 0
Benny Prijonod6e362a2008-07-19 17:53:47 +000027 pj_term_set_color(pj_log_get_color(level));
Benny Prijono9033e312005-11-21 02:08:39 +000028#endif
29}
30
31static void term_restore_color(void)
32{
33#if defined(PJ_TERM_HAS_COLOR) && PJ_TERM_HAS_COLOR != 0
Benny Prijonod6e362a2008-07-19 17:53:47 +000034 /* Set terminal to its default color */
35 pj_term_set_color(pj_log_get_color(77));
Benny Prijono9033e312005-11-21 02:08:39 +000036#endif
37}
38
39
40PJ_DEF(void) pj_log_write(int level, const char *buffer, int len)
41{
42 PJ_CHECK_STACK();
43 PJ_UNUSED_ARG(len);
44
45 /* Copy to terminal/file. */
Benny Prijonod6e362a2008-07-19 17:53:47 +000046 if (pj_log_get_decor() & PJ_LOG_HAS_COLOR) {
47 term_set_color(level);
48 printf("%s", buffer);
49 term_restore_color();
50 } else {
51 printf("%s", buffer);
52 }
Benny Prijono9033e312005-11-21 02:08:39 +000053}
54