如何向C ++日志宏添加额外的参数

问题描述 投票:0回答:1

假设我下面有用于记录的宏

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'打入日志?有什么想法吗!?

c++ variadic-macros
1个回答
1
投票

如果fmt始终是字符串文字,则应该起作用:

#define NEW_TRACE(type, sub-type, message, fmt, args) do { \
        TRACE (type, sub-type, message, "IP:%s " fmt, IP, ##args); \
    } while (0)
© www.soinside.com 2019 - 2024. All rights reserved.