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 int wpa_debug_level = MSG_INFO;
27 int wpa_debug_show_keys = 0;
28 int wpa_debug_timestamp = 0;
31 #ifdef CONFIG_ANDROID_LOG
33 #include <android/log.h>
35 void android_printf(int level, char *format, ...)
37 if (level >= wpa_debug_level) {
39 if (level == MSG_ERROR)
40 level = ANDROID_LOG_ERROR;
41 else if (level == MSG_WARNING)
42 level = ANDROID_LOG_WARN;
43 else if (level == MSG_INFO)
44 level = ANDROID_LOG_INFO;
46 level = ANDROID_LOG_DEBUG;
48 __android_log_vprint(level, "wpa_supplicant", format, ap);
53 #else /* CONFIG_ANDROID_LOG */
55 #ifndef CONFIG_NO_STDOUT_DEBUG
57 #ifdef CONFIG_DEBUG_FILE
58 static FILE *out_file = NULL;
59 #endif /* CONFIG_DEBUG_FILE */
62 void wpa_debug_print_timestamp(void)
66 if (!wpa_debug_timestamp)
70 #ifdef CONFIG_DEBUG_FILE
72 fprintf(out_file, "%ld.%06u: ", (long) tv.sec,
73 (unsigned int) tv.usec);
75 #endif /* CONFIG_DEBUG_FILE */
76 printf("%ld.%06u: ", (long) tv.sec, (unsigned int) tv.usec);
80 #ifdef CONFIG_DEBUG_SYSLOG
82 #define LOG_HOSTAPD LOG_DAEMON
83 #endif /* LOG_HOSTAPD */
85 void wpa_debug_open_syslog(void)
87 openlog("wpa_supplicant", LOG_PID | LOG_NDELAY, LOG_HOSTAPD);
92 void wpa_debug_close_syslog(void)
99 static int syslog_priority(int level)
114 #endif /* CONFIG_DEBUG_SYSLOG */
118 * wpa_printf - conditional printf
119 * @level: priority level (MSG_*) of the message
120 * @fmt: printf format string, followed by optional arguments
122 * This function is used to print conditional debugging and error messages. The
123 * output may be directed to stdout, stderr, and/or syslog based on
126 * Note: New line '\n' is added to the end of the text when printing to stdout.
128 void wpa_printf(int level, const char *fmt, ...)
133 if (level >= wpa_debug_level) {
134 #ifdef CONFIG_DEBUG_SYSLOG
135 if (wpa_debug_syslog) {
136 vsyslog(syslog_priority(level), fmt, ap);
138 #endif /* CONFIG_DEBUG_SYSLOG */
139 wpa_debug_print_timestamp();
140 #ifdef CONFIG_DEBUG_FILE
142 vfprintf(out_file, fmt, ap);
143 fprintf(out_file, "\n");
145 #endif /* CONFIG_DEBUG_FILE */
148 #ifdef CONFIG_DEBUG_FILE
150 #endif /* CONFIG_DEBUG_FILE */
151 #ifdef CONFIG_DEBUG_SYSLOG
153 #endif /* CONFIG_DEBUG_SYSLOG */
159 static void _wpa_hexdump(int level, const char *title, const u8 *buf,
160 size_t len, int show)
163 if (level < wpa_debug_level)
165 wpa_debug_print_timestamp();
166 #ifdef CONFIG_DEBUG_FILE
168 fprintf(out_file, "%s - hexdump(len=%lu):",
169 title, (unsigned long) len);
171 fprintf(out_file, " [NULL]");
173 for (i = 0; i < len; i++)
174 fprintf(out_file, " %02x", buf[i]);
176 fprintf(out_file, " [REMOVED]");
178 fprintf(out_file, "\n");
180 #endif /* CONFIG_DEBUG_FILE */
181 printf("%s - hexdump(len=%lu):", title, (unsigned long) len);
185 for (i = 0; i < len; i++)
186 printf(" %02x", buf[i]);
188 printf(" [REMOVED]");
191 #ifdef CONFIG_DEBUG_FILE
193 #endif /* CONFIG_DEBUG_FILE */
196 void wpa_hexdump(int level, const char *title, const u8 *buf, size_t len)
198 _wpa_hexdump(level, title, buf, len, 1);
202 void wpa_hexdump_key(int level, const char *title, const u8 *buf, size_t len)
204 _wpa_hexdump(level, title, buf, len, wpa_debug_show_keys);
208 static void _wpa_hexdump_ascii(int level, const char *title, const u8 *buf,
209 size_t len, int show)
213 const size_t line_len = 16;
215 if (level < wpa_debug_level)
217 wpa_debug_print_timestamp();
218 #ifdef CONFIG_DEBUG_FILE
222 "%s - hexdump_ascii(len=%lu): [REMOVED]\n",
223 title, (unsigned long) len);
228 "%s - hexdump_ascii(len=%lu): [NULL]\n",
229 title, (unsigned long) len);
232 fprintf(out_file, "%s - hexdump_ascii(len=%lu):\n",
233 title, (unsigned long) len);
235 llen = len > line_len ? line_len : len;
236 fprintf(out_file, " ");
237 for (i = 0; i < llen; i++)
238 fprintf(out_file, " %02x", pos[i]);
239 for (i = llen; i < line_len; i++)
240 fprintf(out_file, " ");
241 fprintf(out_file, " ");
242 for (i = 0; i < llen; i++) {
244 fprintf(out_file, "%c", pos[i]);
246 fprintf(out_file, "_");
248 for (i = llen; i < line_len; i++)
249 fprintf(out_file, " ");
250 fprintf(out_file, "\n");
255 #endif /* CONFIG_DEBUG_FILE */
257 printf("%s - hexdump_ascii(len=%lu): [REMOVED]\n",
258 title, (unsigned long) len);
262 printf("%s - hexdump_ascii(len=%lu): [NULL]\n",
263 title, (unsigned long) len);
266 printf("%s - hexdump_ascii(len=%lu):\n", title, (unsigned long) len);
268 llen = len > line_len ? line_len : len;
270 for (i = 0; i < llen; i++)
271 printf(" %02x", pos[i]);
272 for (i = llen; i < line_len; i++)
275 for (i = 0; i < llen; i++) {
277 printf("%c", pos[i]);
281 for (i = llen; i < line_len; i++)
287 #ifdef CONFIG_DEBUG_FILE
289 #endif /* CONFIG_DEBUG_FILE */
293 void wpa_hexdump_ascii(int level, const char *title, const u8 *buf, size_t len)
295 _wpa_hexdump_ascii(level, title, buf, len, 1);
299 void wpa_hexdump_ascii_key(int level, const char *title, const u8 *buf,
302 _wpa_hexdump_ascii(level, title, buf, len, wpa_debug_show_keys);
306 #ifdef CONFIG_DEBUG_FILE
307 static char *last_path = NULL;
308 #endif /* CONFIG_DEBUG_FILE */
310 int wpa_debug_reopen_file(void)
312 #ifdef CONFIG_DEBUG_FILE
315 char *tmp = os_strdup(last_path);
316 wpa_debug_close_file();
317 rv = wpa_debug_open_file(tmp);
320 wpa_printf(MSG_ERROR, "Last-path was not set, cannot "
321 "re-open log file.");
325 #else /* CONFIG_DEBUG_FILE */
327 #endif /* CONFIG_DEBUG_FILE */
331 int wpa_debug_open_file(const char *path)
333 #ifdef CONFIG_DEBUG_FILE
337 if (last_path == NULL || os_strcmp(last_path, path) != 0) {
338 /* Save our path to enable re-open */
340 last_path = os_strdup(path);
343 out_file = fopen(path, "a");
344 if (out_file == NULL) {
345 wpa_printf(MSG_ERROR, "wpa_debug_open_file: Failed to open "
346 "output file, using standard output");
350 setvbuf(out_file, NULL, _IOLBF, 0);
352 #endif /* CONFIG_DEBUG_FILE */
357 void wpa_debug_close_file(void)
359 #ifdef CONFIG_DEBUG_FILE
366 #endif /* CONFIG_DEBUG_FILE */
369 #endif /* CONFIG_NO_STDOUT_DEBUG */
371 #endif /* CONFIG_ANDROID_LOG */
373 #ifndef CONFIG_NO_WPA_MSG
374 static wpa_msg_cb_func wpa_msg_cb = NULL;
376 void wpa_msg_register_cb(wpa_msg_cb_func func)
382 static wpa_msg_get_ifname_func wpa_msg_ifname_cb = NULL;
384 void wpa_msg_register_ifname_cb(wpa_msg_get_ifname_func func)
386 wpa_msg_ifname_cb = func;
390 void wpa_msg(void *ctx, int level, const char *fmt, ...)
394 const int buflen = 2048;
398 buf = os_malloc(buflen);
400 wpa_printf(MSG_ERROR, "wpa_msg: Failed to allocate message "
406 if (wpa_msg_ifname_cb) {
407 const char *ifname = wpa_msg_ifname_cb(ctx);
409 int res = os_snprintf(prefix, sizeof(prefix), "%s: ",
411 if (res < 0 || res >= (int) sizeof(prefix))
415 len = vsnprintf(buf, buflen, fmt, ap);
417 wpa_printf(level, "%s%s", prefix, buf);
419 wpa_msg_cb(ctx, level, buf, len);
424 void wpa_msg_ctrl(void *ctx, int level, const char *fmt, ...)
428 const int buflen = 2048;
434 buf = os_malloc(buflen);
436 wpa_printf(MSG_ERROR, "wpa_msg_ctrl: Failed to allocate "
441 len = vsnprintf(buf, buflen, fmt, ap);
443 wpa_msg_cb(ctx, level, buf, len);
446 #endif /* CONFIG_NO_WPA_MSG */
449 #ifndef CONFIG_NO_HOSTAPD_LOGGER
450 static hostapd_logger_cb_func hostapd_logger_cb = NULL;
452 void hostapd_logger_register_cb(hostapd_logger_cb_func func)
454 hostapd_logger_cb = func;
458 void hostapd_logger(void *ctx, const u8 *addr, unsigned int module, int level,
459 const char *fmt, ...)
463 const int buflen = 2048;
466 buf = os_malloc(buflen);
468 wpa_printf(MSG_ERROR, "hostapd_logger: Failed to allocate "
473 len = vsnprintf(buf, buflen, fmt, ap);
475 if (hostapd_logger_cb)
476 hostapd_logger_cb(ctx, addr, module, level, buf, len);
478 wpa_printf(MSG_DEBUG, "hostapd_logger: STA " MACSTR " - %s",
481 wpa_printf(MSG_DEBUG, "hostapd_logger: %s", buf);
484 #endif /* CONFIG_NO_HOSTAPD_LOGGER */