Use wpa_msg() instead of wpa_printf()
[mech_eap.git] / src / utils / wpa_debug.h
1 /*
2  * wpa_supplicant/hostapd / Debug prints
3  * Copyright (c) 2002-2007, Jouni Malinen <j@w1.fi>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation.
8  *
9  * Alternatively, this software may be distributed under the terms of BSD
10  * license.
11  *
12  * See README and COPYING for more details.
13  */
14
15 #ifndef WPA_DEBUG_H
16 #define WPA_DEBUG_H
17
18 #include "wpabuf.h"
19
20 /* Debugging function - conditional printf and hex dump. Driver wrappers can
21  * use these for debugging purposes. */
22
23 enum {
24         MSG_EXCESSIVE, MSG_MSGDUMP, MSG_DEBUG, MSG_INFO, MSG_WARNING, MSG_ERROR
25 };
26
27 #ifdef CONFIG_NO_STDOUT_DEBUG
28
29 #define wpa_debug_print_timestamp() do { } while (0)
30 #define wpa_printf(args...) do { } while (0)
31 #define wpa_hexdump(l,t,b,le) do { } while (0)
32 #define wpa_hexdump_buf(l,t,b) do { } while (0)
33 #define wpa_hexdump_key(l,t,b,le) do { } while (0)
34 #define wpa_hexdump_buf_key(l,t,b) do { } while (0)
35 #define wpa_hexdump_ascii(l,t,b,le) do { } while (0)
36 #define wpa_hexdump_ascii_key(l,t,b,le) do { } while (0)
37 #define wpa_debug_open_file(p) do { } while (0)
38 #define wpa_debug_close_file() do { } while (0)
39 #define wpa_dbg(args...) do { } while (0)
40
41 #else /* CONFIG_NO_STDOUT_DEBUG */
42
43 int wpa_debug_open_file(const char *path);
44 int wpa_debug_reopen_file(void);
45 void wpa_debug_close_file(void);
46
47 /**
48  * wpa_debug_printf_timestamp - Print timestamp for debug output
49  *
50  * This function prints a timestamp in seconds_from_1970.microsoconds
51  * format if debug output has been configured to include timestamps in debug
52  * messages.
53  */
54 void wpa_debug_print_timestamp(void);
55
56 /**
57  * wpa_printf - conditional printf
58  * @level: priority level (MSG_*) of the message
59  * @fmt: printf format string, followed by optional arguments
60  *
61  * This function is used to print conditional debugging and error messages. The
62  * output may be directed to stdout, stderr, and/or syslog based on
63  * configuration.
64  *
65  * Note: New line '\n' is added to the end of the text when printing to stdout.
66  */
67 void wpa_printf(int level, const char *fmt, ...)
68 PRINTF_FORMAT(2, 3);
69
70 /**
71  * wpa_hexdump - conditional hex dump
72  * @level: priority level (MSG_*) of the message
73  * @title: title of for the message
74  * @buf: data buffer to be dumped
75  * @len: length of the buf
76  *
77  * This function is used to print conditional debugging and error messages. The
78  * output may be directed to stdout, stderr, and/or syslog based on
79  * configuration. The contents of buf is printed out has hex dump.
80  */
81 void wpa_hexdump(int level, const char *title, const u8 *buf, size_t len);
82
83 static inline void wpa_hexdump_buf(int level, const char *title,
84                                    const struct wpabuf *buf)
85 {
86         wpa_hexdump(level, title, buf ? wpabuf_head(buf) : NULL,
87                     buf ? wpabuf_len(buf) : 0);
88 }
89
90 /**
91  * wpa_hexdump_key - conditional hex dump, hide keys
92  * @level: priority level (MSG_*) of the message
93  * @title: title of for the message
94  * @buf: data buffer to be dumped
95  * @len: length of the buf
96  *
97  * This function is used to print conditional debugging and error messages. The
98  * output may be directed to stdout, stderr, and/or syslog based on
99  * configuration. The contents of buf is printed out has hex dump. This works
100  * like wpa_hexdump(), but by default, does not include secret keys (passwords,
101  * etc.) in debug output.
102  */
103 void wpa_hexdump_key(int level, const char *title, const u8 *buf, size_t len);
104
105 static inline void wpa_hexdump_buf_key(int level, const char *title,
106                                        const struct wpabuf *buf)
107 {
108         wpa_hexdump_key(level, title, buf ? wpabuf_head(buf) : 0,
109                         buf ? wpabuf_len(buf) : 0);
110 }
111
112 /**
113  * wpa_hexdump_ascii - conditional hex dump
114  * @level: priority level (MSG_*) of the message
115  * @title: title of for the message
116  * @buf: data buffer to be dumped
117  * @len: length of the buf
118  *
119  * This function is used to print conditional debugging and error messages. The
120  * output may be directed to stdout, stderr, and/or syslog based on
121  * configuration. The contents of buf is printed out has hex dump with both
122  * the hex numbers and ASCII characters (for printable range) are shown. 16
123  * bytes per line will be shown.
124  */
125 void wpa_hexdump_ascii(int level, const char *title, const u8 *buf,
126                        size_t len);
127
128 /**
129  * wpa_hexdump_ascii_key - conditional hex dump, hide keys
130  * @level: priority level (MSG_*) of the message
131  * @title: title of for the message
132  * @buf: data buffer to be dumped
133  * @len: length of the buf
134  *
135  * This function is used to print conditional debugging and error messages. The
136  * output may be directed to stdout, stderr, and/or syslog based on
137  * configuration. The contents of buf is printed out has hex dump with both
138  * the hex numbers and ASCII characters (for printable range) are shown. 16
139  * bytes per line will be shown. This works like wpa_hexdump_ascii(), but by
140  * default, does not include secret keys (passwords, etc.) in debug output.
141  */
142 void wpa_hexdump_ascii_key(int level, const char *title, const u8 *buf,
143                            size_t len);
144
145 /*
146  * wpa_dbg() behaves like wpa_msg(), but it can be removed from build to reduce
147  * binary size. As such, it should be used with debugging messages that are not
148  * needed in the control interface while wpa_msg() has to be used for anything
149  * that needs to shown to control interface monitors.
150  */
151 #define wpa_dbg(args...) wpa_msg(args)
152
153 #endif /* CONFIG_NO_STDOUT_DEBUG */
154
155
156 #ifdef CONFIG_NO_WPA_MSG
157 #define wpa_msg(args...) do { } while (0)
158 #define wpa_msg_ctrl(args...) do { } while (0)
159 #define wpa_msg_register_cb(f) do { } while (0)
160 #else /* CONFIG_NO_WPA_MSG */
161 /**
162  * wpa_msg - Conditional printf for default target and ctrl_iface monitors
163  * @ctx: Pointer to context data; this is the ctx variable registered
164  *      with struct wpa_driver_ops::init()
165  * @level: priority level (MSG_*) of the message
166  * @fmt: printf format string, followed by optional arguments
167  *
168  * This function is used to print conditional debugging and error messages. The
169  * output may be directed to stdout, stderr, and/or syslog based on
170  * configuration. This function is like wpa_printf(), but it also sends the
171  * same message to all attached ctrl_iface monitors.
172  *
173  * Note: New line '\n' is added to the end of the text when printing to stdout.
174  */
175 void wpa_msg(void *ctx, int level, const char *fmt, ...) PRINTF_FORMAT(3, 4);
176
177 /**
178  * wpa_msg_ctrl - Conditional printf for ctrl_iface monitors
179  * @ctx: Pointer to context data; this is the ctx variable registered
180  *      with struct wpa_driver_ops::init()
181  * @level: priority level (MSG_*) of the message
182  * @fmt: printf format string, followed by optional arguments
183  *
184  * This function is used to print conditional debugging and error messages.
185  * This function is like wpa_msg(), but it sends the output only to the
186  * attached ctrl_iface monitors. In other words, it can be used for frequent
187  * events that do not need to be sent to syslog.
188  */
189 void wpa_msg_ctrl(void *ctx, int level, const char *fmt, ...)
190 PRINTF_FORMAT(3, 4);
191
192 typedef void (*wpa_msg_cb_func)(void *ctx, int level, const char *txt,
193                                 size_t len);
194
195 /**
196  * wpa_msg_register_cb - Register callback function for wpa_msg() messages
197  * @func: Callback function (%NULL to unregister)
198  */
199 void wpa_msg_register_cb(wpa_msg_cb_func func);
200
201 typedef const char * (*wpa_msg_get_ifname_func)(void *ctx);
202 void wpa_msg_register_ifname_cb(wpa_msg_get_ifname_func func);
203
204 #endif /* CONFIG_NO_WPA_MSG */
205
206 #ifdef CONFIG_NO_HOSTAPD_LOGGER
207 #define hostapd_logger(args...) do { } while (0)
208 #define hostapd_logger_register_cb(f) do { } while (0)
209 #else /* CONFIG_NO_HOSTAPD_LOGGER */
210 void hostapd_logger(void *ctx, const u8 *addr, unsigned int module, int level,
211                     const char *fmt, ...) PRINTF_FORMAT(5, 6);
212
213 typedef void (*hostapd_logger_cb_func)(void *ctx, const u8 *addr,
214                                        unsigned int module, int level,
215                                        const char *txt, size_t len);
216
217 /**
218  * hostapd_logger_register_cb - Register callback function for hostapd_logger()
219  * @func: Callback function (%NULL to unregister)
220  */
221 void hostapd_logger_register_cb(hostapd_logger_cb_func func);
222 #endif /* CONFIG_NO_HOSTAPD_LOGGER */
223
224 #define HOSTAPD_MODULE_IEEE80211        0x00000001
225 #define HOSTAPD_MODULE_IEEE8021X        0x00000002
226 #define HOSTAPD_MODULE_RADIUS           0x00000004
227 #define HOSTAPD_MODULE_WPA              0x00000008
228 #define HOSTAPD_MODULE_DRIVER           0x00000010
229 #define HOSTAPD_MODULE_IAPP             0x00000020
230 #define HOSTAPD_MODULE_MLME             0x00000040
231
232 enum hostapd_logger_level {
233         HOSTAPD_LEVEL_DEBUG_VERBOSE = 0,
234         HOSTAPD_LEVEL_DEBUG = 1,
235         HOSTAPD_LEVEL_INFO = 2,
236         HOSTAPD_LEVEL_NOTICE = 3,
237         HOSTAPD_LEVEL_WARNING = 4
238 };
239
240
241 #ifdef CONFIG_DEBUG_SYSLOG
242
243 void wpa_debug_open_syslog(void);
244 void wpa_debug_close_syslog(void);
245
246 #else /* CONFIG_DEBUG_SYSLOG */
247
248 static inline void wpa_debug_open_syslog(void)
249 {
250 }
251
252 static inline void wpa_debug_close_syslog(void)
253 {
254 }
255
256 #endif /* CONFIG_DEBUG_SYSLOG */
257
258
259 #ifdef EAPOL_TEST
260 #define WPA_ASSERT(a)                                                  \
261         do {                                                           \
262                 if (!(a)) {                                            \
263                         printf("WPA_ASSERT FAILED '" #a "' "           \
264                                "%s %s:%d\n",                           \
265                                __FUNCTION__, __FILE__, __LINE__);      \
266                         exit(1);                                       \
267                 }                                                      \
268         } while (0)
269 #else
270 #define WPA_ASSERT(a) do { } while (0)
271 #endif
272
273 #endif /* WPA_DEBUG_H */