From 8271ec3ecbdf30f52371dbfc7d895b07080417a8 Mon Sep 17 00:00:00 2001 From: Evgeny Date: Thu, 26 Mar 2026 00:01:47 +0300 Subject: [PATCH] 1 --- lib/debug_config.c | 20 ++++++++++++++++++++ lib/debug_config.h | 6 ++++++ src/utun_instance.c | 28 +++++----------------------- 3 files changed, 31 insertions(+), 23 deletions(-) diff --git a/lib/debug_config.c b/lib/debug_config.c index 690e3b2d0..ffb648565 100644 --- a/lib/debug_config.c +++ b/lib/debug_config.c @@ -131,6 +131,26 @@ void debug_set_category_level_by_name(const char* category_name, const char* lev debug_set_category_level(cat, lvl); } +int debug_apply_category_config(const char* category_name, const char* level_name) { + if (!category_name || !level_name) return -1; + 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 -1; + } + debug_level_t lvl = debug_level_from_name(level_name); + debug_set_category_level(cat, lvl); + DEBUG_INFO(DEBUG_CATEGORY_CONFIG, "Applied per-category debug: %s=%s", category_name, level_name); + return 0; +} + +void debug_apply_global_level(const char* level_name) { + if (!level_name || !*level_name) return; + debug_level_t level = debug_level_from_name(level_name); + debug_set_level(level); + DEBUG_INFO(DEBUG_CATEGORY_CONFIG, "Applied global debug_level: %s", level_name); +} + /* Get category name for logging */ const char* debug_get_category_name(debug_category_t category_idx) { switch (category_idx) { diff --git a/lib/debug_config.h b/lib/debug_config.h index a4ebe3942..ee460b7ef 100644 --- a/lib/debug_config.h +++ b/lib/debug_config.h @@ -135,6 +135,12 @@ 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); +/* Apply full category config with error reporting */ +int debug_apply_category_config(const char* category_name, const char* level_name); + +/* Apply global debug level from text name */ +void debug_apply_global_level(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 f6395d3b7..e92088818 100644 --- a/src/utun_instance.c +++ b/src/utun_instance.c @@ -142,33 +142,15 @@ struct UTUN_INSTANCE* utun_instance_create(struct UASYNC* ua, const char *config // Apply debug level from config if (config->global.debug_level[0]) { - debug_level_t level = DEBUG_LEVEL_INFO; - if (strcmp(config->global.debug_level, "error") == 0) level = DEBUG_LEVEL_ERROR; - else if (strcmp(config->global.debug_level, "warn") == 0) level = DEBUG_LEVEL_WARN; - else if (strcmp(config->global.debug_level, "info") == 0) level = DEBUG_LEVEL_INFO; - else if (strcmp(config->global.debug_level, "debug") == 0) level = DEBUG_LEVEL_DEBUG; - else if (strcmp(config->global.debug_level, "trace") == 0) level = DEBUG_LEVEL_TRACE; - debug_set_level(level); - DEBUG_INFO(DEBUG_CATEGORY_CONFIG, "Applied debug_level from config: %s", config->global.debug_level); + debug_apply_global_level(config->global.debug_level); } // Apply per-category debug levels from [debug] section for (int i = 0; i < config->global.debug_levels.count; i++) { - const char* cat_name = config->global.debug_levels.category[i]; - const char* lvl_str = config->global.debug_levels.level[i]; - - 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; - else if (strcmp(lvl_str, "info") == 0) lvl = DEBUG_LEVEL_INFO; - else if (strcmp(lvl_str, "debug") == 0) lvl = DEBUG_LEVEL_DEBUG; - else if (strcmp(lvl_str, "trace") == 0) lvl = DEBUG_LEVEL_TRACE; - - debug_set_category_level(cat, lvl); - DEBUG_INFO(DEBUG_CATEGORY_CONFIG, "Applied per-category debug: %s=%s", cat_name, lvl_str); - } + debug_apply_category_config( + config->global.debug_levels.category[i], + config->global.debug_levels.level[i] + ); } // Allocate instance