1

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.

a3f
  • 8,517
  • 1
  • 41
  • 46
m4l490n
  • 1,592
  • 2
  • 25
  • 46
  • What do you mean by "getting stuck"? Also, did you mean to use `vprintf()` instead of `vsprintf()`? – augurar Mar 14 '14 at 03:44
  • You shouldn't use the `##` before `__VA_ARGS__`; you're not pasting it onto another string. And what is `GREEN`, was it supposed to be `GREEN,` ? Otherwise I don't understand that line. – M.M Mar 14 '14 at 04:03
  • 1
    @MattMcNabb ##__VA_ARGS__ is known extension to get rid of `,` sign when __VA_ARGS__ is empty. It is actually correct (but non-standard). – keltar Mar 14 '14 at 04:05

2 Answers2

4

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__);
ldav1s
  • 15,885
  • 2
  • 53
  • 56
keltar
  • 17,711
  • 2
  • 37
  • 42
  • You are right! when I modified the macro to add the __VA_ARGS__ I erased the "__FILE__, __func__, __LINE__," parameters so that was the problem. – m4l490n Mar 17 '14 at 16:02
0

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);;
}
user3386109
  • 34,287
  • 7
  • 49
  • 68