Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trying to combine two similar printf style debug messages in to a single function

Tags:

c++

c

I have two printf style debug logging functions (DebuglogfA, DebuglogfB). Both operate the same way but one of the logging functions takes a logging level as a parameters and ignores low level debug messages.

Currently I duplicate the code for each of these functions but I would like DebuglogfB to be able to call DebuglogfA if the debug level is high enough without having to create a temporary buffer in DebuglogfB.

void DebuglogfA( const char *lpszText, ...)
{
    //Initialize variable argument list
    va_list argList;
    va_start(argList, lpszText);

    char buffer[1024];
    unsigned short length = snprintf_s(buffer, 1024, "[%d] ", CTime::GetCurrentTimeInSec() );
    length += vsnprintf (buffer+length, 1024 - length, lpszText, argList );

    LogSend( buffer, length );
}

void DebuglogfB ( const unsigned int level, const char *lpszText, ... )
{
    if( level < 50 ) {
        return; // To low to report. 
    }

    //Initialize variable argument list
    va_list argList;
    va_start(argList, lpszText);

    char buffer[1024];
    unsigned short length = snprintf_s(buffer, 1024, "[%d] ", CTime::GetCurrentTimeInSec() );
    length += vsnprintf (buffer+length, 1024 - length, lpszText, argList );

    LogSend( buffer, length );
}

My question is:

How do I get function DebuglogfB to call DebuglogfA without creating a buffer for the message in DebuglogfB?

like image 219
Steven Smethurst Avatar asked Jul 20 '11 23:07

Steven Smethurst


1 Answers

You could create a new function DebuglogfV which has const char *lpszText and va_list argList as parameters, and then let DebuglogfA and DebuglogfB call it to perform the actual logging.

like image 123
MRAB Avatar answered Nov 15 '22 00:11

MRAB