From 16dea1557767435d5504f24da49a612e6a352c96 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Sun, 30 Aug 2015 16:32:29 +0200 Subject: rdisc: refactor logging and use common _LOGD() macro --- src/rdisc/nm-fake-rdisc.c | 4 +--- src/rdisc/nm-lndp-rdisc.c | 14 ++++++-------- src/rdisc/nm-rdisc-private.h | 29 +++++++++++++++++++++++++++++ src/rdisc/nm-rdisc.c | 42 +++++++++++++++++++++--------------------- 4 files changed, 57 insertions(+), 32 deletions(-) diff --git a/src/rdisc/nm-fake-rdisc.c b/src/rdisc/nm-fake-rdisc.c index 25ba3f7fb7..36f386c36f 100644 --- a/src/rdisc/nm-fake-rdisc.c +++ b/src/rdisc/nm-fake-rdisc.c @@ -28,9 +28,7 @@ #include "nm-default.h" -#define debug(...) nm_log_dbg (LOGD_IP6, __VA_ARGS__) -#define warning(...) nm_log_warn (LOGD_IP6, __VA_ARGS__) -#define error(...) nm_log_err (LOGD_IP6, __VA_ARGS__) +#define _NMLOG_PREFIX_NAME "rdisc-fake" typedef struct { guint id; diff --git a/src/rdisc/nm-lndp-rdisc.c b/src/rdisc/nm-lndp-rdisc.c index f31a13b2a1..a65ab03239 100644 --- a/src/rdisc/nm-lndp-rdisc.c +++ b/src/rdisc/nm-lndp-rdisc.c @@ -33,9 +33,7 @@ #include "nm-default.h" #include "nm-platform.h" -#define debug(...) nm_log_dbg (LOGD_IP6, __VA_ARGS__) -#define warning(...) nm_log_warn (LOGD_IP6, __VA_ARGS__) -#define error(...) nm_log_err (LOGD_IP6, __VA_ARGS__) +#define _NMLOG_PREFIX_NAME "rdisc-lndp" typedef struct { struct ndp *ndp; @@ -65,7 +63,7 @@ send_rs (NMRDisc *rdisc) error = ndp_msg_send (priv->ndp, msg); ndp_msg_destroy (msg); if (error) { - error ("(%s): cannot send router solicitation: %d.", rdisc->ifname, error); + _LOGE ("cannot send router solicitation: %d.", error); return FALSE; } @@ -109,7 +107,7 @@ receive_ra (struct ndp *ndp, struct ndp_msg *msg, gpointer user_data) * single time when the configuration is finished and updates can * come at any time. */ - debug ("(%s): received router advertisement at %u", rdisc->ifname, now); + _LOGD ("received router advertisement at %u", now); /* DHCP level: * @@ -260,7 +258,7 @@ receive_ra (struct ndp *ndp, struct ndp_msg *msg, gpointer user_data) * Kernel would set it, but would flush out all IPv6 addresses away * from the link, even the link-local, and we wouldn't be able to * listen for further RAs that could fix the MTU. */ - warning ("(%s): MTU too small for IPv6 ignored: %d", rdisc->ifname, mtu); + _LOGW ("MTU too small for IPv6 ignored: %d", mtu); } } @@ -273,7 +271,7 @@ event_ready (GIOChannel *source, GIOCondition condition, NMRDisc *rdisc) { NMLNDPRDiscPrivate *priv = NM_LNDP_RDISC_GET_PRIVATE (rdisc); - debug ("(%s): processing libndp events.", rdisc->ifname); + _LOGD ("processing libndp events"); ndp_callall_eventfd_handler (priv->ndp); return G_SOURCE_CONTINUE; } @@ -323,8 +321,8 @@ nm_lndp_rdisc_new (int ifindex, const char *ifname) priv = NM_LNDP_RDISC_GET_PRIVATE (rdisc); error = ndp_open (&priv->ndp); if (error != 0) { + _LOGD ("error creating socket for NDP; errno=%d", -error); g_object_unref (rdisc); - debug ("(%s): error creating socket for NDP; errno=%d", ifname, -error); return NULL; } return rdisc; diff --git a/src/rdisc/nm-rdisc-private.h b/src/rdisc/nm-rdisc-private.h index f95949def7..59c0cf3757 100644 --- a/src/rdisc/nm-rdisc-private.h +++ b/src/rdisc/nm-rdisc-private.h @@ -33,4 +33,33 @@ gboolean nm_rdisc_add_route (NMRDisc *rdisc, const NMRDiscRoute *new); gboolean nm_rdisc_add_dns_server (NMRDisc *rdisc, const NMRDiscDNSServer *new); gboolean nm_rdisc_add_dns_domain (NMRDisc *rdisc, const NMRDiscDNSDomain *new); +/*********************************************************************************************/ + +#define _NMLOG_DOMAIN LOGD_IP6 +#define _NMLOG(level, ...) _LOG(level, _NMLOG_DOMAIN, rdisc, __VA_ARGS__) + +#define _LOG(level, domain, self, ...) \ + G_STMT_START { \ + const NMLogLevel __level = (level); \ + const NMLogDomain __domain = (domain); \ + \ + if (nm_logging_enabled (__level, __domain)) { \ + char __prefix[64]; \ + const char *__p_prefix = _NMLOG_PREFIX_NAME; \ + const NMRDisc *const __self = (self); \ + \ + if (__self) { \ + g_snprintf (__prefix, sizeof (__prefix), "%s[%p,%s%s%s]", \ + _NMLOG_PREFIX_NAME, __self, \ + NM_PRINT_FMT_QUOTE_STRING (__self->ifname)); \ + __p_prefix = __prefix; \ + } \ + _nm_log (__level, __domain, 0, \ + "%s: " _NM_UTILS_MACRO_FIRST (__VA_ARGS__), \ + __p_prefix _NM_UTILS_MACRO_REST (__VA_ARGS__)); \ + } \ + } G_STMT_END + +/*********************************************************************************************/ + #endif /* __NETWORKMANAGER_RDISC_PRIVATE_H__ */ diff --git a/src/rdisc/nm-rdisc.c b/src/rdisc/nm-rdisc.c index 35436e9d11..cad3a90eed 100644 --- a/src/rdisc/nm-rdisc.c +++ b/src/rdisc/nm-rdisc.c @@ -30,7 +30,7 @@ #include "nm-default.h" #include "nm-utils.h" -#define debug(...) nm_log_dbg (LOGD_IP6, __VA_ARGS__) +#define _NMLOG_PREFIX_NAME "rdisc" typedef struct { int solicitations_left; @@ -243,7 +243,7 @@ nm_rdisc_set_iid (NMRDisc *rdisc, const NMUtilsIPv6IfaceId iid) if (rdisc->iid.id != iid.id) { rdisc->iid = iid; if (rdisc->addresses->len) { - debug ("(%s) IPv6 interface identifier changed, flushing addresses", rdisc->ifname); + _LOGD ("IPv6 interface identifier changed, flushing addresses"); g_array_remove_range (rdisc->addresses, 0, rdisc->addresses->len); g_signal_emit_by_name (rdisc, NM_RDISC_CONFIG_CHANGED, NM_RDISC_CONFIG_ADDRESSES); } @@ -281,20 +281,20 @@ send_rs (NMRDisc *rdisc) NMRDiscClass *klass = NM_RDISC_GET_CLASS (rdisc); NMRDiscPrivate *priv = NM_RDISC_GET_PRIVATE (rdisc); - debug ("(%s): sending router solicitation", rdisc->ifname); + _LOGD ("sending router solicitation"); if (klass->send_rs (rdisc)) priv->solicitations_left--; priv->last_rs = nm_utils_get_monotonic_timestamp_s (); if (priv->solicitations_left > 0) { - debug ("(%s): scheduling router solicitation retry in %d seconds.", - rdisc->ifname, rdisc->rtr_solicitation_interval); + _LOGD ("scheduling router solicitation retry in %d seconds.", + rdisc->rtr_solicitation_interval); priv->send_rs_id = g_timeout_add_seconds (rdisc->rtr_solicitation_interval, (GSourceFunc) send_rs, rdisc); } else { - debug ("(%s): did not receive a router advertisement after %d solicitations.", - rdisc->ifname, rdisc->rtr_solicitations); + _LOGD ("did not receive a router advertisement after %d solicitations.", + rdisc->rtr_solicitations); priv->send_rs_id = 0; } @@ -312,8 +312,8 @@ solicit (NMRDisc *rdisc) priv->solicitations_left = rdisc->rtr_solicitations; next = CLAMP (priv->last_rs + rdisc->rtr_solicitation_interval - now, 0, G_MAXINT32); - debug ("(%s): scheduling explicit router solicitation request in %" G_GINT64_FORMAT " seconds.", - rdisc->ifname, next); + _LOGD ("scheduling explicit router solicitation request in %" G_GINT64_FORMAT " seconds.", + next); priv->send_rs_id = g_timeout_add_seconds ((guint32) next, (GSourceFunc) send_rs, rdisc); } } @@ -337,12 +337,12 @@ nm_rdisc_start (NMRDisc *rdisc) g_assert (klass->start); - debug ("(%s): starting router discovery: %d", rdisc->ifname, rdisc->ifindex); + _LOGD ("starting router discovery: %d", rdisc->ifindex); clear_ra_timeout (rdisc); ra_wait_secs = CLAMP (rdisc->rtr_solicitations * rdisc->rtr_solicitation_interval, 30, 120); priv->ra_timeout_id = g_timeout_add_seconds (ra_wait_secs, rdisc_ra_timeout_cb, rdisc); - debug ("(%s): scheduling RA timeout in %d seconds", rdisc->ifname, ra_wait_secs); + _LOGD ("scheduling RA timeout in %d seconds", ra_wait_secs); if (klass->start) klass->start (rdisc); @@ -394,27 +394,27 @@ config_changed (NMRDisc *rdisc, NMRDiscConfigMap changed) char changedstr[CONFIG_MAP_MAX_STR]; char addrstr[INET6_ADDRSTRLEN]; - if (nm_logging_enabled (LOGL_DEBUG, LOGD_IP6)) { + if (_LOGD_ENABLED ()) { config_map_to_string (changed, changedstr); - debug ("(%s): router discovery configuration changed [%s]:", rdisc->ifname, changedstr); - debug (" dhcp-level %s", dhcp_level_to_string (rdisc->dhcp_level)); + _LOGD ("router discovery configuration changed [%s]:", changedstr); + _LOGD (" dhcp-level %s", dhcp_level_to_string (rdisc->dhcp_level)); for (i = 0; i < rdisc->gateways->len; i++) { NMRDiscGateway *gateway = &g_array_index (rdisc->gateways, NMRDiscGateway, i); inet_ntop (AF_INET6, &gateway->address, addrstr, sizeof (addrstr)); - debug (" gateway %s pref %d exp %u", addrstr, gateway->preference, expiry (gateway)); + _LOGD (" gateway %s pref %d exp %u", addrstr, gateway->preference, expiry (gateway)); } for (i = 0; i < rdisc->addresses->len; i++) { NMRDiscAddress *address = &g_array_index (rdisc->addresses, NMRDiscAddress, i); inet_ntop (AF_INET6, &address->address, addrstr, sizeof (addrstr)); - debug (" address %s exp %u", addrstr, expiry (address)); + _LOGD (" address %s exp %u", addrstr, expiry (address)); } for (i = 0; i < rdisc->routes->len; i++) { NMRDiscRoute *route = &g_array_index (rdisc->routes, NMRDiscRoute, i); inet_ntop (AF_INET6, &route->network, addrstr, sizeof (addrstr)); - debug (" route %s/%d via %s pref %d exp %u", addrstr, route->plen, + _LOGD (" route %s/%d via %s pref %d exp %u", addrstr, route->plen, nm_utils_inet6_ntop (&route->gateway, NULL), route->preference, expiry (route)); } @@ -422,12 +422,12 @@ config_changed (NMRDisc *rdisc, NMRDiscConfigMap changed) NMRDiscDNSServer *dns_server = &g_array_index (rdisc->dns_servers, NMRDiscDNSServer, i); inet_ntop (AF_INET6, &dns_server->address, addrstr, sizeof (addrstr)); - debug (" dns_server %s exp %u", addrstr, expiry (dns_server)); + _LOGD (" dns_server %s exp %u", addrstr, expiry (dns_server)); } for (i = 0; i < rdisc->dns_domains->len; i++) { NMRDiscDNSDomain *dns_domain = &g_array_index (rdisc->dns_domains, NMRDiscDNSDomain, i); - debug (" dns_domain %s exp %u", dns_domain->domain, expiry (dns_domain)); + _LOGD (" dns_domain %s exp %u", dns_domain->domain, expiry (dns_domain)); } } } @@ -564,8 +564,8 @@ check_timestamps (NMRDisc *rdisc, guint32 now, NMRDiscConfigMap changed) if (nextevent != never) { g_return_if_fail (nextevent > now); - debug ("(%s): scheduling next now/lifetime check: %u seconds", - rdisc->ifname, nextevent - now); + _LOGD ("scheduling next now/lifetime check: %u seconds", + nextevent - now); priv->timeout_id = g_timeout_add_seconds (nextevent - now, timeout_cb, rdisc); } } -- cgit v1.2.1