【发布时间】:2011-10-31 12:35:28
【问题描述】:
我在原版C中有以下宏函数:
#define GLOG(format_string, ...) { \
const char *file = strrchr(__FILE__, '/'); \
char format[256] = "%s:%s!%d\t"; \
strncat(format, format_string, 248); \
strcat(format, "\n"); \
printf(format, __FUNCTION__, file ? file : __FILE__, __LINE__, ##__VA_ARGS__); \
}
它让我打印一条包含当前函数、文件和行号的调试消息,例如
GLOG("count=%d", count);
可能会打印
do_count:counter.c!123 count=456
-
如果调用者省略了 format_string,如何修改函数以打印所有局部变量?例如
GLOG();可能会打印
do_count:counter.c!123 count=456, message="Hello world", array=[7, 8] structure={ptr=0xACE0FBA5E, coord={x=9, y=0}} -
如果这不可能,我该如何修改它以仅打印当前函数、文件和行号?例如
do_count:counter.c!123照原样,这会返回一个错误:
错误:',' 标记之前的预期表达式
strncat行很简单strncat(format, , 248);
【问题讨论】:
标签: c macros c-preprocessor