C puzzle ...如何将可变参数传递给宏?

kum*_*umo 5 c variadic-functions c-preprocessor

我被困在这里......

#include <stdio.h>

#define DBG_LVL(lvl, stmt) \
do{ \
    if(lvl>1)  printf stmt; \
}while(0)

#define DBG_INFO(stmt)   DBG_LVL(1, stmt)
#define DBG_ERROR(stmt)  DBG_LVL(2, stmt)


int main()
{
    DBG_INFO(("hello, %s!\n", "world"));
    DBG_ERROR(("crazy, %s!\n", "world"));
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

如您所见,上面的代码使用"DBG_INFO"或"DBG_ERROR"等宏来控制调试信息级别.

现在由于某种原因,我必须DBG_LVL()用新功能替换.

void myprint(int lvl, const char * format, ...);
Run Code Online (Sandbox Code Playgroud)

唯一的区别是调试级别被视为其fisrt参数.我刚在想:

#define DBG_LVL(lvl, stmt) myprint(lvl, stmt)
Run Code Online (Sandbox Code Playgroud)

当然它失败了,因为"stmt"表达式包括括号.然后我用Google搜索试图找到剥离括号的方法,似乎没有什么可以帮助.我也尝试了一些技巧将参数传递给"stmt",仍然失败...... :(

你能帮助我吗?

Jam*_*lis 10

# define EXPAND_ARGS(...) __VA_ARGS__
# define DBG_LVL(lvl, stmt) myprint(lvl, EXPAND_ARGS stmt);
Run Code Online (Sandbox Code Playgroud)