From 64c271ed3bd50ce5ffbf69108d75bb79d279e8d9 Mon Sep 17 00:00:00 2001 From: Felix Fietkau Date: Tue, 4 Oct 2011 20:50:49 +0200 Subject: [PATCH] rework debugging code, add debugging levels --- config.c | 4 ++-- device.c | 10 +++++----- main.c | 7 ++++++- netifd.h | 20 ++++++++++++++++++++ system-dummy.c | 28 ++++++++++++++-------------- utils.h | 10 ---------- 6 files changed, 47 insertions(+), 32 deletions(-) diff --git a/config.c b/config.c index db69f9a..cb79b2a 100644 --- a/config.c +++ b/config.c @@ -132,7 +132,7 @@ config_parse_bridge_interface(struct uci_section *s) uci_to_blob(&b, s, bridge_device_type.config_params); if (!device_create(name, &bridge_device_type, b.head)) { - DPRINTF("Failed to create bridge for interface '%s'\n", s->e.name); + D(INTERFACE, "Failed to create bridge for interface '%s'\n", s->e.name); return -EINVAL; } @@ -148,7 +148,7 @@ config_parse_interface(struct uci_section *s) const char *type; struct blob_attr *config; - DPRINTF("Create interface '%s'\n", s->e.name); + D(INTERFACE, "Create interface '%s'\n", s->e.name); blob_buf_init(&b, 0); diff --git a/device.c b/device.c index ab8ff91..23befa3 100644 --- a/device.c +++ b/device.c @@ -133,7 +133,7 @@ int device_claim(struct device_user *dep) return 0; dep->claimed = true; - DPRINTF("claim device %s, new refcount: %d\n", dev->ifname, dev->active + 1); + D(DEVICE, "claim device %s, new refcount: %d\n", dev->ifname, dev->active + 1); if (++dev->active != 1) return 0; @@ -156,7 +156,7 @@ void device_release(struct device_user *dep) dep->claimed = false; dev->active--; - DPRINTF("release device %s, new refcount: %d\n", dev->ifname, dev->active); + D(DEVICE, "release device %s, new refcount: %d\n", dev->ifname, dev->active); assert(dev->active >= 0); if (dev->active) @@ -183,7 +183,7 @@ void device_init_virtual(struct device *dev, const struct device_type *type, con if (name) strncpy(dev->ifname, name, IFNAMSIZ); - DPRINTF("Initialize device '%s'\n", dev->ifname); + D(DEVICE, "Initialize device '%s'\n", dev->ifname); INIT_LIST_HEAD(&dev->users); dev->type = type; } @@ -232,7 +232,7 @@ void device_cleanup(struct device *dev) { struct device_user *dep, *tmp; - DPRINTF("Clean up device '%s'\n", dev->ifname); + D(DEVICE, "Clean up device '%s'\n", dev->ifname); list_for_each_entry_safe(dep, tmp, &dev->users, list) { if (!dep->cb) continue; @@ -249,7 +249,7 @@ void device_set_present(struct device *dev, bool state) if (dev->present == state) return; - DPRINTF("Device '%s' %s present\n", dev->ifname, state ? "is now" : "is no longer" ); + D(DEVICE, "Device '%s' %s present\n", dev->ifname, state ? "is now" : "is no longer" ); dev->present = state; device_broadcast_event(dev, state ? DEV_EVENT_ADD : DEV_EVENT_REMOVE); } diff --git a/main.c b/main.c index fe97ee1..465398f 100644 --- a/main.c +++ b/main.c @@ -10,6 +10,7 @@ #include "system.h" #include "interface.h" +unsigned int debug_mask = 0; const char *main_path = "."; static char **global_argv; @@ -42,6 +43,7 @@ static int usage(const char *progname) { fprintf(stderr, "Usage: %s [options]\n" "Options:\n" + " -d : Mask for debug messages\n" " -s : Path to the ubus socket\n" " -p : Path to netifd addons (default: %s)\n" "\n", progname, main_path); @@ -56,8 +58,11 @@ int main(int argc, char **argv) global_argv = argv; - while ((ch = getopt(argc, argv, "s:")) != -1) { + while ((ch = getopt(argc, argv, "d:s:")) != -1) { switch(ch) { + case 'd': + debug_mask = strtoul(optarg, NULL, 0); + break; case 's': socket = optarg; break; diff --git a/netifd.h b/netifd.h index fc2b6a7..a368d9b 100644 --- a/netifd.h +++ b/netifd.h @@ -13,6 +13,26 @@ #include "utils.h" +extern unsigned int debug_mask; + +enum { + DEBUG_SYSTEM = 0, + DEBUG_DEVICE = 1, + DEBUG_INTERFACE = 2, +}; + +#ifdef DEBUG +#define DPRINTF(format, ...) fprintf(stderr, "%s(%d): " format, __func__, __LINE__, ## __VA_ARGS__) +#define D(level, format, ...) if (debug_mask & (1 << (DEBUG_ ## level))) DPRINTF(format, ##__VA_ARGS__) +#else +#define DPRINTF(format, ...) no_debug(0, format, ## __VA_ARGS__) +#define D(level, format, ...) no_debug(DEBUG_ ## level, format, ## __VA_ARGS__) +#endif + +static inline void no_debug(int level, const char *fmt, ...) +{ +} + struct device; struct interface; diff --git a/system-dummy.c b/system-dummy.c index a5d352d..d1557f9 100644 --- a/system-dummy.c +++ b/system-dummy.c @@ -18,49 +18,49 @@ int system_init(void) int system_bridge_addbr(struct device *bridge) { - DPRINTF("brctl addbr %s\n", bridge->ifname); + D(SYSTEM, "brctl addbr %s\n", bridge->ifname); return 0; } int system_bridge_delbr(struct device *bridge) { - DPRINTF("brctl delbr %s\n", bridge->ifname); + D(SYSTEM, "brctl delbr %s\n", bridge->ifname); return 0; } int system_bridge_addif(struct device *bridge, struct device *dev) { - DPRINTF("brctl addif %s %s\n", bridge->ifname, dev->ifname); + D(SYSTEM, "brctl addif %s %s\n", bridge->ifname, dev->ifname); return 0; } int system_bridge_delif(struct device *bridge, struct device *dev) { - DPRINTF("brctl delif %s %s\n", bridge->ifname, dev->ifname); + D(SYSTEM, "brctl delif %s %s\n", bridge->ifname, dev->ifname); return 0; } int system_vlan_add(struct device *dev, int id) { - DPRINTF("vconfig add %s %d\n", dev->ifname, id); + D(SYSTEM, "vconfig add %s %d\n", dev->ifname, id); return 0; } int system_vlan_del(struct device *dev) { - DPRINTF("vconfig rem %s\n", dev->ifname); + D(SYSTEM, "vconfig rem %s\n", dev->ifname); return 0; } int system_if_up(struct device *dev) { - DPRINTF("ifconfig %s up\n", dev->ifname); + D(SYSTEM, "ifconfig %s up\n", dev->ifname); return 0; } int system_if_down(struct device *dev) { - DPRINTF("ifconfig %s down\n", dev->ifname); + D(SYSTEM, "ifconfig %s down\n", dev->ifname); return 0; } @@ -80,11 +80,11 @@ int system_add_address(struct device *dev, struct device_addr *addr) char ipaddr[64]; if ((addr->flags & DEVADDR_FAMILY) == DEVADDR_INET4) { - DPRINTF("ifconfig %s add %d.%d.%d.%d/%d\n", + D(SYSTEM, "ifconfig %s add %d.%d.%d.%d/%d\n", dev->ifname, a[0], a[1], a[2], a[3], addr->mask); } else { inet_ntop(AF_INET6, &addr->addr.in6, ipaddr, sizeof(struct in6_addr)); - DPRINTF("ifconfig %s add %s/%d\n", + D(SYSTEM, "ifconfig %s add %s/%d\n", dev->ifname, ipaddr, addr->mask); return -1; } @@ -98,11 +98,11 @@ int system_del_address(struct device *dev, struct device_addr *addr) char ipaddr[64]; if ((addr->flags & DEVADDR_FAMILY) == DEVADDR_INET4) { - DPRINTF("ifconfig %s del %d.%d.%d.%d\n", + D(SYSTEM, "ifconfig %s del %d.%d.%d.%d\n", dev->ifname, a[0], a[1], a[2], a[3]); } else { inet_ntop(AF_INET6, &addr->addr.in6, ipaddr, sizeof(struct in6_addr)); - DPRINTF("ifconfig %s del %s/%d\n", + D(SYSTEM, "ifconfig %s del %s/%d\n", dev->ifname, ipaddr, addr->mask); return -1; } @@ -132,7 +132,7 @@ int system_add_route(struct device *dev, struct device_route *route) if (route->flags & DEVADDR_DEVICE) sprintf(devstr, " dev %s", dev->ifname); - DPRINTF("route add %s%s%s\n", addr, gw, devstr); + D(SYSTEM, "route add %s%s%s\n", addr, gw, devstr); return 0; } @@ -158,6 +158,6 @@ int system_del_route(struct device *dev, struct device_route *route) if (route->flags & DEVADDR_DEVICE) sprintf(devstr, " dev %s", dev->ifname); - DPRINTF("route del %s%s%s\n", addr, gw, devstr); + D(SYSTEM, "route del %s%s%s\n", addr, gw, devstr); return 0; } diff --git a/utils.h b/utils.h index 8ef968a..85d7dc6 100644 --- a/utils.h +++ b/utils.h @@ -4,16 +4,6 @@ #include #include -#ifdef DEBUG -#define DPRINTF(format, ...) fprintf(stderr, "%s(%d): " format, __func__, __LINE__, ## __VA_ARGS__) -#else -#define DPRINTF(format, ...) no_debug(format, ## __VA_ARGS__) -#endif - -static inline void no_debug(const char *fmt, ...) -{ -} - #define __init __attribute__((constructor)) struct vlist_tree; -- 2.30.2