假设我下面有用于记录的宏
TRACE(type,sub-type,message,fmt,args);
现在我需要打出额外的参数,例如将机器IP写入此日志:
#include <conio.h>
char *IP = "100.200.200.100";
#define TRACE(type, sub-type, message, fmt, args) do{ <<<< expands to func which logs to a file >>>> }while(0)
#define NEW_TRACE(type, sub-type, message, fmt, args) do { \
TRACE (type, sub-type, message, fmt, ##args); \
} while (0)
int main()
{
char *w = "world!";
NEW_TRACE("my-type", "my-subtype", 2, "Hello %s", w);
return 0;
}
如何编写NEW_TRACE以便将'IP'打入日志?有什么想法吗!?
如果fmt始终是字符串文字,则应该起作用:
#define NEW_TRACE(type, sub-type, message, fmt, args) do { \
TRACE (type, sub-type, message, "IP:%s " fmt, IP, ##args); \
} while (0)