2 * wpa_supplicant/hostapd / Debug prints
3 * Copyright (c) 2002-2007, Jouni Malinen <j@w1.fi>
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
13 #ifdef CONFIG_DEBUG_SYSLOG
16 static int wpa_debug_syslog = 0;
17 #endif /* CONFIG_DEBUG_SYSLOG */
20 int wpa_debug_level = MSG_INFO;
21 int wpa_debug_show_keys = 0;
22 int wpa_debug_timestamp = 0;
25 #ifdef CONFIG_ANDROID_LOG
27 #include <android/log.h>
29 #ifndef ANDROID_LOG_NAME
30 #define ANDROID_LOG_NAME "wpa_supplicant"
31 #endif /* ANDROID_LOG_NAME */
33 static int wpa_to_android_level(int level)
35 if (level == MSG_ERROR)
36 return ANDROID_LOG_ERROR;
37 if (level == MSG_WARNING)
38 return ANDROID_LOG_WARN;
39 if (level == MSG_INFO)
40 return ANDROID_LOG_INFO;
41 return ANDROID_LOG_DEBUG;
44 #endif /* CONFIG_ANDROID_LOG */
46 #ifndef CONFIG_NO_STDOUT_DEBUG
48 #ifdef CONFIG_DEBUG_FILE
49 static FILE *out_file = NULL;
50 #endif /* CONFIG_DEBUG_FILE */
53 void wpa_debug_print_timestamp(void)
55 #ifndef CONFIG_ANDROID_LOG
58 if (!wpa_debug_timestamp)
62 #ifdef CONFIG_DEBUG_FILE
64 fprintf(out_file, "%ld.%06u: ", (long) tv.sec,
65 (unsigned int) tv.usec);
67 #endif /* CONFIG_DEBUG_FILE */
68 printf("%ld.%06u: ", (long) tv.sec, (unsigned int) tv.usec);
69 #endif /* CONFIG_ANDROID_LOG */
73 #ifdef CONFIG_DEBUG_SYSLOG
75 #define LOG_HOSTAPD LOG_DAEMON
76 #endif /* LOG_HOSTAPD */
78 void wpa_debug_open_syslog(void)
80 openlog("wpa_supplicant", LOG_PID | LOG_NDELAY, LOG_HOSTAPD);
85 void wpa_debug_close_syslog(void)
92 static int syslog_priority(int level)
107 #endif /* CONFIG_DEBUG_SYSLOG */
111 * wpa_printf - conditional printf
112 * @level: priority level (MSG_*) of the message
113 * @fmt: printf format string, followed by optional arguments
115 * This function is used to print conditional debugging and error messages. The
116 * output may be directed to stdout, stderr, and/or syslog based on
119 * Note: New line '\n' is added to the end of the text when printing to stdout.
121 void wpa_printf(int level, const char *fmt, ...)
126 if (level >= wpa_debug_level) {
127 #ifdef CONFIG_ANDROID_LOG
128 __android_log_vprint(wpa_to_android_level(level),
129 ANDROID_LOG_NAME, fmt, ap);
130 #else /* CONFIG_ANDROID_LOG */
131 #ifdef CONFIG_DEBUG_SYSLOG
132 if (wpa_debug_syslog) {
133 vsyslog(syslog_priority(level), fmt, ap);
135 #endif /* CONFIG_DEBUG_SYSLOG */
136 wpa_debug_print_timestamp();
137 #ifdef CONFIG_DEBUG_FILE
139 vfprintf(out_file, fmt, ap);
140 fprintf(out_file, "\n");
142 #endif /* CONFIG_DEBUG_FILE */
145 #ifdef CONFIG_DEBUG_FILE
147 #endif /* CONFIG_DEBUG_FILE */
148 #ifdef CONFIG_DEBUG_SYSLOG
150 #endif /* CONFIG_DEBUG_SYSLOG */
151 #endif /* CONFIG_ANDROID_LOG */
157 static void _wpa_hexdump(int level, const char *title, const u8 *buf,
158 size_t len, int show)
161 if (level < wpa_debug_level)
163 #ifdef CONFIG_ANDROID_LOG
170 } else if (len == 0) {
172 } else if (show && len) {
173 /* Limit debug message length for Android log */
176 strbuf = os_malloc(1 + 3 * slen);
177 if (strbuf == NULL) {
178 wpa_printf(MSG_ERROR, "wpa_hexdump: Failed to "
179 "allocate message buffer");
183 for (i = 0; i < slen; i++)
184 os_snprintf(&strbuf[i * 3], 4, " %02x",
189 display = " [REMOVED]";
192 __android_log_print(wpa_to_android_level(level),
194 "%s - hexdump(len=%lu):%s%s",
195 title, (long unsigned int) len, display,
196 len > slen ? " ..." : "");
200 #else /* CONFIG_ANDROID_LOG */
201 #ifdef CONFIG_DEBUG_SYSLOG
202 if (wpa_debug_syslog) {
208 } else if (len == 0) {
210 } else if (show && len) {
211 strbuf = os_malloc(1 + 3 * len);
212 if (strbuf == NULL) {
213 wpa_printf(MSG_ERROR, "wpa_hexdump: Failed to "
214 "allocate message buffer");
218 for (i = 0; i < len; i++)
219 os_snprintf(&strbuf[i * 3], 4, " %02x",
224 display = " [REMOVED]";
227 syslog(syslog_priority(level), "%s - hexdump(len=%lu):%s",
228 title, len, display);
232 #endif /* CONFIG_DEBUG_SYSLOG */
233 wpa_debug_print_timestamp();
234 #ifdef CONFIG_DEBUG_FILE
236 fprintf(out_file, "%s - hexdump(len=%lu):",
237 title, (unsigned long) len);
239 fprintf(out_file, " [NULL]");
241 for (i = 0; i < len; i++)
242 fprintf(out_file, " %02x", buf[i]);
244 fprintf(out_file, " [REMOVED]");
246 fprintf(out_file, "\n");
248 #endif /* CONFIG_DEBUG_FILE */
249 printf("%s - hexdump(len=%lu):", title, (unsigned long) len);
253 for (i = 0; i < len; i++)
254 printf(" %02x", buf[i]);
256 printf(" [REMOVED]");
259 #ifdef CONFIG_DEBUG_FILE
261 #endif /* CONFIG_DEBUG_FILE */
262 #endif /* CONFIG_ANDROID_LOG */
265 void wpa_hexdump(int level, const char *title, const u8 *buf, size_t len)
267 _wpa_hexdump(level, title, buf, len, 1);
271 void wpa_hexdump_key(int level, const char *title, const u8 *buf, size_t len)
273 _wpa_hexdump(level, title, buf, len, wpa_debug_show_keys);
277 static void _wpa_hexdump_ascii(int level, const char *title, const u8 *buf,
278 size_t len, int show)
282 const size_t line_len = 16;
284 if (level < wpa_debug_level)
286 #ifdef CONFIG_ANDROID_LOG
287 _wpa_hexdump(level, title, buf, len, show);
288 #else /* CONFIG_ANDROID_LOG */
289 wpa_debug_print_timestamp();
290 #ifdef CONFIG_DEBUG_FILE
294 "%s - hexdump_ascii(len=%lu): [REMOVED]\n",
295 title, (unsigned long) len);
300 "%s - hexdump_ascii(len=%lu): [NULL]\n",
301 title, (unsigned long) len);
304 fprintf(out_file, "%s - hexdump_ascii(len=%lu):\n",
305 title, (unsigned long) len);
307 llen = len > line_len ? line_len : len;
308 fprintf(out_file, " ");
309 for (i = 0; i < llen; i++)
310 fprintf(out_file, " %02x", pos[i]);
311 for (i = llen; i < line_len; i++)
312 fprintf(out_file, " ");
313 fprintf(out_file, " ");
314 for (i = 0; i < llen; i++) {
316 fprintf(out_file, "%c", pos[i]);
318 fprintf(out_file, "_");
320 for (i = llen; i < line_len; i++)
321 fprintf(out_file, " ");
322 fprintf(out_file, "\n");
327 #endif /* CONFIG_DEBUG_FILE */
329 printf("%s - hexdump_ascii(len=%lu): [REMOVED]\n",
330 title, (unsigned long) len);
334 printf("%s - hexdump_ascii(len=%lu): [NULL]\n",
335 title, (unsigned long) len);
338 printf("%s - hexdump_ascii(len=%lu):\n", title, (unsigned long) len);
340 llen = len > line_len ? line_len : len;
342 for (i = 0; i < llen; i++)
343 printf(" %02x", pos[i]);
344 for (i = llen; i < line_len; i++)
347 for (i = 0; i < llen; i++) {
349 printf("%c", pos[i]);
353 for (i = llen; i < line_len; i++)
359 #ifdef CONFIG_DEBUG_FILE
361 #endif /* CONFIG_DEBUG_FILE */
362 #endif /* CONFIG_ANDROID_LOG */
366 void wpa_hexdump_ascii(int level, const char *title, const u8 *buf, size_t len)
368 _wpa_hexdump_ascii(level, title, buf, len, 1);
372 void wpa_hexdump_ascii_key(int level, const char *title, const u8 *buf,
375 _wpa_hexdump_ascii(level, title, buf, len, wpa_debug_show_keys);
379 #ifdef CONFIG_DEBUG_FILE
380 static char *last_path = NULL;
381 #endif /* CONFIG_DEBUG_FILE */
383 int wpa_debug_reopen_file(void)
385 #ifdef CONFIG_DEBUG_FILE
388 char *tmp = os_strdup(last_path);
389 wpa_debug_close_file();
390 rv = wpa_debug_open_file(tmp);
393 wpa_printf(MSG_ERROR, "Last-path was not set, cannot "
394 "re-open log file.");
398 #else /* CONFIG_DEBUG_FILE */
400 #endif /* CONFIG_DEBUG_FILE */
404 int wpa_debug_open_file(const char *path)
406 #ifdef CONFIG_DEBUG_FILE
410 if (last_path == NULL || os_strcmp(last_path, path) != 0) {
411 /* Save our path to enable re-open */
413 last_path = os_strdup(path);
416 out_file = fopen(path, "a");
417 if (out_file == NULL) {
418 wpa_printf(MSG_ERROR, "wpa_debug_open_file: Failed to open "
419 "output file, using standard output");
423 setvbuf(out_file, NULL, _IOLBF, 0);
425 #endif /* CONFIG_DEBUG_FILE */
430 void wpa_debug_close_file(void)
432 #ifdef CONFIG_DEBUG_FILE
439 #endif /* CONFIG_DEBUG_FILE */
442 #endif /* CONFIG_NO_STDOUT_DEBUG */
445 #ifndef CONFIG_NO_WPA_MSG
446 static wpa_msg_cb_func wpa_msg_cb = NULL;
448 void wpa_msg_register_cb(wpa_msg_cb_func func)
454 static wpa_msg_get_ifname_func wpa_msg_ifname_cb = NULL;
456 void wpa_msg_register_ifname_cb(wpa_msg_get_ifname_func func)
458 wpa_msg_ifname_cb = func;
462 void wpa_msg(void *ctx, int level, const char *fmt, ...)
466 const int buflen = 2048;
470 buf = os_malloc(buflen);
472 wpa_printf(MSG_ERROR, "wpa_msg: Failed to allocate message "
478 if (wpa_msg_ifname_cb) {
479 const char *ifname = wpa_msg_ifname_cb(ctx);
481 int res = os_snprintf(prefix, sizeof(prefix), "%s: ",
483 if (res < 0 || res >= (int) sizeof(prefix))
487 len = vsnprintf(buf, buflen, fmt, ap);
489 wpa_printf(level, "%s%s", prefix, buf);
491 wpa_msg_cb(ctx, level, buf, len);
496 void wpa_msg_ctrl(void *ctx, int level, const char *fmt, ...)
500 const int buflen = 2048;
506 buf = os_malloc(buflen);
508 wpa_printf(MSG_ERROR, "wpa_msg_ctrl: Failed to allocate "
513 len = vsnprintf(buf, buflen, fmt, ap);
515 wpa_msg_cb(ctx, level, buf, len);
518 #endif /* CONFIG_NO_WPA_MSG */
521 #ifndef CONFIG_NO_HOSTAPD_LOGGER
522 static hostapd_logger_cb_func hostapd_logger_cb = NULL;
524 void hostapd_logger_register_cb(hostapd_logger_cb_func func)
526 hostapd_logger_cb = func;
530 void hostapd_logger(void *ctx, const u8 *addr, unsigned int module, int level,
531 const char *fmt, ...)
535 const int buflen = 2048;
538 buf = os_malloc(buflen);
540 wpa_printf(MSG_ERROR, "hostapd_logger: Failed to allocate "
545 len = vsnprintf(buf, buflen, fmt, ap);
547 if (hostapd_logger_cb)
548 hostapd_logger_cb(ctx, addr, module, level, buf, len);
550 wpa_printf(MSG_DEBUG, "hostapd_logger: STA " MACSTR " - %s",
553 wpa_printf(MSG_DEBUG, "hostapd_logger: %s", buf);
556 #endif /* CONFIG_NO_HOSTAPD_LOGGER */