You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
185 lines
7.2 KiB
185 lines
7.2 KiB
/** |
|
* Runtime debug configuration system |
|
* Provides flexible control over debug output without recompilation |
|
*/ |
|
|
|
#ifndef DEBUG_CONFIG_H |
|
#define DEBUG_CONFIG_H |
|
|
|
#ifdef _WIN32 |
|
#include <winsock2.h> |
|
#include <ws2tcpip.h> |
|
#else |
|
#include <sys/socket.h> // struct sockaddr_storage |
|
#endif |
|
#include <stddef.h> // size_t |
|
#include <stdint.h> |
|
#include <stddef.h> |
|
#include <time.h> |
|
#include <stdio.h> // FILE |
|
|
|
#ifdef __cplusplus |
|
extern "C" { |
|
#endif |
|
|
|
/* Debug levels */ |
|
typedef enum { |
|
DEBUG_LEVEL_NONE = 0, // No debug output |
|
DEBUG_LEVEL_ERROR = 1, // Errors only |
|
DEBUG_LEVEL_WARN = 2, // Warnings and errors |
|
DEBUG_LEVEL_INFO = 3, // Info, warnings, errors |
|
DEBUG_LEVEL_DEBUG = 4, // Full debug output |
|
DEBUG_LEVEL_TRACE = 5 // Trace everything |
|
} debug_level_t; |
|
|
|
/* Debug categories as indices (0-based) */ |
|
typedef int debug_category_t; |
|
|
|
#define DEBUG_CATEGORY_NONE 0 |
|
#define DEBUG_CATEGORY_UASYNC 1 // u_async module |
|
#define DEBUG_CATEGORY_LL_QUEUE 2 // ll_queue module |
|
#define DEBUG_CATEGORY_CONNECTION 3 // connection module |
|
#define DEBUG_CATEGORY_ETCP 4 // etcp module |
|
#define DEBUG_CATEGORY_CRYPTO 5 // crypto operations |
|
#define DEBUG_CATEGORY_MEMORY 6 // memory management |
|
#define DEBUG_CATEGORY_TIMING 7 // timing/performance |
|
#define DEBUG_CATEGORY_CONFIG 8 // configuration parsing |
|
#define DEBUG_CATEGORY_TUN 9 // TUN interface |
|
#define DEBUG_CATEGORY_ROUTING 10 // routing table |
|
#define DEBUG_CATEGORY_TIMERS 11 // timer management |
|
#define DEBUG_CATEGORY_NORMALIZER 12 // packet normalizer |
|
#define DEBUG_CATEGORY_BGP 13 // BGP route exchange |
|
#define DEBUG_CATEGORY_SOCKET 14 // Socket operations |
|
#define DEBUG_CATEGORY_CONTROL 15 // Control/monitoring server |
|
#define DEBUG_CATEGORY_DUMP 16 // Packet dump/logging |
|
#define DEBUG_CATEGORY_TRAFFIC 17 // Traffic flow (src/dst node IDs) |
|
#define DEBUG_CATEGORY_DEBUG 18 // Current debugging |
|
#define DEBUG_CATEGORY_GENERAL 19 // Messages for user (common log) |
|
#define DEBUG_CATEGORY_COUNT 20 // Total number of categories |
|
#define DEBUG_CATEGORY_ALL (-1) // special value for all categories |
|
|
|
/* Debug configuration structure */ |
|
typedef struct { |
|
debug_level_t level; // Global debug level (default: ERROR) |
|
debug_level_t category_levels[DEBUG_CATEGORY_COUNT]; // Per-category levels (0 = disabled) |
|
int timestamp_enabled; // Include timestamps in output |
|
int function_name_enabled; // Include function names |
|
int file_line_enabled; // Include file:line info |
|
const char* output_file; // NULL = stdout, otherwise file path |
|
|
|
// Dual output support |
|
FILE* file_output; // File handle for log file (NULL if not open) |
|
debug_level_t file_level; // Log level for file output |
|
debug_level_t console_level; // Log level for console output |
|
int console_enabled; // Enable console output |
|
} debug_config_t; |
|
|
|
/* Global debug configuration */ |
|
extern debug_config_t g_debug_config; |
|
|
|
/* Initialize debug system with default settings */ |
|
void debug_config_init(void); |
|
|
|
/* Set debug level */ |
|
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); |
|
|
|
/* Enable/disable specific categories */ |
|
void debug_enable_category(debug_category_t category); |
|
void debug_disable_category(debug_category_t category); |
|
void debug_set_categories(debug_category_t categories); |
|
|
|
/* Set masks directly */ |
|
void debug_set_masks(debug_category_t categories, debug_level_t level); |
|
|
|
/* Configure output options */ |
|
void debug_enable_timestamp(int enable); |
|
void debug_enable_function_name(int enable); |
|
void debug_enable_file_line(int enable); |
|
void debug_set_output_file(const char* file_path); |
|
|
|
/* Dual output configuration */ |
|
void debug_set_console_level(debug_level_t level); |
|
void debug_set_file_level(debug_level_t level); |
|
void debug_enable_file_output(const char* file_path, int truncate); |
|
void debug_disable_file_output(void); |
|
void debug_enable_console(int enable); |
|
|
|
// IP address to string (static buffer, single-threaded) |
|
typedef struct { |
|
char str[48]; // INET6_ADDRSTRLEN(45) + \0 |
|
} ip_str_t; |
|
|
|
ip_str_t ip_to_str(const void *addr, int family); |
|
|
|
// hex dump в лог |
|
void log_dump(const char* prefix, const uint8_t* data, size_t len); |
|
|
|
/* Check if debug output should be shown for given level and category */ |
|
int debug_should_output(debug_level_t level, debug_category_t category_idx); |
|
|
|
/* Get current debug level */ |
|
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); |
|
|
|
/* 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, |
|
const char* format, ...); |
|
|
|
/* Parse debug configuration from string (e.g. "etcp=trace,config=info") */ |
|
int debug_parse_config(const char* config_string); |
|
|
|
/* Convenience macros for debug output */ |
|
#define DEBUG_ERROR(category, fmt, ...) \ |
|
do { if (debug_should_output(DEBUG_LEVEL_ERROR, category)) { \ |
|
debug_output(DEBUG_LEVEL_ERROR, category, __FUNCTION__, __FILE__, __LINE__, fmt, ##__VA_ARGS__); \ |
|
} } while(0) |
|
|
|
#define DEBUG_WARN(category, fmt, ...) \ |
|
do { if (debug_should_output(DEBUG_LEVEL_WARN, category)) { \ |
|
debug_output(DEBUG_LEVEL_WARN, category, __FUNCTION__, __FILE__, __LINE__, fmt, ##__VA_ARGS__); \ |
|
} } while(0) |
|
|
|
#define DEBUG_INFO(category, fmt, ...) \ |
|
do { if (debug_should_output(DEBUG_LEVEL_INFO, category)) { \ |
|
debug_output(DEBUG_LEVEL_INFO, category, __FUNCTION__, __FILE__, __LINE__, fmt, ##__VA_ARGS__); \ |
|
} } while(0) |
|
|
|
#define DEBUG_DEBUG(category, fmt, ...) \ |
|
do { if (debug_should_output(DEBUG_LEVEL_DEBUG, category)) { \ |
|
debug_output(DEBUG_LEVEL_DEBUG, category, __FUNCTION__, __FILE__, __LINE__, fmt, ##__VA_ARGS__); \ |
|
} } while(0) |
|
|
|
#define DEBUG_TRACE(category, fmt, ...) \ |
|
do { if (debug_should_output(DEBUG_LEVEL_TRACE, category)) { \ |
|
debug_output(DEBUG_LEVEL_TRACE, category, __FUNCTION__, __FILE__, __LINE__, fmt, ##__VA_ARGS__); \ |
|
} } while(0) |
|
|
|
/* Backward compatibility - default to ERROR level if not specified */ |
|
#define DEBUG_OUTPUT(fmt, ...) DEBUG_ERROR(DEBUG_CATEGORY_ALL, fmt, ##__VA_ARGS__) |
|
|
|
#ifdef __cplusplus |
|
} |
|
#endif |
|
|
|
#endif // DEBUG_CONFIG_H
|
|
|