开发者

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

开发者 https://www.devze.com 2023-03-22 07:04 出处:网络
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 m

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( 开发者_JAVA百科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?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消