我的代码中包含一些日志记录宏,内容如下:

#define LOG_MSG (pri, msg, ... ) \
    if (pri > PriorityLevel ) \
        printf( msg, ##\__VA_ARGS__);

我知道我可以使用LCOV_EXCL_START,LCOV_EXCL_STOP或LCOV_EXCL_LINE来禁止分支。但这仅在我将其添加到我称之为LOG_MSG的每个位置时都有效:
LOG_MSG(ERROR, "An Error has occurred\n");//LCOV_EXCL_LINE
我想在宏中包含该注释,但是如果我将其放在其中,LCOV将无法识别它。例如,此代码仍会产生分支。
#define LOG_MSG (pri, msg, ... ) \
    if (pri > PriorityLevel ) \
        printf( msg, ##\__VA_ARGS__);//LCOV_EXCL_LINE

有没有一种好的方法可以抑制宏本身中的这些分支?

最佳答案

为什么不将宏转换为功能?

喜欢:

template <typename ... Ts>
void LOG_MSG(int priority, const std::string& message, Ts&&...ts)
{
    if (priority > PriorityLevel)
        printf(message.c_str(), std::forward<Ts>(ts)...);
    // Or more appropriate stuff
}

09-04 15:36
查看更多