985ce6a2a2feaa26b17e11a3178ec112501ab4aa
[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
18 #include "debug.h"
19
20 ENUM(debug_names, DBG_DMN, DBG_LIB,
21 "DMN",
22 "MGR",
23 "IKE",
24 "CHD",
25 "JOB",
26 "CFG",
27 "KNL",
28 "NET",
29 "ASN",
30 "ENC",
31 "TNC",
32 "IMC",
33 "IMV",
34 "PTS",
35 "TLS",
36 "APP",
37 "LIB",
38 );
39
40 ENUM(debug_lower_names, DBG_DMN, DBG_LIB,
41 "dmn",
42 "mgr",
43 "ike",
44 "chd",
45 "job",
46 "cfg",
47 "knl",
48 "net",
49 "asn",
50 "enc",
51 "tnc",
52 "imc",
53 "imv",
54 "pts",
55 "tls",
56 "app",
57 "lib",
58 );
59
60 /**
61 * level logged by the default logger
62 */
63 static level_t default_level = 1;
64
65 /**
66 * stream logged to by the default logger
67 */
68 static FILE *default_stream = NULL;
69
70 /**
71 * default dbg function which printf all to stderr
72 */
73 void dbg_default(debug_t group, level_t level, char *fmt, ...)
74 {
75 if (!default_stream)
76 {
77 default_stream = stderr;
78 }
79 if (level <= default_level)
80 {
81 va_list args;
82
83 va_start(args, fmt);
84 vfprintf(default_stream, fmt, args);
85 fprintf(default_stream, "\n");
86 va_end(args);
87 }
88 }
89
90 /**
91 * set the level logged by the default stderr logger
92 */
93 void dbg_default_set_level(level_t level)
94 {
95 default_level = level;
96 }
97
98 /**
99 * set the stream logged by dbg_default() to
100 */
101 void dbg_default_set_stream(FILE *stream)
102 {
103 default_stream = stream;
104 }
105
106 /**
107 * The registered debug hook.
108 */
109 void (*dbg) (debug_t group, level_t level, char *fmt, ...) = dbg_default;
110