static void DTEDFormat( unsigned char *pszTarget, size_t nTargetLenSize, size_t nOffset, const char *pszFormat, ... ) { va_list args; va_start(args, pszFormat); CPLvsnprintf( (char*)pszTarget + nOffset, nTargetLenSize - nOffset, pszFormat, args ); va_end(args); }
void CPLErrorV(CPLErr eErrClass, int err_no, const char *fmt, va_list args ) { CPLErrorContext *psCtx = CPLGetErrorContext(); if (psCtx->nFailureIntoWarning > 0 && eErrClass == CE_Failure) eErrClass = CE_Warning; /* -------------------------------------------------------------------- */ /* Expand the error message */ /* -------------------------------------------------------------------- */ #if defined(HAVE_VSNPRINTF) { int nPR; va_list wrk_args; #ifdef va_copy va_copy( wrk_args, args ); #else wrk_args = args; #endif /* -------------------------------------------------------------------- */ /* If CPL_ACCUM_ERROR_MSG=ON accumulate the error messages, */ /* rather than just replacing the last error message. */ /* -------------------------------------------------------------------- */ int nPreviousSize = 0; if ( psCtx->psHandlerStack != NULL && EQUAL(CPLGetConfigOption( "CPL_ACCUM_ERROR_MSG", "" ), "ON")) { nPreviousSize = strlen(psCtx->szLastErrMsg); if (nPreviousSize) { if (nPreviousSize + 1 + 1 >= psCtx->nLastErrMsgMax) { psCtx->nLastErrMsgMax *= 3; psCtx = (CPLErrorContext *) CPLRealloc(psCtx, sizeof(CPLErrorContext) - DEFAULT_LAST_ERR_MSG_SIZE + psCtx->nLastErrMsgMax + 1); CPLSetTLS( CTLS_ERRORCONTEXT, psCtx, TRUE ); } psCtx->szLastErrMsg[nPreviousSize] = '\n'; psCtx->szLastErrMsg[nPreviousSize+1] = '0'; nPreviousSize ++; } } while( ((nPR = CPLvsnprintf( psCtx->szLastErrMsg+nPreviousSize, psCtx->nLastErrMsgMax-nPreviousSize, fmt, wrk_args )) == -1 || nPR >= psCtx->nLastErrMsgMax-nPreviousSize-1) && psCtx->nLastErrMsgMax < 1000000 ) { #ifdef va_copy va_end( wrk_args ); va_copy( wrk_args, args ); #else wrk_args = args; #endif psCtx->nLastErrMsgMax *= 3; psCtx = (CPLErrorContext *) CPLRealloc(psCtx, sizeof(CPLErrorContext) - DEFAULT_LAST_ERR_MSG_SIZE + psCtx->nLastErrMsgMax + 1); CPLSetTLS( CTLS_ERRORCONTEXT, psCtx, TRUE ); } va_end( wrk_args ); } #else // !HAVE_VSNPRINTF CPLvsnprintf( psCtx->szLastErrMsg, psCtx->nLastErrMsgMax, fmt, args); #endif /* -------------------------------------------------------------------- */ /* Obfuscate any password in error message */ /* -------------------------------------------------------------------- */ char* pszPassword = strstr(psCtx->szLastErrMsg, "password="******"password="******"CPL_LOG_ERRORS",NULL) != NULL ) CPLDebug( "CPLError", "%s", psCtx->szLastErrMsg ); /* -------------------------------------------------------------------- */ /* Invoke the current error handler. */ /* -------------------------------------------------------------------- */ if( psCtx->psHandlerStack != NULL ) { psCtx->psHandlerStack->pfnHandler(eErrClass, err_no, psCtx->szLastErrMsg); } else { CPLMutexHolderD( &hErrorMutex ); if( pfnErrorHandler != NULL ) pfnErrorHandler(eErrClass, err_no, psCtx->szLastErrMsg); } if( eErrClass == CE_Fatal ) abort(); }
void CPLDebug( const char * pszCategory, const char * pszFormat, ... ) { CPLErrorContext *psCtx = CPLGetErrorContext(); char *pszMessage; va_list args; const char *pszDebug = CPLGetConfigOption("CPL_DEBUG",NULL); #define ERROR_MAX 25000 /* -------------------------------------------------------------------- */ /* Does this message pass our current criteria? */ /* -------------------------------------------------------------------- */ if( pszDebug == NULL ) return; if( !EQUAL(pszDebug,"ON") && !EQUAL(pszDebug,"") ) { size_t i, nLen = strlen(pszCategory); for( i = 0; pszDebug[i] != '\0'; i++ ) { if( EQUALN(pszCategory,pszDebug+i,nLen) ) break; } if( pszDebug[i] == '\0' ) return; } /* -------------------------------------------------------------------- */ /* Allocate a block for the error. */ /* -------------------------------------------------------------------- */ pszMessage = (char *) VSIMalloc( ERROR_MAX ); if( pszMessage == NULL ) return; /* -------------------------------------------------------------------- */ /* Dal -- always log a timestamp as the first part of the line */ /* to ensure one is looking at what one should be looking at! */ /* -------------------------------------------------------------------- */ pszMessage[0] = '\0'; #ifdef TIMESTAMP_DEBUG if( CPLGetConfigOption( "CPL_TIMESTAMP", NULL ) != NULL ) { strcpy( pszMessage, VSICTime( VSITime(NULL) ) ); // On windows anyway, ctime puts a \n at the end, but I'm not // convinced this is standard behaviour, so we'll get rid of it // carefully if (pszMessage[strlen(pszMessage) -1 ] == '\n') { pszMessage[strlen(pszMessage) - 1] = 0; // blow it out } strcat( pszMessage, ": " ); } #endif /* -------------------------------------------------------------------- */ /* Add the process memory size. */ /* -------------------------------------------------------------------- */ #ifdef MEMORY_DEBUG char szVmSize[32]; CPLsprintf( szVmSize, "[VmSize: %d] ", CPLGetProcessMemorySize()); strcat( pszMessage, szVmSize ); #endif //sprintf(pszMessage,"[%d] ", (int)getpid()); /* -------------------------------------------------------------------- */ /* Add the category. */ /* -------------------------------------------------------------------- */ strcat( pszMessage, pszCategory ); strcat( pszMessage, ": " ); /* -------------------------------------------------------------------- */ /* Format the application provided portion of the debug message. */ /* -------------------------------------------------------------------- */ va_start(args, pszFormat); CPLvsnprintf(pszMessage+strlen(pszMessage), ERROR_MAX - strlen(pszMessage), pszFormat, args); va_end(args); /* -------------------------------------------------------------------- */ /* Obfuscate any password in error message */ /* -------------------------------------------------------------------- */ char* pszPassword = strstr(pszMessage, "password="******"password="); while( *pszIter != ' ' && *pszIter != '\0' ) { *pszIter = 'X'; pszIter ++; } } /* -------------------------------------------------------------------- */ /* Invoke the current error handler. */ /* -------------------------------------------------------------------- */ if( psCtx->psHandlerStack != NULL ) { psCtx->psHandlerStack->pfnHandler( CE_Debug, CPLE_None, pszMessage ); } else { CPLMutexHolderD( &hErrorMutex ); if( pfnErrorHandler != NULL ) pfnErrorHandler( CE_Debug, CPLE_None, pszMessage ); } VSIFree( pszMessage ); }
CPLString &CPLString::vPrintf( const char *pszFormat, va_list args ) { /* -------------------------------------------------------------------- */ /* This implementation for platforms without vsnprintf() will */ /* just plain fail if the formatted contents are too large. */ /* -------------------------------------------------------------------- */ #if !defined(HAVE_VSNPRINTF) char *pszBuffer = (char *) CPLMalloc(30000); if( CPLvsnprintf( pszBuffer, 30000, pszFormat, args) > 29998 ) { CPLError( CE_Fatal, CPLE_AppDefined, "CPLString::vPrintf() ... buffer overrun." ); } *this = pszBuffer; CPLFree( pszBuffer ); /* -------------------------------------------------------------------- */ /* This should grow a big enough buffer to hold any formatted */ /* result. */ /* -------------------------------------------------------------------- */ #else va_list wrk_args; #ifdef va_copy va_copy( wrk_args, args ); #else wrk_args = args; #endif char szModestBuffer[500]; szModestBuffer[0] = '\0'; int nPR = CPLvsnprintf( szModestBuffer, sizeof(szModestBuffer), pszFormat, wrk_args ); if( nPR == -1 || nPR >= (int) sizeof(szModestBuffer)-1 ) { int nWorkBufferSize = 2000; char *pszWorkBuffer = reinterpret_cast<char *>( CPLMalloc(nWorkBufferSize)); #ifdef va_copy va_end( wrk_args ); va_copy( wrk_args, args ); #else wrk_args = args; #endif while( (nPR=CPLvsnprintf( pszWorkBuffer, nWorkBufferSize, pszFormat,wrk_args)) >= nWorkBufferSize-1 || nPR == -1 ) { nWorkBufferSize *= 4; pszWorkBuffer = (char *) CPLRealloc(pszWorkBuffer, nWorkBufferSize ); #ifdef va_copy va_end( wrk_args ); va_copy( wrk_args, args ); #else wrk_args = args; #endif } *this = pszWorkBuffer; CPLFree( pszWorkBuffer ); } else { *this = szModestBuffer; } #ifdef va_copy va_end( wrk_args ); #endif #endif /* !defined(HAVE_VSNPRINTF) */ return *this; }