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)
41 * Oct, 26th. 2011. TIZEN
42 * Change time log's display expression like year-month-day hour:min:sec.milisec
48 if (!wpa_debug_timestamp)
53 ptm = localtime ( &tv.sec);
54 strftime(time_string,sizeof(time_string), "%Y-%m-%d %H:%M:%S",ptm);
55 #ifdef CONFIG_DEBUG_FILE
57 fprintf(out_file, "%s.%06u: ", time_string,
58 (unsigned int) tv.usec);
60 #endif /* CONFIG_DEBUG_FILE */
61 printf("%s.%06u: ", time_string, (unsigned int) tv.usec);
65 #ifdef CONFIG_DEBUG_SYSLOG
66 void wpa_debug_open_syslog(void)
68 openlog("wpa_supplicant", LOG_PID | LOG_NDELAY, LOG_DAEMON);
73 void wpa_debug_close_syslog(void)
80 static int syslog_priority(int level)
95 #endif /* CONFIG_DEBUG_SYSLOG */
99 * wpa_printf - conditional printf
100 * @level: priority level (MSG_*) of the message
101 * @fmt: printf format string, followed by optional arguments
103 * This function is used to print conditional debugging and error messages. The
104 * output may be directed to stdout, stderr, and/or syslog based on
107 * Note: New line '\n' is added to the end of the text when printing to stdout.
109 void wpa_printf(int level, const char *fmt, ...)
114 if (level >= wpa_debug_level) {
115 #ifdef CONFIG_DEBUG_SYSLOG
116 if (wpa_debug_syslog) {
117 vsyslog(syslog_priority(level), fmt, ap);
119 #endif /* CONFIG_DEBUG_SYSLOG */
120 wpa_debug_print_timestamp();
121 #ifdef CONFIG_DEBUG_FILE
123 vfprintf(out_file, fmt, ap);
124 fprintf(out_file, "\n");
126 #endif /* CONFIG_DEBUG_FILE */
129 #ifdef CONFIG_DEBUG_FILE
131 #endif /* CONFIG_DEBUG_FILE */
132 #ifdef CONFIG_DEBUG_SYSLOG
134 #endif /* CONFIG_DEBUG_SYSLOG */
140 static void _wpa_hexdump(int level, const char *title, const u8 *buf,
141 size_t len, int show)
144 if (level < wpa_debug_level)
146 wpa_debug_print_timestamp();
147 #ifdef CONFIG_DEBUG_FILE
149 fprintf(out_file, "%s - hexdump(len=%lu):",
150 title, (unsigned long) len);
152 fprintf(out_file, " [NULL]");
154 for (i = 0; i < len; i++)
155 fprintf(out_file, " %02x", buf[i]);
157 fprintf(out_file, " [REMOVED]");
159 fprintf(out_file, "\n");
161 #endif /* CONFIG_DEBUG_FILE */
162 printf("%s - hexdump(len=%lu):", title, (unsigned long) len);
166 for (i = 0; i < len; i++)
167 printf(" %02x", buf[i]);
169 printf(" [REMOVED]");
172 #ifdef CONFIG_DEBUG_FILE
174 #endif /* CONFIG_DEBUG_FILE */
177 void wpa_hexdump(int level, const char *title, const u8 *buf, size_t len)
179 _wpa_hexdump(level, title, buf, len, 1);
183 void wpa_hexdump_key(int level, const char *title, const u8 *buf, size_t len)
185 _wpa_hexdump(level, title, buf, len, wpa_debug_show_keys);
189 static void _wpa_hexdump_ascii(int level, const char *title, const u8 *buf,
190 size_t len, int show)
194 const size_t line_len = 16;
196 if (level < wpa_debug_level)
198 wpa_debug_print_timestamp();
199 #ifdef CONFIG_DEBUG_FILE
203 "%s - hexdump_ascii(len=%lu): [REMOVED]\n",
204 title, (unsigned long) len);
209 "%s - hexdump_ascii(len=%lu): [NULL]\n",
210 title, (unsigned long) len);
213 fprintf(out_file, "%s - hexdump_ascii(len=%lu):\n",
214 title, (unsigned long) len);
216 llen = len > line_len ? line_len : len;
217 fprintf(out_file, " ");
218 for (i = 0; i < llen; i++)
219 fprintf(out_file, " %02x", pos[i]);
220 for (i = llen; i < line_len; i++)
221 fprintf(out_file, " ");
222 fprintf(out_file, " ");
223 for (i = 0; i < llen; i++) {
225 fprintf(out_file, "%c", pos[i]);
227 fprintf(out_file, "_");
229 for (i = llen; i < line_len; i++)
230 fprintf(out_file, " ");
231 fprintf(out_file, "\n");
236 #endif /* CONFIG_DEBUG_FILE */
238 printf("%s - hexdump_ascii(len=%lu): [REMOVED]\n",
239 title, (unsigned long) len);
243 printf("%s - hexdump_ascii(len=%lu): [NULL]\n",
244 title, (unsigned long) len);
247 printf("%s - hexdump_ascii(len=%lu):\n", title, (unsigned long) len);
249 llen = len > line_len ? line_len : len;
251 for (i = 0; i < llen; i++)
252 printf(" %02x", pos[i]);
253 for (i = llen; i < line_len; i++)
256 for (i = 0; i < llen; i++) {
258 printf("%c", pos[i]);
262 for (i = llen; i < line_len; i++)
268 #ifdef CONFIG_DEBUG_FILE
270 #endif /* CONFIG_DEBUG_FILE */
274 void wpa_hexdump_ascii(int level, const char *title, const u8 *buf, size_t len)
276 _wpa_hexdump_ascii(level, title, buf, len, 1);
280 void wpa_hexdump_ascii_key(int level, const char *title, const u8 *buf,
283 _wpa_hexdump_ascii(level, title, buf, len, wpa_debug_show_keys);
287 int wpa_debug_open_file(const char *path)
289 #ifdef CONFIG_DEBUG_FILE
292 out_file = fopen(path, "a");
293 if (out_file == NULL) {
294 wpa_printf(MSG_ERROR, "wpa_debug_open_file: Failed to open "
295 "output file, using standard output");
299 setvbuf(out_file, NULL, _IOLBF, 0);
301 #endif /* CONFIG_DEBUG_FILE */
306 void wpa_debug_close_file(void)
308 #ifdef CONFIG_DEBUG_FILE
313 #endif /* CONFIG_DEBUG_FILE */
316 #endif /* CONFIG_NO_STDOUT_DEBUG */
319 #ifndef CONFIG_NO_WPA_MSG
320 static wpa_msg_cb_func wpa_msg_cb = NULL;
322 void wpa_msg_register_cb(wpa_msg_cb_func func)
328 void wpa_msg(void *ctx, int level, const char *fmt, ...)
332 const int buflen = 2048;
335 buf = os_malloc(buflen);
337 wpa_printf(MSG_ERROR, "wpa_msg: Failed to allocate message "
342 len = vsnprintf(buf, buflen, fmt, ap);
344 wpa_printf(level, "%s", buf);
346 wpa_msg_cb(ctx, level, buf, len);
351 void wpa_msg_ctrl(void *ctx, int level, const char *fmt, ...)
355 const int buflen = 2048;
361 buf = os_malloc(buflen);
363 wpa_printf(MSG_ERROR, "wpa_msg_ctrl: Failed to allocate "
368 len = vsnprintf(buf, buflen, fmt, ap);
370 wpa_msg_cb(ctx, level, buf, len);
373 #endif /* CONFIG_NO_WPA_MSG */
376 #ifndef CONFIG_NO_HOSTAPD_LOGGER
377 static hostapd_logger_cb_func hostapd_logger_cb = NULL;
379 void hostapd_logger_register_cb(hostapd_logger_cb_func func)
381 hostapd_logger_cb = func;
385 void hostapd_logger(void *ctx, const u8 *addr, unsigned int module, int level,
386 const char *fmt, ...)
390 const int buflen = 2048;
393 buf = os_malloc(buflen);
395 wpa_printf(MSG_ERROR, "hostapd_logger: Failed to allocate "
400 len = vsnprintf(buf, buflen, fmt, ap);
402 if (hostapd_logger_cb)
403 hostapd_logger_cb(ctx, addr, module, level, buf, len);
405 wpa_printf(MSG_DEBUG, "hostapd_logger: STA " MACSTR " - %s",
408 wpa_printf(MSG_DEBUG, "hostapd_logger: %s", buf);
411 #endif /* CONFIG_NO_HOSTAPD_LOGGER */