Pass Variable arguments from function to another with variable arguments

124 views Asked by At

I have the following function that I shouldn't change to not break dependencies

void  Trace(
        __in DbgInfo dbgInfo,
        __in_z _Printf_format_string_ const WCHAR* pwszMsgFormat,
        ...) {

            // some code I need     
    };  

I need it to call a function that also has ... args:

#define TRACE_LINE(     format, ... ) _TRACE( LogLevelInfo ,format, __VA_ARGS__ )

#define _TRACE( mask, format, ... )  \
        ((mask) & g_LogLevel)              \
            ? TraceGlobal::TraceLine( mask, L"[" __FILEW__ L":" _BLOB_WIDE(_BLOB_STRINGIZE(__LINE__)) L"] "  L"MyService!" __FUNCTIONW__ L": " format , __VA_ARGS__ ) \
            : (void)0

void
ServiceTracing::TraceGlobal::TraceLine(
    _In_            LogLevel level,
    _In_z_          PCWSTR format,
    _In_            ...)
/**
Routine Description:

    Trace a formatted line with Level tracing

Arguments:

    level: enum level of trace line
    format: the string format
    ...: the variable arguments to be formatted

Return value:

    None
**/
{
    ASSERT(format);

    va_list args;
    va_start(args, format);

    // code does tracing

    va_end(args);

}

how can I do that?

Inside the main Trace function above I tried this:

TRACE_LINE( pwszMsgFormat, __VA_ARGS__ );

this didn't work however and I got error:

 syntax error : missing ')' before identifier 'pwszMsgFormat'
 error C2059: syntax error : ')'

I am assuming because it is macro the __VA_ARGS__ it gives this error.

On the other hand I tried calling the TraceGlobal directly

va_list args;
va_start(args, pwszMsgFormat);
TraceGlobal::TraceLine(LogLevelInfo, pwszMsgFormat , args );
va_end(args);

this builds fine but I get Segmentation Fault / Access Violation in the tracing function because I also initialize va_list and use va_start in TraceLine

Is there a simple way I can just pass along the variable arguments from function to another. I don't want to change Trace function to Macro.

0

There are 0 answers