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
21 #endif /* CONFIG_DEBUG_SYSLOG */
24 #ifdef CONFIG_DEBUG_FILE
25 static FILE *out_file = NULL;
26 #endif /* CONFIG_DEBUG_FILE */
27 int wpa_debug_level = MSG_INFO;
28 int wpa_debug_show_keys = 0;
29 int wpa_debug_timestamp = 0;
30 int wpa_debug_syslog = 0;
33 #ifndef CONFIG_NO_STDOUT_DEBUG
35 void wpa_debug_print_timestamp(void)
39 if (!wpa_debug_timestamp)
43 #ifdef CONFIG_DEBUG_FILE
45 fprintf(out_file, "%ld.%06u: ", (long) tv.sec,
46 (unsigned int) tv.usec);
48 #endif /* CONFIG_DEBUG_FILE */
49 printf("%ld.%06u: ", (long) tv.sec, (unsigned int) tv.usec);
52 void wpa_debug_open_syslog(void)
54 #ifdef CONFIG_DEBUG_SYSLOG
55 openlog("wpa_supplicant", LOG_PID | LOG_NDELAY, LOG_DAEMON);
60 void wpa_debug_close_syslog(void)
62 #ifdef CONFIG_DEBUG_SYSLOG
68 #ifdef CONFIG_DEBUG_SYSLOG
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);
338 #endif /* CONFIG_NO_WPA_MSG */
341 #ifndef CONFIG_NO_HOSTAPD_LOGGER
342 static hostapd_logger_cb_func hostapd_logger_cb = NULL;
344 void hostapd_logger_register_cb(hostapd_logger_cb_func func)
346 hostapd_logger_cb = func;
350 void hostapd_logger(void *ctx, const u8 *addr, unsigned int module, int level,
351 const char *fmt, ...)
355 const int buflen = 2048;
358 buf = os_malloc(buflen);
360 wpa_printf(MSG_ERROR, "hostapd_logger: Failed to allocate "
365 len = vsnprintf(buf, buflen, fmt, ap);
367 if (hostapd_logger_cb)
368 hostapd_logger_cb(ctx, addr, module, level, buf, len);
370 wpa_printf(MSG_DEBUG, "hostapd_logger: %s", buf);
373 #endif /* CONFIG_NO_HOSTAPD_LOGGER */