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.
 
 
 
 
 
 

144 lines
5.4 KiB

/**
#include <stdio.h>
* Runtime debug configuration system
* Provides flexible control over debug output without recompilation
*/
#ifndef DEBUG_CONFIG_H
#define DEBUG_CONFIG_H
#include <stdint.h>
#include <stddef.h>
#include <time.h>
#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 - can be combined with bitwise OR */
typedef enum {
DEBUG_CATEGORY_NONE = 0,
DEBUG_CATEGORY_UASYNC = 1 << 0, // u_async module
DEBUG_CATEGORY_LL_QUEUE = 1 << 1, // ll_queue module
DEBUG_CATEGORY_CONNECTION = 1 << 2, // connection module
DEBUG_CATEGORY_ETCP = 1 << 3, // etcp module
DEBUG_CATEGORY_CRYPTO = 1 << 4, // crypto operations
DEBUG_CATEGORY_MEMORY = 1 << 5, // memory management
DEBUG_CATEGORY_TIMING = 1 << 6, // timing/performance
DEBUG_CATEGORY_CONFIG = 1 << 7, // configuration parsing
DEBUG_CATEGORY_TUN = 1 << 8, // TUN interface
DEBUG_CATEGORY_ROUTING = 1 << 9, // routing table
DEBUG_CATEGORY_ALL = 0xFFFFFFFF
} debug_category_t;
/* Debug configuration structure */
typedef struct {
debug_level_t level; // Global debug level
uint32_t categories; // Enabled categories (bitmask)
int timestamp_enabled; // Include timestamps in output
int function_name_enabled; // Include function names
int file_line_enabled; // Include file:line info
int color_enabled; // Use ANSI colors (if terminal supports)
size_t max_output_per_second; // Rate limiting (0 = unlimited)
const char* output_file; // NULL = stderr, otherwise file path
} 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);
/* Enable/disable specific categories */
void debug_enable_category(debug_category_t category);
void debug_disable_category(debug_category_t category);
void debug_set_categories(uint32_t categories);
/* 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_enable_color(int enable);
void debug_set_rate_limit(size_t max_per_second);
void debug_set_output_file(const char* file_path);
/* Parse debug configuration from string (e.g., "uasync:debug,ll_queue:info") */
int debug_parse_config(const char* config_string);
/* Parse debug configuration from file */
int debug_parse_config_file(const char* file_path);
/* Enable/disable automatic config file reloading */
int debug_enable_config_reload(const char* file_path, int interval_seconds);
#include <stdio.h>
extern FILE* debug_output_file;
/* Check if debug output should be shown for given level and category */
int debug_should_output(debug_level_t level, debug_category_t category);
/* Get current debug level for a category */
debug_level_t debug_get_effective_level(debug_category_t category);
/* Format and output debug message (internal use by macros) */
void debug_output(debug_level_t level, debug_category_t category,
const char* function, const char* file, int line,
const char* format, ...);
/* Parse debug configuration from file */
int debug_parse_config_file(const char* file_path);
/* Enable/disable automatic config file reloading */
int debug_enable_config_reload(const char* file_path, int interval_seconds);
/* Format and output debug message (internal use by macros) */
void debug_output(debug_level_t level, debug_category_t category,
const char* function, const char* file, int line,
const char* format, ...);
/* 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