Search code examples
cc-preprocessorvariadic-functionsvariadic-macros

Why "vsprintf" is getting stuck when calling a function from a macro using __VA_ARGS__?


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.


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__);