借调试宏的设计,梳理下宏的用法
嵌入式设备基本会配置RS232串口作为调试IO接口,假设底层串口单字节输出函数为SERIAL_PutChar(),利用fputc()和fputs()重定向printf函数 ```cpp void fputc(int byte, FILE* stream) { (void)stream;
SERIAL_PutChar(byte);
}
void fputs(const char *pstr, FILE *stream) { (void)stream;
while(*pstr)
{
SERIAL_PutChar(*pstr++);
}
} ```这样在代码里面利用printf()函数输出的字符串都老老实实从调试串口出来
某个C驱动模块,希望在调试时打印调试信息,而产品代码中不显示调试信息。
```cpp
#define DRV_PRINT(x) printf(x)
#define DRV_PRINT(x)
```这个版本的DRVPRINT(x)只能输出单变量——纯字符串
1
2
3
4 |
void
foo() { DRV_PRINT( "Driver Initialize Success!" ); } |
```
当然也可以直接这样定义 ```cpp
```但是如果宏调用了多个参数:
1
2
3
4 |
void
foo() { DRV_PRINT( "Driver Initialize Success: ver %d.%d !" , 1, 2); } |
怎么办?一种处女座接受不了的做法,多加对
号
1
2
3
4 |
void
foo() { DRV_PRINT(( "Driver Initialize Success: ver %d.%d !" , 1, 2)); } |
待续
待续
原文:http://www.cnblogs.com/codeinsight/p/macro.html