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.
20 #ifdef CONFIG_DEBUG_FILE
21 static FILE *out_file
= NULL
;
22 #endif /* CONFIG_DEBUG_FILE */
23 int wpa_debug_level
= MSG_INFO
;
24 int wpa_debug_show_keys
= 0;
25 int wpa_debug_timestamp
= 0;
28 #ifndef CONFIG_NO_STDOUT_DEBUG
30 void wpa_debug_print_timestamp(void)
34 if (!wpa_debug_timestamp
)
38 #ifdef CONFIG_DEBUG_FILE
40 fprintf(out_file
, "%ld.%06u: ", (long) tv
.sec
,
41 (unsigned int) tv
.usec
);
43 #endif /* CONFIG_DEBUG_FILE */
44 printf("%ld.%06u: ", (long) tv
.sec
, (unsigned int) tv
.usec
);
49 * wpa_printf - conditional printf
50 * @level: priority level (MSG_*) of the message
51 * @fmt: printf format string, followed by optional arguments
53 * This function is used to print conditional debugging and error messages. The
54 * output may be directed to stdout, stderr, and/or syslog based on
57 * Note: New line '\n' is added to the end of the text when printing to stdout.
59 void wpa_printf(int level
, char *fmt
, ...)
64 if (level
>= wpa_debug_level
) {
65 wpa_debug_print_timestamp();
66 #ifdef CONFIG_DEBUG_FILE
68 vfprintf(out_file
, fmt
, ap
);
69 fprintf(out_file
, "\n");
71 #endif /* CONFIG_DEBUG_FILE */
74 #ifdef CONFIG_DEBUG_FILE
76 #endif /* CONFIG_DEBUG_FILE */
82 static void _wpa_hexdump(int level
, const char *title
, const u8
*buf
,
86 if (level
< wpa_debug_level
)
88 wpa_debug_print_timestamp();
89 #ifdef CONFIG_DEBUG_FILE
91 fprintf(out_file
, "%s - hexdump(len=%lu):",
92 title
, (unsigned long) len
);
94 fprintf(out_file
, " [NULL]");
96 for (i
= 0; i
< len
; i
++)
97 fprintf(out_file
, " %02x", buf
[i
]);
99 fprintf(out_file
, " [REMOVED]");
101 fprintf(out_file
, "\n");
103 #endif /* CONFIG_DEBUG_FILE */
104 printf("%s - hexdump(len=%lu):", title
, (unsigned long) len
);
108 for (i
= 0; i
< len
; i
++)
109 printf(" %02x", buf
[i
]);
111 printf(" [REMOVED]");
114 #ifdef CONFIG_DEBUG_FILE
116 #endif /* CONFIG_DEBUG_FILE */
119 void wpa_hexdump(int level
, const char *title
, const u8
*buf
, size_t len
)
121 _wpa_hexdump(level
, title
, buf
, len
, 1);
125 void wpa_hexdump_key(int level
, const char *title
, const u8
*buf
, size_t len
)
127 _wpa_hexdump(level
, title
, buf
, len
, wpa_debug_show_keys
);
131 static void _wpa_hexdump_ascii(int level
, const char *title
, const u8
*buf
,
132 size_t len
, int show
)
136 const size_t line_len
= 16;
138 if (level
< wpa_debug_level
)
140 wpa_debug_print_timestamp();
141 #ifdef CONFIG_DEBUG_FILE
145 "%s - hexdump_ascii(len=%lu): [REMOVED]\n",
146 title
, (unsigned long) len
);
151 "%s - hexdump_ascii(len=%lu): [NULL]\n",
152 title
, (unsigned long) len
);
155 fprintf(out_file
, "%s - hexdump_ascii(len=%lu):\n",
156 title
, (unsigned long) len
);
158 llen
= len
> line_len
? line_len
: len
;
159 fprintf(out_file
, " ");
160 for (i
= 0; i
< llen
; i
++)
161 fprintf(out_file
, " %02x", pos
[i
]);
162 for (i
= llen
; i
< line_len
; i
++)
163 fprintf(out_file
, " ");
164 fprintf(out_file
, " ");
165 for (i
= 0; i
< llen
; i
++) {
167 fprintf(out_file
, "%c", pos
[i
]);
169 fprintf(out_file
, "_");
171 for (i
= llen
; i
< line_len
; i
++)
172 fprintf(out_file
, " ");
173 fprintf(out_file
, "\n");
178 #endif /* CONFIG_DEBUG_FILE */
180 printf("%s - hexdump_ascii(len=%lu): [REMOVED]\n",
181 title
, (unsigned long) len
);
185 printf("%s - hexdump_ascii(len=%lu): [NULL]\n",
186 title
, (unsigned long) len
);
189 printf("%s - hexdump_ascii(len=%lu):\n", title
, (unsigned long) len
);
191 llen
= len
> line_len
? line_len
: len
;
193 for (i
= 0; i
< llen
; i
++)
194 printf(" %02x", pos
[i
]);
195 for (i
= llen
; i
< line_len
; i
++)
198 for (i
= 0; i
< llen
; i
++) {
200 printf("%c", pos
[i
]);
204 for (i
= llen
; i
< line_len
; i
++)
210 #ifdef CONFIG_DEBUG_FILE
212 #endif /* CONFIG_DEBUG_FILE */
216 void wpa_hexdump_ascii(int level
, const char *title
, const u8
*buf
, size_t len
)
218 _wpa_hexdump_ascii(level
, title
, buf
, len
, 1);
222 void wpa_hexdump_ascii_key(int level
, const char *title
, const u8
*buf
,
225 _wpa_hexdump_ascii(level
, title
, buf
, len
, wpa_debug_show_keys
);
229 int wpa_debug_open_file(const char *path
)
231 #ifdef CONFIG_DEBUG_FILE
234 out_file
= fopen(path
, "a");
235 if (out_file
== NULL
) {
236 wpa_printf(MSG_ERROR
, "wpa_debug_open_file: Failed to open "
237 "output file, using standard output");
241 setvbuf(out_file
, NULL
, _IOLBF
, 0);
243 #endif /* CONFIG_DEBUG_FILE */
248 void wpa_debug_close_file(void)
250 #ifdef CONFIG_DEBUG_FILE
255 #endif /* CONFIG_DEBUG_FILE */
258 #endif /* CONFIG_NO_STDOUT_DEBUG */
261 #ifndef CONFIG_NO_WPA_MSG
262 static wpa_msg_cb_func wpa_msg_cb
= NULL
;
264 void wpa_msg_register_cb(wpa_msg_cb_func func
)
270 void wpa_msg(void *ctx
, int level
, char *fmt
, ...)
274 const int buflen
= 2048;
277 buf
= os_malloc(buflen
);
279 wpa_printf(MSG_ERROR
, "wpa_msg: Failed to allocate message "
284 len
= vsnprintf(buf
, buflen
, fmt
, ap
);
286 wpa_printf(level
, "%s", buf
);
288 wpa_msg_cb(ctx
, level
, buf
, len
);
291 #endif /* CONFIG_NO_WPA_MSG */
294 #ifndef CONFIG_NO_HOSTAPD_LOGGER
295 static hostapd_logger_cb_func hostapd_logger_cb
= NULL
;
297 void hostapd_logger_register_cb(hostapd_logger_cb_func func
)
299 hostapd_logger_cb
= func
;
303 void hostapd_logger(void *ctx
, const u8
*addr
, unsigned int module
, int level
,
304 const char *fmt
, ...)
308 const int buflen
= 2048;
311 buf
= os_malloc(buflen
);
313 wpa_printf(MSG_ERROR
, "hostapd_logger: Failed to allocate "
318 len
= vsnprintf(buf
, buflen
, fmt
, ap
);
320 if (hostapd_logger_cb
)
321 hostapd_logger_cb(ctx
, addr
, module
, level
, buf
, len
);
323 wpa_printf(MSG_DEBUG
, "hostapd_logger: %s", buf
);
326 #endif /* CONFIG_NO_HOSTAPD_LOGGER */