5bd4243d7e48aab1ddf079cc752f77ca9e3e6866
[strongswan.git] / src / libstrongswan / debug.c
1 /*
2 * Copyright (C) 2006 Martin Willi
3 * Hochschule fuer Technik Rapperswil
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 */
15
16 #include <stdarg.h>
17 #include <stdio.h>
18
19 #include "debug.h"
20
21 /**
22 * level logged by the default logger
23 */
24 static int default_level = 1;
25
26 /**
27 * stream logged to by the default logger
28 */
29 static FILE *default_stream = NULL;
30
31 /**
32 * default dbg function which printf all to stderr
33 */
34 void dbg_default(int level, char *fmt, ...)
35 {
36 if (!default_stream)
37 {
38 default_stream = stderr;
39 }
40 if (level <= default_level)
41 {
42 va_list args;
43
44 va_start(args, fmt);
45 vfprintf(default_stream, fmt, args);
46 fprintf(default_stream, "\n");
47 va_end(args);
48 }
49 }
50
51 /**
52 * set the level logged by the default stderr logger
53 */
54 void dbg_default_set_level(int level)
55 {
56 default_level = level;
57 }
58
59 /**
60 * set the stream logged by dbg_default() to
61 */
62 void dbg_default_set_stream(FILE *stream)
63 {
64 default_stream = stream;
65 }
66
67 /**
68 * The registered debug hook.
69 */
70 void (*dbg) (int level, char *fmt, ...) = dbg_default;
71