Add AT45DBxx1D driver
[fw/altos] / ao-tools / lib / ccdbg-debug.c
1 /*
2  * Copyright © 2008 Keith Packard <keithp@keithp.com>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
17  */
18
19 #include "ccdbg.h"
20 #include <stdarg.h>
21
22 int
23 ccdbg_level = 0;
24
25 void
26 ccdbg_add_debug(int level)
27 {
28         ccdbg_level |= level;
29 }
30
31 void
32 ccdbg_clear_debug(int level)
33 {
34         ccdbg_level &= ~level;
35 }
36
37 static int initialized;
38
39 void
40 ccdbg_debug(int level, char *format, ...)
41 {
42         va_list ap;
43
44         if (!initialized) {
45                 char *level;
46                 initialized = 1;
47                 level = getenv("CCDEBUG");
48                 if (level)
49                         ccdbg_level |= strtoul(level, NULL, 0);
50         }
51         if (ccdbg_level & level) {
52                 va_start(ap, format);
53                 vprintf(format, ap);
54                 va_end(ap);
55         }
56 }
57
58 void
59 ccdbg_flush(int level)
60 {
61         if (ccdbg_level & level)
62                 fflush(stdout);
63 }