/* Copyright(c) 2025 Rangersly All rights reserved. DEBUG TOOLS Version: v0.1-Alpha 添加了字符串检查输出调试报错 v0.1-Beta v0.2-Alpha 添加了内存函数重封装 v0.3-Alpha 添加了日志系统 */ #ifndef DEBUGTOOLS #define DEBUGTOOLS #include #include #include /* 一些基础调试函数 */ #ifdef DEBUG /* 字符串检查宏,输出字符串的每一位数据 */ #define print_str(str, len) do{ \ size_t _len = (size_t)len; \ printf("%s\n", str); \ for(int i = 0; i < _len; i++) \ printf("%2x -- %c\n", i, (char)str[i], (uint8_t)str[i]); \ }while(0) /* 输出调试错误报错信息,辅助定位错误地点 */ #define PERROR(str) printf("%s:%d:%s:\n%s\n", __FILE__, __LINE__, __func__, str); exit(0) #else #define print_str(str, len) #define PERROR(str) #endif /*----------------------------------------------*/ /* 内存函数重封装 */ #ifdef DEBUG /* 显示当前的内存情况 */ #define display_memory_status() __display_memory_status() /* 重封装malloc函数,第二个参数是内存块说明 */ #define _malloc(size, message) __malloc(size, message) /* 重封装free */ #define _free(point) __free(point) #else /* 非调试版本 */ #define display_memory_status() #define _malloc(size, message) malloc(size) #define _free(point) free(point) #endif struct Memory_info { size_t size; /* 内存块大小 */ struct Memory_info *next; /* 下一个内存块 */ void *address; /* 内存块地址 */ char message[32]; /* 内存块信息 */ }; struct Memory_status { size_t all_size; /* 使用的内存总大小 */ int count; /* 当前内存块个数 */ struct Memory_info *front; /* 存储信息头部 */ }; void* __malloc(size_t size, const char *a); void __free(void *ptr); void __display_memory_status(void); /*----------------------------------------------*/ /* 日志系统 */ enum LOG_LEVEL{ LOG_INFO = 0, LOG_WARNING = 1, LOG_ERROR = 2, LOG_FATAL = 3 }; /* 最大日志头长度 */ #define LOG_HEAD_MAX_FORMAT 32 /* 设置日志输出 */ void log_setoutfile(const char *restrict filename); /* 设置日志的最低输出等级 */ void log_setlevel(enum LOG_LEVEL level); /* 设置日志头,最大不可超过15个字符 */ void log_sethead(const char *restrict head); /* * 符号表 * %Y year * %M month * %d day * %h hour * %m minute * %s second * %n number * %c core time .3f */ /* 输出日志,包含日志等级与日志信息 */ void log_print(enum LOG_LEVEL level, const char *restrict message); /*----------------------------------------------*/ #endif