|
|
|
|
@ -58,7 +58,7 @@ ip_str_t ip_to_str(const void *addr, int family) {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Get category by name */ |
|
|
|
|
/* Get category index by name */ |
|
|
|
|
static debug_category_t get_category_by_name(const char* name) { |
|
|
|
|
struct { |
|
|
|
|
const char* n; |
|
|
|
|
@ -96,13 +96,39 @@ static debug_category_t get_category_by_name(const char* name) {
|
|
|
|
|
return DEBUG_CATEGORY_NONE; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/* Get category name for logging */ |
|
|
|
|
const char* debug_get_category_name(debug_category_t category_idx) { |
|
|
|
|
switch (category_idx) { |
|
|
|
|
case DEBUG_CATEGORY_UASYNC: return "UASYNC"; |
|
|
|
|
case DEBUG_CATEGORY_LL_QUEUE: return "LL_QUEUE"; |
|
|
|
|
case DEBUG_CATEGORY_CONNECTION: return "CONNECTION"; |
|
|
|
|
case DEBUG_CATEGORY_ETCP: return "ETCP"; |
|
|
|
|
case DEBUG_CATEGORY_CRYPTO: return "CRYPTO"; |
|
|
|
|
case DEBUG_CATEGORY_MEMORY: return "MEMORY"; |
|
|
|
|
case DEBUG_CATEGORY_TIMING: return "TIMING"; |
|
|
|
|
case DEBUG_CATEGORY_CONFIG: return "CONFIG"; |
|
|
|
|
case DEBUG_CATEGORY_TUN: return "TUN"; |
|
|
|
|
case DEBUG_CATEGORY_ROUTING: return "ROUTING"; |
|
|
|
|
case DEBUG_CATEGORY_TIMERS: return "TIMERS"; |
|
|
|
|
case DEBUG_CATEGORY_NORMALIZER: return "NORMALIZER"; |
|
|
|
|
case DEBUG_CATEGORY_BGP: return "BGP"; |
|
|
|
|
case DEBUG_CATEGORY_SOCKET: return "SOCKET"; |
|
|
|
|
case DEBUG_CATEGORY_CONTROL: return "CONTROL"; |
|
|
|
|
case DEBUG_CATEGORY_DUMP: return "DUMP"; |
|
|
|
|
case DEBUG_CATEGORY_TRAFFIC: return "TRAFFIC"; |
|
|
|
|
case DEBUG_CATEGORY_DEBUG: return "DEBUG"; |
|
|
|
|
case DEBUG_CATEGORY_GENERAL: return "GENERAL"; |
|
|
|
|
case DEBUG_CATEGORY_NONE: return "NONE"; |
|
|
|
|
default: return "UNKNOWN"; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/* Global debug configuration */ |
|
|
|
|
debug_config_t g_debug_config; |
|
|
|
|
|
|
|
|
|
/* Initialize debug system with default settings */ |
|
|
|
|
void debug_config_init(void) { |
|
|
|
|
g_debug_config.level = DEBUG_LEVEL_ERROR; // Global level: errors only by default
|
|
|
|
|
g_debug_config.categories = DEBUG_CATEGORY_ALL; |
|
|
|
|
for (int i = 0; i < DEBUG_CATEGORY_COUNT; i++) { |
|
|
|
|
g_debug_config.category_levels[i] = DEBUG_LEVEL_NONE; // 0 = disabled per category
|
|
|
|
|
} |
|
|
|
|
@ -125,34 +151,34 @@ void debug_set_level(debug_level_t level) {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/* Set debug level for specific category (0 = disabled, otherwise uses that level) */ |
|
|
|
|
void debug_set_category_level(debug_category_t category, debug_level_t level) { |
|
|
|
|
if (category == DEBUG_CATEGORY_NONE) return; |
|
|
|
|
int idx = 0; |
|
|
|
|
while (!(category & (1ULL << idx)) && idx < DEBUG_CATEGORY_COUNT) { |
|
|
|
|
idx++; |
|
|
|
|
} |
|
|
|
|
if (idx < DEBUG_CATEGORY_COUNT) { |
|
|
|
|
g_debug_config.category_levels[idx] = level; |
|
|
|
|
} |
|
|
|
|
void debug_set_category_level(debug_category_t category_idx, debug_level_t level) { |
|
|
|
|
if (category_idx == DEBUG_CATEGORY_NONE || category_idx >= DEBUG_CATEGORY_COUNT) return; |
|
|
|
|
g_debug_config.category_levels[category_idx] = level; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/* Enable/disable specific categories */ |
|
|
|
|
void debug_enable_category(debug_category_t category) { |
|
|
|
|
g_debug_config.categories |= category; |
|
|
|
|
/* Enable/disable specific categories (now sets level) */ |
|
|
|
|
void debug_enable_category(debug_category_t category_idx) { |
|
|
|
|
if (category_idx >= 0 && category_idx < DEBUG_CATEGORY_COUNT) { |
|
|
|
|
g_debug_config.category_levels[category_idx] = g_debug_config.level; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void debug_disable_category(debug_category_t category) { |
|
|
|
|
g_debug_config.categories &= ~category; |
|
|
|
|
void debug_disable_category(debug_category_t category_idx) { |
|
|
|
|
if (category_idx >= 0 && category_idx < DEBUG_CATEGORY_COUNT) { |
|
|
|
|
g_debug_config.category_levels[category_idx] = DEBUG_LEVEL_NONE; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void debug_set_categories(debug_category_t categories) { |
|
|
|
|
g_debug_config.categories = categories; |
|
|
|
|
void debug_set_categories(debug_category_t category_idx) { |
|
|
|
|
/* Deprecated - use per-category levels */ |
|
|
|
|
if (category_idx >= 0 && category_idx < DEBUG_CATEGORY_COUNT) { |
|
|
|
|
g_debug_config.category_levels[category_idx] = g_debug_config.level; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/* Set masks directly */ |
|
|
|
|
void debug_set_masks(debug_category_t categories, debug_level_t level) { |
|
|
|
|
g_debug_config.categories = categories; |
|
|
|
|
g_debug_config.level = level; |
|
|
|
|
/* Set level for category (main API) */ |
|
|
|
|
void debug_set_masks(debug_category_t category_idx, debug_level_t level) { |
|
|
|
|
debug_set_category_level(category_idx, level); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void debug_enable_function_name(int enable) { |
|
|
|
|
@ -217,23 +243,18 @@ void debug_disable_file_output(void) {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/* Check if debug output should be shown for given level and category */ |
|
|
|
|
int debug_should_output(debug_level_t level, debug_category_t category) { |
|
|
|
|
|
|
|
|
|
/* Check if category is enabled */ |
|
|
|
|
if (!(g_debug_config.categories & category)) { |
|
|
|
|
int debug_should_output(debug_level_t level, debug_category_t category_idx) { |
|
|
|
|
if (category_idx < 0 || category_idx >= DEBUG_CATEGORY_COUNT) { |
|
|
|
|
return 0; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/* Determine effective level: max(global, category) */ |
|
|
|
|
int idx = 0; |
|
|
|
|
while (!(category & (1ULL << idx)) && idx < DEBUG_CATEGORY_COUNT) { |
|
|
|
|
idx++; |
|
|
|
|
debug_level_t cat_level = g_debug_config.category_levels[category_idx]; |
|
|
|
|
if (cat_level == DEBUG_LEVEL_NONE) { |
|
|
|
|
cat_level = g_debug_config.level; |
|
|
|
|
} |
|
|
|
|
debug_level_t cat_level = (idx < DEBUG_CATEGORY_COUNT) ? g_debug_config.category_levels[idx] : DEBUG_LEVEL_NONE; |
|
|
|
|
debug_level_t effective = (cat_level > g_debug_config.level) ? cat_level : g_debug_config.level; |
|
|
|
|
|
|
|
|
|
/* Check if level is sufficient */ |
|
|
|
|
if (level > effective) { |
|
|
|
|
if (level > cat_level) { |
|
|
|
|
return 0; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@ -258,7 +279,7 @@ static const char* get_level_name(debug_level_t level) {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/* Format and output debug message */ |
|
|
|
|
void debug_output(debug_level_t level, debug_category_t category,
|
|
|
|
|
void debug_output(debug_level_t level, debug_category_t category_idx,
|
|
|
|
|
const char* function, const char* file, int line, |
|
|
|
|
const char* format, ...) { |
|
|
|
|
|
|
|
|
|
@ -285,8 +306,9 @@ void debug_output(debug_level_t level, debug_category_t category,
|
|
|
|
|
offset += snprintf(buffer + offset, remaining, "[%s] ", level_name); |
|
|
|
|
remaining = BUFFER_SIZE - offset; |
|
|
|
|
|
|
|
|
|
/* Add category */ |
|
|
|
|
offset += snprintf(buffer + offset, remaining, "[%llu] ", (unsigned long long)category); |
|
|
|
|
/* Add category name */ |
|
|
|
|
const char* cat_name = debug_get_category_name(category_idx); |
|
|
|
|
offset += snprintf(buffer + offset, remaining, "[%s] ", cat_name); |
|
|
|
|
remaining = BUFFER_SIZE - offset; |
|
|
|
|
|
|
|
|
|
/* Add file:line if enabled */ |
|
|
|
|
|