0

I have a method in a class with this signature:

void addMessage_( std::string appender, LogLevel level /*= LOGLEVEL_INFO*/, char* msg, ... );

I want to create 'alias' of this method in this way:

void debugMsg( std::string appender, char* msg, ... ){
 addMessage(appender, LOGLEVEL_DEBUG, msg, ...);
}

My question is: do I need to expand the args to call the addMessage method? I don't want to replicate the code I each utility method, but I don't want to waste performance. What is the best solution?

Solved: I let my solution as a documentation:

void DEBUG_MSG(std::string appender, char* msg, ...){
 va_list argptr;
 va_start(argptr,msg);
 addMessage_(appender, LOGLEVEL_DEBUG, msg, argptr);
 va_end(argptr);
}

And the addMessage_ method:

 void CGlobalLog::addMessage_( std::string appender, LogLevel level, char* msg, va_list args ){
 int len;
 char *buffer;
 len = _vscprintf( msg, args ) // _vscprintf doesn't count
 + 1; // terminating '0円'
 buffer = (char*)malloc( len * sizeof(char) );
 vsprintf( buffer, msg, args ); // C4996
 // Note: vsprintf is deprecated; consider using vsprintf_s instead
 addMessage(buffer,appender,level);
 free( buffer );
}

Thanks!

asked Apr 21, 2011 at 10:14

2 Answers 2

2

It's not possible that way (without macros), you must change your original function to take a va_list.

It is fully explained here: Passing variable number of arguments around

answered Apr 21, 2011 at 10:23
Sign up to request clarification or add additional context in comments.

Comments

0

Macro can solve that in just one line:

#define debugMsg(appender,msg,...) addMessage(appender,LOGLEVEL_DEBUG, msg, __VA_ARGS__)

Or, you've to make use of va_list, va_start and va_end as explained here.

answered Apr 21, 2011 at 10:22

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.