6

我的代码中有一些日志记录宏,如下所示:

#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

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

4

5 回答 5

2

the new lcov version 1.11 (or 1.12) introduce LCOV_EXCL_BR_LINE keyword. So in your case:

LOG_MSG(ERROR, "An Error has occurred\n"); //LCOV_EXCL_BR_LINE

or, even better:

LOG_MSG(ERROR, "An Error has occurred\n"); (void)("LCOV_EXCL_BR_LINE");

which survives precompiler comment stripping.

于 2016-11-30T11:27:31.233 回答
1

为什么不把宏变成函数呢?

喜欢:

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
}
于 2014-01-03T17:36:09.370 回答
1

我不知道如何将代码添加到答案中,但这是对@Jarod42 解决方案的回应。我没有使用 C++0x,所以我稍微修改了他的解决方案:

void LogMsgFunc( U32 pri, const char* msg, ... )
{
    //LCOV_EXCL_START
    va_list variableArgumentList;
    va_start( variableArgumentList, msg );
    if ( pri <= PriorityLevel ) 
    { 
        vfprintf( stderr, msg, variableArgumentList );
    }    
    va_end( variableArgumentList );
    //LCOV_EXCL_STOP
}

#define LOG_MSG (pri, msg, ... ) \
    LogMsgFunc(pri, msg, ##__VA_ARGS__);
于 2014-01-06T15:25:39.663 回答
0

怎么样

#define LOG_MSG__LCOV_EXCL_BR_LINE LOG_MSG

然后用LOG_MSG新的宏替换您不希望进行覆盖测试的任何调用LOG_MSG__LCOV_EXCL_BR_LINE。那行得通吗?

于 2017-11-03T04:01:40.757 回答
0

中提到的解决方案怎么样:https ://github.com/linux-test-project/lcov/issues/44#issuecomment-427449082

更改 lcovrc 添加:

lcov_excl_br_line = LCOV_EXCL_BR_LINE|LOG_MSG
于 2020-10-27T13:46:56.840 回答