linux C下 利用宏函数打印调试信息

来源:互联网 发布:网店美工学生实训报告 编辑:程序博客网 时间:2024/06/05 11:03

/*****************************************************

这个代码里面

把字符串根据指定的分割符可以拆分,合并

主要是运用了一个宏来打印调试信息

函数调试好以后可以注释掉此行使,程序打印调试信息的代码不编译进去

__LINE__:在源代码中插入当前源代码行号;

__FILE__:在源文件中插入当前源文件名;

__DATE__:在源文件中插入当前的编译日期

__TIME__:在源文件中插入当前编译时间;

__STDC__:当要求程序严格遵循ANSI C标准时该标识被赋值为1;

__cplusplus:当编写C++程序时该标识符被定义。


******************************************************/

#include <netinet/in.h>

#include <pthread.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/select.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <unistd.h>




  
#define __DEBUG__  
//#undef  __DEBUG__  //函数调试好以后可以注释掉此行使,程序打印调试信息的代码不编译进去
#ifdef __DEBUG__  
#define DEBUG(format,...) \
        printf("File: "__FILE__", Line: %05d, FUNCTION: %s ::"format"\n", __LINE__,__FUNCTION__, ##__VA_ARGS__)  
#else  
#define DEBUG(format,...)  

#endif  



void getchildstr(unsigned char child[][30],char *fatherstr,char key){
char *p = NULL;
char *q = NULL;
int i = 0;
p = q = fatherstr;
while(p = strchr(p,key))
{
strncpy(child[i],q,p-q);
child[i][p-q] = '\0';
p = p+1;
q = p;
i ++;
}
}




void cat_childstr(unsigned char child[][30],int child_num,char *fatherstr,char key){
int i,len ;
for(i=0;i<child_num;i++)
{
strcat(fatherstr,child[i]);
len = strlen(fatherstr);
fatherstr[len]=key;
fatherstr[len+1]= '\0';
}
fatherstr[len]='\0';
}




int main( void )
{
    unsigned char color_child[4][30];
    unsigned char color[5]={ 145,233,222,238,0 };
char fatherstr[17]={0};
int  chil_num = 4;
int i;
for(i=0;i<4;i++)
{
char str[4];
sprintf(str, "%u", color[i]);
strcpy(color_child[i],str);
   DEBUG("main %u**%s**%s\n",color[i],str,color_child[i]);
}
cat_childstr(color_child,4,fatherstr,',');
DEBUG("%s",fatherstr);
DEBUG("%s\n\n",fatherstr);

}


//结果:



我这个项目唯一的串口用来控制单片机了,所以如果要用串口来调试,必须在编译的时候屏蔽掉所有串口读写函数,可以用以下几步做到

1、将所有与串口读写有关的函数写到一个文件里

2、所有的函数里面有预编译命令处理

int  function( )

{

#ifdef  __SERIOL__

 函数体

return 0;

#else

return  -1;  

#endif

}

3、在次文件头上加上

#define __SERIOL__  
//#undef  __SERIOL__ 

如果其他调试完成以后把//注释去掉即可。

4、本文件的函数在其他文件的函数中正常调用即可,只是有宏的时候是个空函数,直接return -1编译器不会报错。


原创粉丝点击