Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to exclude lcov branches within a macro

I've got some logging macros in my code along the lines of:

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

I know I can use LCOV_EXCL_START, LCOV_EXCL_STOP or LCOV_EXCL_LINE to suppress a branch. But that only works if I add it every place I call LOG_MSG:

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

I'd like to include that comment in the macro, but LCOV doesn't recognize it if I put it there. For example, this code still produces branches.

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

Is there a good way to suppress these branches in the macro itself?

like image 371
perogiex Avatar asked Jan 03 '14 15:01

perogiex


2 Answers

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.

like image 84
tutejszy Avatar answered Sep 28 '22 07:09

tutejszy


Why not turn the macro into function ?

like:

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
}
like image 40
Jarod42 Avatar answered Sep 28 '22 07:09

Jarod42