C 语言经常在实际的调试过程中,使用最基本的调试方法printf,我们可以使用__FILENAME__、__FUNCTION__、__LINE__
,增加自己的输出宏定义:
#define DVR_PRT(format,...) printf("[File:"__FILE__", Line:%d] "format, __LINE__, ##__VA_ARGS__)
#define PRT(format,...) printf("[File:%s, Line:%d] "format, __FILE__, __LINE__, ##__VA_ARGS__)
测试代码:
#include <stdarg.h>
#include <stdio.h>
#define PRT(...) printf("Filename %s, Function %s, Line %d > ", __FILE__, __FUNCTION__, __LINE__);
printf(__VA_ARGS__);
printf("
");
int main()
{
int a =0;
PRT("a");
PRT("hello, %d ", 10);
PRT("%d, %s, %d", 10, "dafdsa", 20);
return 0;
}
测试结果:
root@ubuntu:/home/ybq/Desktop# gcc printf.c -o printf
root@ubuntu:/home/ybq/Desktop# ./printf
Filename printf.c, Function main, Line 22 > a
Filename printf.c, Function main, Line 23 > hello, 10
Filename printf.c, Function main, Line 24 > 10, dafdsa, 20