Add interface name to wpa_msg() output

This makes log files much more readable if multiple interfaces
are being controlled by the same process. The interface name is
added to stdout/file/syslog entries, but not to the messages
sent to control interface monitors to avoid issues with parsing
in external programs.

Signed-off-by: Ben Greear <greearb@candelatech.com>
This commit is contained in:
Ben Greear 2011-02-06 20:39:34 +02:00 committed by Jouni Malinen
parent b41a47c03f
commit 4f1495aefa
3 changed files with 35 additions and 2 deletions

View file

@ -348,12 +348,21 @@ void wpa_msg_register_cb(wpa_msg_cb_func func)
} }
static wpa_msg_get_ifname_func wpa_msg_ifname_cb = NULL;
void wpa_msg_register_ifname_cb(wpa_msg_get_ifname_func func)
{
wpa_msg_ifname_cb = func;
}
void wpa_msg(void *ctx, int level, const char *fmt, ...) void wpa_msg(void *ctx, int level, const char *fmt, ...)
{ {
va_list ap; va_list ap;
char *buf; char *buf;
const int buflen = 2048; const int buflen = 2048;
int len; int len;
char prefix[130];
buf = os_malloc(buflen); buf = os_malloc(buflen);
if (buf == NULL) { if (buf == NULL) {
@ -362,9 +371,19 @@ void wpa_msg(void *ctx, int level, const char *fmt, ...)
return; return;
} }
va_start(ap, fmt); va_start(ap, fmt);
prefix[0] = '\0';
if (wpa_msg_ifname_cb) {
const char *ifname = wpa_msg_ifname_cb(ctx);
if (ifname) {
int res = os_snprintf(prefix, sizeof(prefix), "%s: ",
ifname);
if (res < 0 || res >= (int) sizeof(prefix))
prefix[0] = '\0';
}
}
len = vsnprintf(buf, buflen, fmt, ap); len = vsnprintf(buf, buflen, fmt, ap);
va_end(ap); va_end(ap);
wpa_printf(level, "%s", buf); wpa_printf(level, "%s%s", prefix, buf);
if (wpa_msg_cb) if (wpa_msg_cb)
wpa_msg_cb(ctx, level, buf, len); wpa_msg_cb(ctx, level, buf, len);
os_free(buf); os_free(buf);

View file

@ -188,8 +188,11 @@ typedef void (*wpa_msg_cb_func)(void *ctx, int level, const char *txt,
* @func: Callback function (%NULL to unregister) * @func: Callback function (%NULL to unregister)
*/ */
void wpa_msg_register_cb(wpa_msg_cb_func func); void wpa_msg_register_cb(wpa_msg_cb_func func);
#endif /* CONFIG_NO_WPA_MSG */
typedef const char * (*wpa_msg_get_ifname_func)(void *ctx);
void wpa_msg_register_ifname_cb(wpa_msg_get_ifname_func func);
#endif /* CONFIG_NO_WPA_MSG */
#ifdef CONFIG_NO_HOSTAPD_LOGGER #ifdef CONFIG_NO_HOSTAPD_LOGGER
#define hostapd_logger(args...) do { } while (0) #define hostapd_logger(args...) do { } while (0)

View file

@ -2333,6 +2333,15 @@ struct wpa_supplicant * wpa_supplicant_get_iface(struct wpa_global *global,
} }
static const char * wpa_supplicant_msg_ifname_cb(void *ctx)
{
struct wpa_supplicant *wpa_s = ctx;
if (wpa_s == NULL)
return NULL;
return wpa_s->ifname;
}
/** /**
* wpa_supplicant_init - Initialize %wpa_supplicant * wpa_supplicant_init - Initialize %wpa_supplicant
* @params: Parameters for %wpa_supplicant * @params: Parameters for %wpa_supplicant
@ -2350,6 +2359,8 @@ struct wpa_global * wpa_supplicant_init(struct wpa_params *params)
if (params == NULL) if (params == NULL)
return NULL; return NULL;
wpa_msg_register_ifname_cb(wpa_supplicant_msg_ifname_cb);
wpa_debug_open_file(params->wpa_debug_file_path); wpa_debug_open_file(params->wpa_debug_file_path);
if (params->wpa_debug_syslog) if (params->wpa_debug_syslog)
wpa_debug_open_syslog(); wpa_debug_open_syslog();