使用printf函数格式化调试输出
使用C语言调试程序时,经常用printf函数,发布程序时,需要将printf函数注释,可以使用一下宏.
#define ENABLE_ALL_LEVEL_LOG (1) #define ENABLE_DEBUG_LEVEL (1) #define ENABLE_ERROR_LEVEL (1) #define ENABLE_INFO_LEVEL (1) #if ENABLE_ALL_LEVEL_LOG #define p_debug(format, ...)\ do {fprintf(stderr, "\033[;37m DEBUG [%s][%s][%d]:" format "\033[0m\n",\ __FILE__,__func__,__LINE__, ##__VA_ARGS__ );\ } while (0) #define p_error(format, ...)\ do {fprintf(stderr, "\033[;31m ERROR [%s][%s][%d]:" format "\033[0m\n",\ __FILE__,__func__,__LINE__, ##__VA_ARGS__ );\ } while (0) #define p_info(format, ...)\ do {fprintf(stderr, "\033[;32m INFO [%s][%s][%d]:" format "\033[0m\n",\ __FILE__,__func__,__LINE__, ##__VA_ARGS__ );\ } while (0) #else #if ENABLE_DEBUG_LEVEL #define p_debug(format, ...)\ do {fprintf(stderr, "\033[;37m DEBUG [%s][%s][%d]:" format "\033[0m\n",\ __FILE__,__func__,__LINE__, ##__VA_ARGS__ );\ } while (0) #else #define p_debug(format, ...) #endif #if ENABLE_ERROR_LEVEL #define p_error(format, ...)\ do {fprintf(stderr, "\033[;31m ERROR [%s][%s][%d]:" format "\033[0m\n",\ __FILE__,__func__,__LINE__, ##__VA_ARGS__ );\ } while (0) #else #define p_error(format, ...) #endif #if ENABLE_INFO_LEVEL #define p_info(format, ...)\ do {fprintf(stderr, "\033[;32m INFO [%s][%s][%d]:" format "\033[0m\n",\ __FILE__,__func__,__LINE__, ##__VA_ARGS__ );\ } while (0) #else #define p_info(format, ...) #endif #endif
具体颜色可以参考这里:
C语言中默认的printf打印都是一种颜色的,有时候为了突出显示其中的信息,避免重要的警告(warning)和错误(error)信息淹没在其他不重要的log中,有必要区别不同信息级别的字体颜色。
格式 printf("\033[字背景颜色;字体颜色m字符串\033[0m" ); 例如, printf("\033[47;31mThis is a color test.\033[0m"); 47是字背景颜色, 31是字体的颜色, This is a color test.是字符串. \033[0m是控制码. 颜色打印 附录 部分颜色代码: 字背景颜色: 40--49 字颜色: 30--39 40: 黑 30: 黑 41: 红 31: 红 42: 绿 32: 绿 43: 黄 33: 黄 44: 蓝 34: 蓝 45: 紫 35: 紫 46: 深绿 36: 深绿 47:白色 37:白色
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 如何调用 DeepSeek 的自然语言处理 API 接口并集成到在线客服系统
· 【译】Visual Studio 中新的强大生产力特性
· 2025年我用 Compose 写了一个 Todo App