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_use_file = 0;
24
int wpa_debug_level = MSG_INFO;
25
int wpa_debug_show_keys = 0;
26
int wpa_debug_timestamp = 0;
29
#ifndef CONFIG_NO_STDOUT_DEBUG
31
void wpa_debug_print_timestamp(void)
35
if (!wpa_debug_timestamp)
39
#ifdef CONFIG_DEBUG_FILE
41
fprintf(out_file, "%ld.%06u: ", (long) tv.sec,
42
(unsigned int) tv.usec);
44
#endif /* CONFIG_DEBUG_FILE */
45
printf("%ld.%06u: ", (long) tv.sec, (unsigned int) tv.usec);
50
* wpa_printf - conditional printf
51
* @level: priority level (MSG_*) of the message
52
* @fmt: printf format string, followed by optional arguments
54
* This function is used to print conditional debugging and error messages. The
55
* output may be directed to stdout, stderr, and/or syslog based on
58
* Note: New line '\n' is added to the end of the text when printing to stdout.
60
void wpa_printf(int level, char *fmt, ...)
65
if (level >= wpa_debug_level) {
66
wpa_debug_print_timestamp();
67
#ifdef CONFIG_DEBUG_FILE
69
vfprintf(out_file, fmt, ap);
70
fprintf(out_file, "\n");
72
#endif /* CONFIG_DEBUG_FILE */
75
#ifdef CONFIG_DEBUG_FILE
77
#endif /* CONFIG_DEBUG_FILE */
83
static void _wpa_hexdump(int level, const char *title, const u8 *buf,
87
if (level < wpa_debug_level)
89
wpa_debug_print_timestamp();
90
#ifdef CONFIG_DEBUG_FILE
92
fprintf(out_file, "%s - hexdump(len=%lu):",
93
title, (unsigned long) len);
95
fprintf(out_file, " [NULL]");
97
for (i = 0; i < len; i++)
98
fprintf(out_file, " %02x", buf[i]);
100
fprintf(out_file, " [REMOVED]");
102
fprintf(out_file, "\n");
104
#endif /* CONFIG_DEBUG_FILE */
105
printf("%s - hexdump(len=%lu):", title, (unsigned long) len);
109
for (i = 0; i < len; i++)
110
printf(" %02x", buf[i]);
112
printf(" [REMOVED]");
115
#ifdef CONFIG_DEBUG_FILE
117
#endif /* CONFIG_DEBUG_FILE */
120
void wpa_hexdump(int level, const char *title, const u8 *buf, size_t len)
122
_wpa_hexdump(level, title, buf, len, 1);
126
void wpa_hexdump_key(int level, const char *title, const u8 *buf, size_t len)
128
_wpa_hexdump(level, title, buf, len, wpa_debug_show_keys);
132
static void _wpa_hexdump_ascii(int level, const char *title, const u8 *buf,
133
size_t len, int show)
137
const size_t line_len = 16;
139
if (level < wpa_debug_level)
141
wpa_debug_print_timestamp();
142
#ifdef CONFIG_DEBUG_FILE
146
"%s - hexdump_ascii(len=%lu): [REMOVED]\n",
147
title, (unsigned long) len);
152
"%s - hexdump_ascii(len=%lu): [NULL]\n",
153
title, (unsigned long) len);
156
fprintf(out_file, "%s - hexdump_ascii(len=%lu):\n",
157
title, (unsigned long) len);
159
llen = len > line_len ? line_len : len;
160
fprintf(out_file, " ");
161
for (i = 0; i < llen; i++)
162
fprintf(out_file, " %02x", pos[i]);
163
for (i = llen; i < line_len; i++)
164
fprintf(out_file, " ");
165
fprintf(out_file, " ");
166
for (i = 0; i < llen; i++) {
168
fprintf(out_file, "%c", pos[i]);
170
fprintf(out_file, "_");
172
for (i = llen; i < line_len; i++)
173
fprintf(out_file, " ");
174
fprintf(out_file, "\n");
179
#endif /* CONFIG_DEBUG_FILE */
181
printf("%s - hexdump_ascii(len=%lu): [REMOVED]\n",
182
title, (unsigned long) len);
186
printf("%s - hexdump_ascii(len=%lu): [NULL]\n",
187
title, (unsigned long) len);
190
printf("%s - hexdump_ascii(len=%lu):\n", title, (unsigned long) len);
192
llen = len > line_len ? line_len : len;
194
for (i = 0; i < llen; i++)
195
printf(" %02x", pos[i]);
196
for (i = llen; i < line_len; i++)
199
for (i = 0; i < llen; i++) {
201
printf("%c", pos[i]);
205
for (i = llen; i < line_len; i++)
211
#ifdef CONFIG_DEBUG_FILE
213
#endif /* CONFIG_DEBUG_FILE */
217
void wpa_hexdump_ascii(int level, const char *title, const u8 *buf, size_t len)
219
_wpa_hexdump_ascii(level, title, buf, len, 1);
223
void wpa_hexdump_ascii_key(int level, const char *title, const u8 *buf,
226
_wpa_hexdump_ascii(level, title, buf, len, wpa_debug_show_keys);
230
int wpa_debug_open_file(void)
232
#ifdef CONFIG_DEBUG_FILE
233
static int count = 0;
235
if (!wpa_debug_use_file)
238
os_snprintf(fname, sizeof(fname), "\\Temp\\wpa_supplicant-log-%d.txt",
241
os_snprintf(fname, sizeof(fname), "/tmp/wpa_supplicant-log-%d.txt",
244
out_file = fopen(fname, "w");
245
return out_file == NULL ? -1 : 0;
246
#else /* CONFIG_DEBUG_FILE */
248
#endif /* CONFIG_DEBUG_FILE */
252
void wpa_debug_close_file(void)
254
#ifdef CONFIG_DEBUG_FILE
255
if (!wpa_debug_use_file)
259
#endif /* CONFIG_DEBUG_FILE */
262
#endif /* CONFIG_NO_STDOUT_DEBUG */
265
#ifndef CONFIG_NO_WPA_MSG
266
static wpa_msg_cb_func wpa_msg_cb = NULL;
268
void wpa_msg_register_cb(wpa_msg_cb_func func)
274
void wpa_msg(void *ctx, int level, char *fmt, ...)
278
const int buflen = 2048;
281
buf = os_malloc(buflen);
283
wpa_printf(MSG_ERROR, "wpa_msg: Failed to allocate message "
288
len = vsnprintf(buf, buflen, fmt, ap);
290
wpa_printf(level, "%s", buf);
292
wpa_msg_cb(ctx, level, buf, len);
295
#endif /* CONFIG_NO_WPA_MSG */
298
#ifndef CONFIG_NO_HOSTAPD_LOGGER
299
static hostapd_logger_cb_func hostapd_logger_cb = NULL;
301
void hostapd_logger_register_cb(hostapd_logger_cb_func func)
303
hostapd_logger_cb = func;
307
void hostapd_logger(void *ctx, const u8 *addr, unsigned int module, int level,
308
const char *fmt, ...)
312
const int buflen = 2048;
315
buf = os_malloc(buflen);
317
wpa_printf(MSG_ERROR, "hostapd_logger: Failed to allocate "
322
len = vsnprintf(buf, buflen, fmt, ap);
324
if (hostapd_logger_cb)
325
hostapd_logger_cb(ctx, addr, module, level, buf, len);
327
wpa_printf(MSG_DEBUG, "hostapd_logger: %s", buf);
330
#endif /* CONFIG_NO_HOSTAPD_LOGGER */