diff --git a/lib/debug_config.c b/lib/debug_config.c index e888161..690e3b2 100644 --- a/lib/debug_config.c +++ b/lib/debug_config.c @@ -58,44 +58,79 @@ ip_str_t ip_to_str(const void *addr, int family) { } -/* Get category index by name */ -static debug_category_t get_category_by_name(const char* name) { - struct { - const char* n; - debug_category_t c; - } map[] = { - {"none", DEBUG_CATEGORY_NONE}, - {"uasync", DEBUG_CATEGORY_UASYNC}, - {"ll_queue", DEBUG_CATEGORY_LL_QUEUE}, - {"connection", DEBUG_CATEGORY_CONNECTION}, - {"etcp", DEBUG_CATEGORY_ETCP}, - {"crypto", DEBUG_CATEGORY_CRYPTO}, - {"memory", DEBUG_CATEGORY_MEMORY}, - {"timing", DEBUG_CATEGORY_TIMING}, - {"config", DEBUG_CATEGORY_CONFIG}, - {"tun", DEBUG_CATEGORY_TUN}, - {"routing", DEBUG_CATEGORY_ROUTING}, - {"timers", DEBUG_CATEGORY_TIMERS}, - {"normalizer", DEBUG_CATEGORY_NORMALIZER}, - {"bgp", DEBUG_CATEGORY_BGP}, - {"socket", DEBUG_CATEGORY_SOCKET}, - {"control", DEBUG_CATEGORY_CONTROL}, - {"dump", DEBUG_CATEGORY_DUMP}, - {"traffic", DEBUG_CATEGORY_TRAFFIC}, - {"debug", DEBUG_CATEGORY_DEBUG}, - {"general", DEBUG_CATEGORY_GENERAL}, - {"all", DEBUG_CATEGORY_ALL}, - {NULL, 0} - }; - - for (int i = 0; map[i].n; i++) { - if (strcasecmp(map[i].n, name) == 0) { - return map[i].c; +static const struct { + const char* name; + debug_category_t category; +} g_debug_categories[] = { + {"none", DEBUG_CATEGORY_NONE}, + {"uasync", DEBUG_CATEGORY_UASYNC}, + {"ll_queue", DEBUG_CATEGORY_LL_QUEUE}, + {"connection", DEBUG_CATEGORY_CONNECTION}, + {"etcp", DEBUG_CATEGORY_ETCP}, + {"crypto", DEBUG_CATEGORY_CRYPTO}, + {"memory", DEBUG_CATEGORY_MEMORY}, + {"timing", DEBUG_CATEGORY_TIMING}, + {"config", DEBUG_CATEGORY_CONFIG}, + {"tun", DEBUG_CATEGORY_TUN}, + {"routing", DEBUG_CATEGORY_ROUTING}, + {"timers", DEBUG_CATEGORY_TIMERS}, + {"normalizer", DEBUG_CATEGORY_NORMALIZER}, + {"bgp", DEBUG_CATEGORY_BGP}, + {"socket", DEBUG_CATEGORY_SOCKET}, + {"control", DEBUG_CATEGORY_CONTROL}, + {"dump", DEBUG_CATEGORY_DUMP}, + {"traffic", DEBUG_CATEGORY_TRAFFIC}, + {"debug", DEBUG_CATEGORY_DEBUG}, + {"general", DEBUG_CATEGORY_GENERAL}, + {"all", DEBUG_CATEGORY_ALL}, + {NULL, DEBUG_CATEGORY_NONE} +}; + +static const struct { + const char* name; + debug_level_t level; +} g_level_table[] = { + {"none", DEBUG_LEVEL_NONE}, + {"error", DEBUG_LEVEL_ERROR}, + {"warn", DEBUG_LEVEL_WARN}, + {"info", DEBUG_LEVEL_INFO}, + {"debug", DEBUG_LEVEL_DEBUG}, + {"trace", DEBUG_LEVEL_TRACE}, + {NULL, DEBUG_LEVEL_NONE} +}; + +debug_category_t get_category_by_name(const char* name) { + if (!name) return DEBUG_CATEGORY_NONE; + for (int i = 0; g_debug_categories[i].name; i++) { + if (strcasecmp(g_debug_categories[i].name, name) == 0) { + return g_debug_categories[i].category; } } return DEBUG_CATEGORY_NONE; } +debug_level_t debug_level_from_name(const char* name) { + if (!name) return DEBUG_LEVEL_NONE; + for (int i = 0; g_level_table[i].name; i++) { + if (strcasecmp(g_level_table[i].name, name) == 0) { + return g_level_table[i].level; + } + } + DEBUG_ERROR(DEBUG_CATEGORY_CONFIG, "Invalid debug level '%s' (valid: none,error,warn,info,debug,trace)", name); + return DEBUG_LEVEL_INFO; /* fallback */ +} + +void debug_set_category_level_by_name(const char* category_name, const char* level_name) { + if (!category_name || !level_name) return; + debug_category_t cat = get_category_by_name(category_name); + if (cat == DEBUG_CATEGORY_NONE) { + DEBUG_ERROR(DEBUG_CATEGORY_CONFIG, "Unknown debug category '%s'", category_name); + return; + } + debug_level_t lvl = debug_level_from_name(level_name); + debug_set_category_level(cat, lvl); +} + /* Get category name for logging */ const char* debug_get_category_name(debug_category_t category_idx) { switch (category_idx) { @@ -390,27 +425,12 @@ int debug_parse_config(const char* config_string) { debug_category_t cat = get_category_by_name(category_name); if (cat == DEBUG_CATEGORY_NONE) { + DEBUG_ERROR(DEBUG_CATEGORY_CONFIG, "Unknown debug category '%s'", category_name); u_free(str); return -1; } - debug_level_t lev = DEBUG_LEVEL_NONE; - if (strcasecmp(level_name, "none") == 0) { - lev = DEBUG_LEVEL_NONE; - } else if (strcasecmp(level_name, "error") == 0) { - lev = DEBUG_LEVEL_ERROR; - } else if (strcasecmp(level_name, "warn") == 0) { - lev = DEBUG_LEVEL_WARN; - } else if (strcasecmp(level_name, "info") == 0) { - lev = DEBUG_LEVEL_INFO; - } else if (strcasecmp(level_name, "debug") == 0) { - lev = DEBUG_LEVEL_DEBUG; - } else if (strcasecmp(level_name, "trace") == 0) { - lev = DEBUG_LEVEL_TRACE; - } else { - u_free(str); - return -1; - } + debug_level_t lev = debug_level_from_name(level_name); /* Set per-category level */ int idx = 0; diff --git a/lib/debug_config.h b/lib/debug_config.h index 9df5942..a4ebe39 100644 --- a/lib/debug_config.h +++ b/lib/debug_config.h @@ -126,6 +126,15 @@ debug_level_t debug_get_level(void); /* Get category name by index */ const char* debug_get_category_name(debug_category_t category_idx); +/* Get category by name string */ +debug_category_t get_category_by_name(const char* name); + +/* Get level by name string with error reporting */ +debug_level_t debug_level_from_name(const char* name); + +/* Set category level using text names (main API) */ +void debug_set_category_level_by_name(const char* category_name, const char* level_name); + /* Format and output debug message (internal use by macros) */ void debug_output(debug_level_t level, debug_category_t category_idx, const char* function, const char* file, int line, diff --git a/src/utun_instance.c b/src/utun_instance.c index 1d30f5c..f6395d3 100644 --- a/src/utun_instance.c +++ b/src/utun_instance.c @@ -157,27 +157,8 @@ struct UTUN_INSTANCE* utun_instance_create(struct UASYNC* ua, const char *config const char* cat_name = config->global.debug_levels.category[i]; const char* lvl_str = config->global.debug_levels.level[i]; - debug_category_t cat = 0; - // Map category name to bitmask - if (strcmp(cat_name, "uasync") == 0) cat = DEBUG_CATEGORY_UASYNC; - else if (strcmp(cat_name, "ll_queue") == 0) cat = DEBUG_CATEGORY_LL_QUEUE; - else if (strcmp(cat_name, "connection") == 0) cat = DEBUG_CATEGORY_CONNECTION; - else if (strcmp(cat_name, "etcp") == 0) cat = DEBUG_CATEGORY_ETCP; - else if (strcmp(cat_name, "crypto") == 0) cat = DEBUG_CATEGORY_CRYPTO; - else if (strcmp(cat_name, "memory") == 0) cat = DEBUG_CATEGORY_MEMORY; - else if (strcmp(cat_name, "timing") == 0) cat = DEBUG_CATEGORY_TIMING; - else if (strcmp(cat_name, "config") == 0) cat = DEBUG_CATEGORY_CONFIG; - else if (strcmp(cat_name, "tun") == 0) cat = DEBUG_CATEGORY_TUN; - else if (strcmp(cat_name, "routing") == 0) cat = DEBUG_CATEGORY_ROUTING; - else if (strcmp(cat_name, "timers") == 0) cat = DEBUG_CATEGORY_TIMERS; - else if (strcmp(cat_name, "normalizer") == 0) cat = DEBUG_CATEGORY_NORMALIZER; - else if (strcmp(cat_name, "bgp") == 0) cat = DEBUG_CATEGORY_BGP; - else if (strcmp(cat_name, "socket") == 0) cat = DEBUG_CATEGORY_SOCKET; - else if (strcmp(cat_name, "control") == 0) cat = DEBUG_CATEGORY_CONTROL; - else if (strcmp(cat_name, "dump") == 0) cat = DEBUG_CATEGORY_DUMP; - else if (strcmp(cat_name, "traffic") == 0) cat = DEBUG_CATEGORY_TRAFFIC; - - if (cat) { + debug_category_t cat = get_category_by_name(cat_name); + if (cat != DEBUG_CATEGORY_NONE) { debug_level_t lvl = DEBUG_LEVEL_NONE; if (strcmp(lvl_str, "error") == 0) lvl = DEBUG_LEVEL_ERROR; else if (strcmp(lvl_str, "warn") == 0) lvl = DEBUG_LEVEL_WARN;