2 * wpa_supplicant/hostapd / Debug prints
3 * Copyright (c) 2002-2007, Jouni Malinen <j@w1.fi>
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.
9 * Alternatively, this software may be distributed under the terms of BSD
12 * See README and COPYING for more details.
19 #ifdef CONFIG_DEBUG_SYSLOG
22 static int wpa_debug_syslog
= 0;
23 #endif /* CONFIG_DEBUG_SYSLOG */
26 #ifdef CONFIG_DEBUG_FILE
27 static FILE *out_file
= NULL
;
28 #endif /* CONFIG_DEBUG_FILE */
29 int wpa_debug_level
= MSG_INFO
;
30 int wpa_debug_show_keys
= 0;
31 int wpa_debug_timestamp
= 0;
34 #ifndef CONFIG_NO_STDOUT_DEBUG
36 void wpa_debug_print_timestamp(void)
40 if (!wpa_debug_timestamp
)
44 #ifdef CONFIG_DEBUG_FILE
46 fprintf(out_file
, "%ld.%06u: ", (long) tv
.sec
,
47 (unsigned int) tv
.usec
);
49 #endif /* CONFIG_DEBUG_FILE */
50 printf("%ld.%06u: ", (long) tv
.sec
, (unsigned int) tv
.usec
);
54 #ifdef CONFIG_DEBUG_SYSLOG
55 void wpa_debug_open_syslog(void)
57 openlog("wpa_supplicant", LOG_PID
| LOG_NDELAY
, LOG_DAEMON
);
62 void wpa_debug_close_syslog(void)
69 static int syslog_priority(int level
)
84 #endif /* CONFIG_DEBUG_SYSLOG */
88 * wpa_printf - conditional printf
89 * @level: priority level (MSG_*) of the message
90 * @fmt: printf format string, followed by optional arguments
92 * This function is used to print conditional debugging and error messages. The
93 * output may be directed to stdout, stderr, and/or syslog based on
96 * Note: New line '\n' is added to the end of the text when printing to stdout.
98 void wpa_printf(int level
, char *fmt
, ...)
103 if (level
>= wpa_debug_level
) {
104 #ifdef CONFIG_DEBUG_SYSLOG
105 if (wpa_debug_syslog
) {
106 vsyslog(syslog_priority(level
), fmt
, ap
);
108 #endif /* CONFIG_DEBUG_SYSLOG */
109 wpa_debug_print_timestamp();
110 #ifdef CONFIG_DEBUG_FILE
112 vfprintf(out_file
, fmt
, ap
);
113 fprintf(out_file
, "\n");
115 #endif /* CONFIG_DEBUG_FILE */
118 #ifdef CONFIG_DEBUG_FILE
120 #endif /* CONFIG_DEBUG_FILE */
121 #ifdef CONFIG_DEBUG_SYSLOG
123 #endif /* CONFIG_DEBUG_SYSLOG */
129 static void _wpa_hexdump(int level
, const char *title
, const u8
*buf
,
130 size_t len
, int show
)
133 if (level
< wpa_debug_level
)
135 wpa_debug_print_timestamp();
136 #ifdef CONFIG_DEBUG_FILE
138 fprintf(out_file
, "%s - hexdump(len=%lu):",
139 title
, (unsigned long) len
);
141 fprintf(out_file
, " [NULL]");
143 for (i
= 0; i
< len
; i
++)
144 fprintf(out_file
, " %02x", buf
[i
]);
146 fprintf(out_file
, " [REMOVED]");
148 fprintf(out_file
, "\n");
150 #endif /* CONFIG_DEBUG_FILE */
151 printf("%s - hexdump(len=%lu):", title
, (unsigned long) len
);
155 for (i
= 0; i
< len
; i
++)
156 printf(" %02x", buf
[i
]);
158 printf(" [REMOVED]");
161 #ifdef CONFIG_DEBUG_FILE
163 #endif /* CONFIG_DEBUG_FILE */
166 void wpa_hexdump(int level
, const char *title
, const u8
*buf
, size_t len
)
168 _wpa_hexdump(level
, title
, buf
, len
, 1);
172 void wpa_hexdump_key(int level
, const char *title
, const u8
*buf
, size_t len
)
174 _wpa_hexdump(level
, title
, buf
, len
, wpa_debug_show_keys
);
178 static void _wpa_hexdump_ascii(int level
, const char *title
, const u8
*buf
,
179 size_t len
, int show
)
183 const size_t line_len
= 16;
185 if (level
< wpa_debug_level
)
187 wpa_debug_print_timestamp();
188 #ifdef CONFIG_DEBUG_FILE
192 "%s - hexdump_ascii(len=%lu): [REMOVED]\n",
193 title
, (unsigned long) len
);
198 "%s - hexdump_ascii(len=%lu): [NULL]\n",
199 title
, (unsigned long) len
);
202 fprintf(out_file
, "%s - hexdump_ascii(len=%lu):\n",
203 title
, (unsigned long) len
);
205 llen
= len
> line_len
? line_len
: len
;
206 fprintf(out_file
, " ");
207 for (i
= 0; i
< llen
; i
++)
208 fprintf(out_file
, " %02x", pos
[i
]);
209 for (i
= llen
; i
< line_len
; i
++)
210 fprintf(out_file
, " ");
211 fprintf(out_file
, " ");
212 for (i
= 0; i
< llen
; i
++) {
214 fprintf(out_file
, "%c", pos
[i
]);
216 fprintf(out_file
, "_");
218 for (i
= llen
; i
< line_len
; i
++)
219 fprintf(out_file
, " ");
220 fprintf(out_file
, "\n");
225 #endif /* CONFIG_DEBUG_FILE */
227 printf("%s - hexdump_ascii(len=%lu): [REMOVED]\n",
228 title
, (unsigned long) len
);
232 printf("%s - hexdump_ascii(len=%lu): [NULL]\n",
233 title
, (unsigned long) len
);
236 printf("%s - hexdump_ascii(len=%lu):\n", title
, (unsigned long) len
);
238 llen
= len
> line_len
? line_len
: len
;
240 for (i
= 0; i
< llen
; i
++)
241 printf(" %02x", pos
[i
]);
242 for (i
= llen
; i
< line_len
; i
++)
245 for (i
= 0; i
< llen
; i
++) {
247 printf("%c", pos
[i
]);
251 for (i
= llen
; i
< line_len
; i
++)
257 #ifdef CONFIG_DEBUG_FILE
259 #endif /* CONFIG_DEBUG_FILE */
263 void wpa_hexdump_ascii(int level
, const char *title
, const u8
*buf
, size_t len
)
265 _wpa_hexdump_ascii(level
, title
, buf
, len
, 1);
269 void wpa_hexdump_ascii_key(int level
, const char *title
, const u8
*buf
,
272 _wpa_hexdump_ascii(level
, title
, buf
, len
, wpa_debug_show_keys
);
276 int wpa_debug_open_file(const char *path
)
278 #ifdef CONFIG_DEBUG_FILE
281 out_file
= fopen(path
, "a");
282 if (out_file
== NULL
) {
283 wpa_printf(MSG_ERROR
, "wpa_debug_open_file: Failed to open "
284 "output file, using standard output");
288 setvbuf(out_file
, NULL
, _IOLBF
, 0);
290 #endif /* CONFIG_DEBUG_FILE */
295 void wpa_debug_close_file(void)
297 #ifdef CONFIG_DEBUG_FILE
302 #endif /* CONFIG_DEBUG_FILE */
305 #endif /* CONFIG_NO_STDOUT_DEBUG */
308 #ifndef CONFIG_NO_WPA_MSG
309 static wpa_msg_cb_func wpa_msg_cb
= NULL
;
311 void wpa_msg_register_cb(wpa_msg_cb_func func
)
317 void wpa_msg(void *ctx
, int level
, char *fmt
, ...)
321 const int buflen
= 2048;
324 buf
= os_malloc(buflen
);
326 wpa_printf(MSG_ERROR
, "wpa_msg: Failed to allocate message "
331 len
= vsnprintf(buf
, buflen
, fmt
, ap
);
333 wpa_printf(level
, "%s", buf
);
335 wpa_msg_cb(ctx
, level
, buf
, len
);
340 void wpa_msg_ctrl(void *ctx
, int level
, char *fmt
, ...)
344 const int buflen
= 2048;
350 buf
= os_malloc(buflen
);
352 wpa_printf(MSG_ERROR
, "wpa_msg_ctrl: Failed to allocate "
357 len
= vsnprintf(buf
, buflen
, fmt
, ap
);
359 wpa_msg_cb(ctx
, level
, buf
, len
);
362 #endif /* CONFIG_NO_WPA_MSG */
365 #ifndef CONFIG_NO_HOSTAPD_LOGGER
366 static hostapd_logger_cb_func hostapd_logger_cb
= NULL
;
368 void hostapd_logger_register_cb(hostapd_logger_cb_func func
)
370 hostapd_logger_cb
= func
;
374 void hostapd_logger(void *ctx
, const u8
*addr
, unsigned int module
, int level
,
375 const char *fmt
, ...)
379 const int buflen
= 2048;
382 buf
= os_malloc(buflen
);
384 wpa_printf(MSG_ERROR
, "hostapd_logger: Failed to allocate "
389 len
= vsnprintf(buf
, buflen
, fmt
, ap
);
391 if (hostapd_logger_cb
)
392 hostapd_logger_cb(ctx
, addr
, module
, level
, buf
, len
);
394 wpa_printf(MSG_DEBUG
, "hostapd_logger: %s", buf
);
397 #endif /* CONFIG_NO_HOSTAPD_LOGGER */