Question

I have the following macro:

#define TRACE__LOW(str, col, ...)\
        TR_Trace("\r\e[" COLOR(col) "%s :: %s():%d; LOW - " str "\e[0m\r\n",\
        ##__VA_ARGS__);

And the function TR_Trace looks like this:

void TR_Trace(const char *const string, ...)
{
   va_list aptr;
   size_t stringSize = 0;
   char tempString[250];

   va_start(aptr, string);
   vsprintf(tempString, string, aptr);
   va_end(aptr);
}

And I'm using it like this:

TRACE__LOW("Led[%d] toggled every %d milliseconds (%5d)", GREEN
            init_parameters.led, delay_time, counter++);

The problem here is that once the execution gets to vsprintf(tempString, string, aptr); it gets stuck there.

Do anybody know what is happening or if I'm not using correctly the VA_ARGS?

Regards.

Was it helpful?

Solution

You adding %s :: %s():%d; to format string, but don't adding extra arguments to fill these patterns.

I suppose it meant to be

#define TRACE__LOW(str, col, ...)\
        TR_Trace("\r\e[" COLOR(col) "%s :: %s():%d; LOW - " str "\e[0m\r\n",\
        __FILE__, __func__, __LINE__,\
        ##__VA_ARGS__);

OTHER TIPS

Random thoughts:

Your use of __VA_ARGS__ appears to be correct.

The TRACE__LOW macro adds a superfluous semi-colon to the output (could cause problems for conditional statements that don't have curly braces).

I don't know what COLOR(GREEN) expands to, so that may be the source of the problem.

Suggestion:

Your compiler should have an option to output the results from the preprocessor. On the compiler I'm using that option is -E. If you have the compiler output the results from the preprocessor, you can see precisely what your macro expands to.

For example, compiling the following code with -E

#include <stdio.h>

#define TRACE__LOW(str, col, ...)\
TR_Trace("\r\e[" COLOR(col) "%s :: %s():%d; LOW - " str "\e[0m\r\n",\
##__VA_ARGS__);

int main( void )
{
    TRACE__LOW( "test", 3, a, b, c );
}

produces this output (after about 5 million other lines ;) )

int main( void )
{
    TR_Trace("\r\e[" COLOR(3) "%s :: %s():%d; LOW - " "test" "\e[0m\r\n", a, b, c);;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top