C编写的调试宏
来源:互联网 发布:mfi 指标算法 编辑:程序博客网 时间:2024/05/23 22:40
#define LOG(...) {char _bf[1024]; snprintf(_bf, sizeof(_bf),__VA_ARGS__);fprintf(stderr,"%s", _bf);}
以上的代码为另外一种风格,后期可以进行封装。
<<debug.h>>
#ifndef _DEBUG_H_
#define _DEBUG_H_
#include <stdio.h>
#include <string.h>
#include <malloc.h>
#define ERRBUFLEN 1024
#ifdef DEBUG_ON
#define ERR_PRINT(str) \
do \
{ \
char errbuf[ERRBUFLEN] = { '\0' }; \
snprintf(errbuf, ERRBUFLEN, "[file %s line: %d] %s", __FILE__, __LINE__, str); \
fprintf(stderr, "\033[31m"); \
perror(errbuf); \
fprintf(stderr, "\033[0m"); \
} while( 0 )
#define INFO_PRINT(str) \
do \
{ \
printf("\033[31m"); \
printf("[file: %s line: %d] %s\n", __FILE__, __LINE__, str); \
printf("\033[0m"); \
} while ( 0 )
#else
#define ERR_PRINT(str)
#define INFO_PRINT(str)
#endif
#endif
#ifndef _DEBUG_H_#define _DEBUG_H_#include <stdio.h>#include <string.h>#include <malloc.h>#define ERRBUFLEN1024#ifdef DEBUG_ON#define ERR_PRINT(str) \ do \ { \ char errbuf[ERRBUFLEN] = { '\0' }; \ snprintf(errbuf, ERRBUFLEN, "[file %s line: %d func:%s] %s", __FILE__, __LINE__, __FUNCTION__, str); \ fprintf(stderr, "\033[31m"); \ perror(errbuf); \ fprintf(stderr, "\033[0m"); \ } while( 0 )#define INFO_PRINT(str) \ do \ { \ printf("\033[31m"); \ printf("[file: %s line: %d func:%s] %s\n", __FILE__, __LINE__, __FUNCTION__, str); \ printf("\033[0m"); \ } while ( 0 )#else#define ERR_PRINT(str)#define INFO_PRINT(str)#endif#endif
<<debug.c>>
#include "debug.h"
int main()
{
printf("test\n");
FILE *fp = NULL;
fp = fopen("./none.txt", "r");
if (NULL == fp){
ERR_PRINT("fopen error");
}
int i = 1;
if (i < 2){
INFO_PRINT("i < 2");
}
return 0;
}
#include "debug.h"int main(){ printf("test\n"); FILE *fp = NULL; fp = fopen("./none.txt", "r"); if (NULL == fp){ ERR_PRINT("fopen error"); } int i = 1; if (i < 2){ INFO_PRINT("i < 2"); } return 0;}
运行结果:
gcc的-D和-U参数:宏的设置与取消
- C编写的调试宏
- linux 中C语言便于调试的宏定义编写及 __FILE__,__FUNCTION__, __LINE__参数使用
- linux 中C语言便于调试的宏定义编写及 __FILE__,__FUNCTION__, __LINE__参数使用
- linux 中C语言便于调试的宏定义编写及 __FILE__,__FUNCTION__, __LINE__参数使用
- Linux下C语言程序简单的编写和调试
- javascript的编写、调试
- Unity3.5的脚本编写(C#),请使用VS编写,然后用MonoDevelop调试!
- 使用Turbo C进行C程序编写和调试的技巧
- 编写方便调试的系统
- ubuntu12.10下Qtcreator编写c/c++程序无法调试的解决办法
- ubuntu下Qtcreator编写c/c++程序无法调试的解决办法
- 用C语言编写控制台程序进行调试时程序闪退的解决办法
- Linux 操作系统 C 语言编程入门之编译调试说明及Makefile的编写
- 为C++编写图形化的调试可视化工具(Writing graphical debugger visualizers for C++)
- C语言的宏编写(面试题)
- C编写的通讯录
- c语言的编写
- C/C++常用的调试宏
- LAMP的解释
- C语言atoi()和itoa()函数的实现
- CPU位数、操作系统位数和编译器位数关系
- 指针的疑问
- Java中的值传递和引用传递
- C编写的调试宏
- Xcode 配置SVN 几点注意的问题
- 【discuzX2】缓存机制分析
- js中的||(或)运算究竟返回什么?
- Android软件架构及子系统介绍
- 什么是网站开发?
- android java.lang.UnsatisfiedLinkError: 分析及解决方法
- 3个重要的配置文件
- java 加减乘除错误