C语言学习之 DEBUG宏

DEBUG宏:

专门用于调试程序的宏函数,这种宏函数在程序测试、调试、试运行阶段执行,在程序正式上线阶段不执行。

    一些操作提示,如:xxx操作成功,xxx操作失败,分配内存的记录、释放内存的记录,这类型消息开发人员、测试人员需要看到,但用户不需要看到。

举例如下:

#include <stdio.h>
#include <stdlib.h>

void* _my_malloc(size_t size,const char* file,const char* func,size_t line)
{
     void* ptr = malloc(size);
     printf("%s %s %u malloc %p  %u byte\n",file,func,line,ptr,size);
     return ptr;
 }
 
 #ifdef DEBUG
 # define debug(...) do{\
 printf("file:%s func:%s line:%d:",__FILE__,__func__,__LINE__); \
 printf("\33[01;32m");\
 printf(__VA_ARGS__);\
 printf("\33[00m");\
 }while(0)
 #else
 # define debug(...) do{\
 printf(__VA_ARGS__);\
 }while(0)
 #endif//DEBUG
 
 int main(int argc,const char* argv[])
 {
     debug("%s文件打开成功!\n","hehe.text");
     return 0;
 }

猜你喜欢

转载自blog.csdn.net/m0_62480610/article/details/125928229
今日推荐