Exemplo n.º 1
0
char* form(const char* format ...)
{
	register char* buf = bfree;
	if (max < buf+fld_size) buf = formbuf;

#	ifdef VSPRINTF
		va_list args ;
		va_start(args,format) ;
		VSPRINTF(buf,format,args) ;
		va_end(args) ;
#	else
		// not very portable
		register int* ap = (int*)((char*)&format+sizeof(char*));
		sprintf(buf,format,ap[0],ap[1],ap[2],ap[3],ap[4],ap[5],ap[6],ap[7],ap[8],ap[9]);	
#	endif

	register int ll = strlen(buf);			// not all sprintf's return length

	// If we have scribbled beyond the end of the buffer then
	// who knows what data we've destroyed.  Better to abort here
	// here there is some chance that somebody can associate
	// the location with the error than to continue and die
	// a mysterious death later.
//	if (fld_size < ll) abort();
	if (buf+ll >= formbuf+cb_size-1) {
	    fprintf(stderr, fmsg1);
	    abort();
	}
	
	bfree = buf+ll+1;
	return buf;
}
Exemplo n.º 2
0
/* Wrapper for sprintf() */
int	gtm_sprintf(char *str, const char *format, ...)
{
	va_list		printargs;
	int		retval;

	va_start(printargs, format);
	VSPRINTF(str, format, printargs, retval);
	va_end(printargs);
	return retval;
}
Exemplo n.º 3
0
//--------------------------------------------------------------------------------
std_string Format(const string_char* format, ...)
{
	string_char strMessage[1024];

	va_list ArgPtr;

	va_start(ArgPtr, format);
	VSPRINTF(strMessage, 1024, format, ArgPtr);
	va_end(ArgPtr);

	return strMessage;
}
Exemplo n.º 4
0
void UserMsg(
    HWND        hwnd,   // in
    const char* format, // in: args like printf
                ...)    // in
{
    va_list args;
    va_start(args, format);
    VSPRINTF(msg_g, format, args);
    va_end(args);
    msg_g[0] = char(toupper(msg_g[0]));
    TimedUserMsg1(hwnd, int(1e7)); // ten thousand seconds
}
Exemplo n.º 5
0
void TimedUserMsg(
    HWND        hwnd,   // in
    const char* format, // in: args like printf
                ...)    // in
{
    va_list args;
    va_start(args, format);
    VSPRINTF(msg_g, format, args);
    va_end(args);
    msg_g[0] = char(toupper(msg_g[0]));
    TimedUserMsg1(hwnd, 3000); // 3000 ms
}
Exemplo n.º 6
0
int gtm_snprintf(char *str, size_t size, const char *format, ...)
{ /* hack for VMS, ignore size argument and call sprintf. When snprintf becomes available on VMS, nix this file and define SNPRINTF
   * in gtm_stdio.h to snprintf */

	va_list	printargs;
	int	retval, rc;

	va_start(printargs, format);
	retval = VSPRINTF(str, format, printargs, rc);
	va_end(printargs);
	return retval;
}
Exemplo n.º 7
0
void PrintManager::Printf(const HashedString& Category, int Level,
                          const char* Format, ...) {
  Map<HashedString, int>::Iterator CatIter;
  if (Category) {
    CatIter = sPrintLevels.Search(Category);
  }
  // If we don't have a level defined for the given category, use the default
  if (CatIter.IsNull()) {
    CatIter = sPrintLevels.Search(HashedString::NullString);
  }
  int PrintLevel = CatIter.IsNull() ? 0 : *CatIter;

  if (Level <= PrintLevel) {
    va_list Args;

    va_start(Args, Format);
    VSPRINTF(m_StringBuffer, STRINGBUFFERSIZE, Format, Args);

    // Now send string wherever I want it to go...

    if (m_Channels & PRINTCHANNEL_Console) {
      fputs(m_StringBuffer, stdout);  // puts appends an \n
    }

#if BUILD_DEV
    if (m_Channels & PRINTCHANNEL_Output) {
#if BUILD_WINDOWS
      OutputDebugString(m_StringBuffer);
#else
// TODO PORT LATER: Output to debugger on other platforms.
#endif
    }
#endif

    // Handle logging differently in Debug and Release
    if (m_Channels & PRINTCHANNEL_Log) {
#if OPENLOGFILE
      if (m_LogFilename) {
        FileStream LogStream(m_LogFilename,
                             FileStream::EFM_Append);  // Append files for every
                                                       // separate print; slow.
        LogStream.PrintF(m_StringBuffer);
      }
#else
      if (m_LogStream) {
        m_LogStream->PrintF(m_StringBuffer);
      }
#endif
    }
  }
}
Exemplo n.º 8
0
/*static*/ SimpleString SimpleString::PrintF( const char* FormatString, ... )
{
	va_list Args;
	va_start( Args, FormatString );

	int Length = VSPRINTF_COUNT( FormatString, Args ) + 1;
	char* pBuffer = Allocate( Length );
	VSPRINTF( pBuffer, Length, FormatString, Args );

	SimpleString RetVal( pBuffer );
	SafeDeleteArray( pBuffer );

	return RetVal;
}
Exemplo n.º 9
0
/**
 * Like asprintf, just portable.
 *
 * @param buf set to a buffer of sufficient size (allocated, caller must free)
 * @param format format string (see printf, fprintf, etc.)
 * @param ... data for format string
 * @return number of bytes in "*buf" excluding 0-termination
 */
int
GNUNET_asprintf (char **buf, const char *format, ...)
{
  int ret;
  va_list args;

  va_start (args, format);
  ret = VSNPRINTF (NULL, 0, format, args);
  va_end (args);
  *buf = GNUNET_malloc (ret + 1);
  va_start (args, format);
  ret = VSPRINTF (*buf, format, args);
  va_end (args);
  return ret;
}
Exemplo n.º 10
0
int DynamicMemoryStream::PrintF(const char* Str, ...) const {
  va_list Args;
  int Length = 0;
  char* Buffer = nullptr;
  int RetVal = 0;

  va_start(Args, Str);
  Length = VSPRINTF_COUNT(Str, Args) + 1;
  Buffer =
      new char[Length];  // TODO: Pool this instead of dynamically allocating
  VSPRINTF(Buffer, Length, Str, Args);

  RetVal = Write(Length - 1, Buffer);

  SafeDelete(Buffer);
  return RetVal;
}
Exemplo n.º 11
0
int FileStream::PrintF( const char* Str, ... ) const
{
	ASSERT( m_FileMode == EFM_Write || m_FileMode == EFM_Append );

	va_list	Args;
	int		Length	= 0;
	char*	Buffer	= NULL;
	int		RetVal	= 0;

	va_start( Args, Str );
	Length = VSPRINTF_COUNT( Str, Args ) + 1;
	Buffer = new char[ Length ];	// TODO: Pool this instead of dynamically allocating
	VSPRINTF( Buffer, Length, Str, Args );

	RetVal = Write( Length - 1, Buffer );

	SafeDelete( Buffer );
	return RetVal;
}
Exemplo n.º 12
0
// internal function
void dnm_cli_printf_v (const char *format, va_list arg)
{
   static  BOOLEAN  wasError = FALSE;

   INT32S  len, hdrLen;
   INT32S  res;
   char    buf[DN_CLI_CTRL_SIZE];
   BOOLEAN prevVal;
   CS_LOCAL_VAR;

//   ((dn_cli_ctrlMsg_t *)buf)->cmdId   = DN_CLI_CMD_TYPE_TRACE;
#ifdef WIN32
   if (dnm_cli_v.pCliLogFile != NULL) vfprintf(dnm_cli_v.pCliLogFile, format, arg);
#endif
   
//   hdrLen = sizeof(dn_cli_ctrlMsg_t);
   hdrLen = 0;

   if (wasError) {   // Add "..."
      SNPRINTF(buf + hdrLen, sizeof(buf) - hdrLen, ERR_INDICATOR);
      hdrLen += strlen(ERR_INDICATOR);
   }

   len = VSPRINTF(buf + hdrLen, sizeof(buf) - hdrLen, format, arg);
   if(len < 0)   // error - print '***********'
      len = SNPRINTF(buf + hdrLen, sizeof(buf) - hdrLen, "*** CLI_LEN_ERROR ***\r\n");
   buf[sizeof(buf)-1] = 0;
   len += hdrLen;
   if (len>sizeof(buf)) 
      len = sizeof(buf);  
   
   prevVal = wasError;
   res = dn_write(DN_CLI_DEV_ID, buf, len);

   OS_ENTER_CRITICAL();
   if (res == DN_ERR_NO_RESOURCES || (!prevVal && wasError))
      wasError = TRUE;
   else
      wasError = FALSE;
   OS_EXIT_CRITICAL();
}
Exemplo n.º 13
0
/*! 
 *************************************************************************************
 * \brief	System trace log output in Wels
 *
 * \param	pCtx	instance pointer
 * \param	kiLevel	log iLevel ( WELS_LOG_QUIET, ERROR, WARNING, INFO, DEBUG )
 * \param	kpFmtStr	formated string to mount
 * \param 	argv	pData string argument
 *
 * \return	NONE
 *
 * \note	N/A
 *************************************************************************************
 */
void WelsLogDefault( void *pCtx, const int32_t kiLevel, const str_t *kpFmtStr, va_list argv )
{
	sWelsEncCtx *pEncCtx	= (sWelsEncCtx *)pCtx;
	iWelsLogLevel		 iVal	= (kiLevel & g_iLevelLog);

	if ( 0 == iVal || NULL == pEncCtx )	// such iLevel not enabled
	{
		return;
	}
	else
	{
		str_t pBuf[WELS_LOG_BUF_SIZE+1] = {0};		
		const int32_t kiBufSize = sizeof(pBuf) / sizeof(pBuf[0]) - 1;
		int32_t iCurUsed = 0;
		int32_t iBufUsed = 0;
		int32_t iBufLeft = kiBufSize - iBufUsed;
		
		if ( pEncCtx ){
			time_t l_time;
#if defined(WIN32)
#if defined(_MSC_VER)
#if _MSC_VER >= 1500
			struct tm t_now;
#else//VC6
			struct tm* t_now;
#endif//_MSC_VER >= 1500
#endif//_MSC_VER
#else//__GNUC__
			struct tm* t_now;
#endif//WIN32			
			
#if defined( WIN32 )
			struct _timeb tb;
			
			time(&l_time);
#ifdef _MSC_VER
#if _MSC_VER >= 1500
			LOCALTIME(&t_now, &l_time);
#else
			t_now = LOCALTIME(&l_time);
			if ( NULL == t_now )
			{
				return;
			}
#endif//_MSC_VER >= 1500
#endif//_MSC_VER			
			FTIME(&tb);
#elif defined( __GNUC__ )
			struct timeval tv;
			time(&l_time);
			t_now = (struct tm *)LOCALTIME(&l_time);
			gettimeofday(&tv,NULL);
#endif//WIN32
			if (iBufLeft > 0){
#ifdef _MSC_VER
#if _MSC_VER >= 1500
				iCurUsed = SNPRINTF( &pBuf[iBufUsed], iBufLeft, iBufLeft, "[0x%p @ ", pEncCtx );	// confirmed_safe_unsafe_usage
#else
				iCurUsed = SNPRINTF( &pBuf[iBufUsed], iBufLeft, "[0x%p @ ", pEncCtx );	// confirmed_safe_unsafe_usage
#endif//_MSC_VER >= 1500
#endif//_MSC_VER
				if (iCurUsed >= 0){
					iBufUsed += iCurUsed;
					iBufLeft -= iCurUsed;
				}				
			}
			else{
				return;
			}

			if ( iBufLeft > 0 ){			
				iCurUsed = GetCodeName( &pBuf[iBufUsed], iBufLeft );
				if ( iCurUsed > 0 ){
					iBufUsed += iCurUsed;
					iBufLeft -= iCurUsed;
				}
				pBuf[iBufUsed] = ' ';
				++ iBufUsed;
				-- iBufLeft;
				
				iCurUsed = GetLibName( &pBuf[iBufUsed], iBufLeft );
				if ( iCurUsed > 0 ){
					iBufUsed += iCurUsed;
					iBufLeft -= iCurUsed;
				}
				pBuf[iBufUsed] = ' ';
				++ iBufUsed;
				-- iBufLeft;

				pBuf[iBufUsed] = 'v';
				++ iBufUsed;
				-- iBufLeft;		
				iCurUsed = GetVerNum( &pBuf[iBufUsed], iBufLeft );
				if ( iCurUsed > 0 ){
					iBufUsed += iCurUsed;
					iBufLeft -= iCurUsed;
				}
				pBuf[iBufUsed] = ' ';
				++ iBufUsed;
				-- iBufLeft;				
			}

			if (iBufLeft > 0){
#if defined(WIN32) && defined(_MSC_VER) && (_MSC_VER >= 1500)
				iCurUsed = strftime(&pBuf[iBufUsed], iBufLeft, "%y-%m-%d %H:%M:%S", &t_now);
#else
				iCurUsed = strftime(&pBuf[iBufUsed], iBufLeft, "%y-%m-%d %H:%M:%S", t_now);
#endif//WIN32..
				if (iCurUsed > 0){
					iBufUsed += iCurUsed;
					iBufLeft -= iCurUsed;
				}
			}
			else{
				return;
			}

			if (iBufLeft > 0){
#if defined (WIN32)
#ifdef _MSC_VER
#if _MSC_VER >= 1500
				iCurUsed = SNPRINTF(&pBuf[iBufUsed], iBufLeft, iBufLeft, ".%03.3u]: ", tb.millitm);	// confirmed_safe_unsafe_usage
#else
				iCurUsed = SNPRINTF(&pBuf[iBufUsed], iBufLeft, ".%3.3u]: ", tb.millitm);	// confirmed_safe_unsafe_usage
#endif//_MSC_VER >= 1500
#endif//_MSC_VER
#elif defined (__GNUC__)
				iCurUsed = SNPRINTF(&pBuf[iBufUsed], iBufLeft, ".%3.3u]: ", tv.tv_usec/1000);	// confirmed_safe_unsafe_usage
#endif//WIN32
				if (iCurUsed >= 0){
					iBufUsed += iCurUsed;
					iBufLeft -= iCurUsed;
				}
			}
			else{
				return;
			}
		}

		// fixed stack corruption issue on vs2008
		if ( iBufLeft > 0 ){
			int32_t i_shift = 0;			
			str_t *pStr = NULL;
			pStr	= GetLogTag( kiLevel, &i_shift );
			if ( NULL != pCtx){
				int32_t iLenTag = STRNLEN( pStr, 8 );	// confirmed_safe_unsafe_usage
				STRCAT( &pBuf[iBufUsed], iBufLeft, pStr );	// confirmed_safe_unsafe_usage
				iBufUsed += iLenTag;
				pBuf[iBufUsed] = ' ';
				iBufUsed++;
				++iLenTag;
				iBufLeft -= iLenTag;
			}			
		}
		if (iBufLeft > 0){
#if defined(WIN32) && defined(_MSC_VER) && (_MSC_VER >= 1500)
			int32_t len = 0;
			len = _vscprintf( kpFmtStr, argv ) // _vscprintf doesn't count
					+ 1; // terminating '\0'
			iCurUsed = VSPRINTF(&pBuf[iBufUsed], len, kpFmtStr, argv);	// confirmed_safe_unsafe_usage
#else
			iCurUsed = VSPRINTF(&pBuf[iBufUsed], kpFmtStr, argv);	// confirmed_safe_unsafe_usage
#endif//WIN32..
			if (iCurUsed > 0){
				iBufUsed += iCurUsed;
				iBufLeft -= iCurUsed;
			}
		}
#ifdef ENABLE_TRACE_FILE
		if (NULL != pEncCtx && NULL != pEncCtx->pFileLog){
			if ( pEncCtx->uiSizeLog > MAX_TRACE_LOG_SIZE){
				if (0 == fseek(pEncCtx->pFileLog, 0L, SEEK_SET))
					pEncCtx->uiSizeLog = 0;
			}
			if ( iBufUsed > 0 && iBufUsed < WELS_LOG_BUF_SIZE )
			{
				iCurUsed = fwrite(pBuf, 1, iBufUsed, pEncCtx->pFileLog);
				fflush( pEncCtx->pFileLog );
				if ( iCurUsed == iBufUsed )
					pEncCtx->uiSizeLog += iBufUsed;
			}			
		}
		else{
#if defined(WIN32) && defined(_DEBUG)
			OutputDebugStringA(pBuf);
#endif
		}
#endif//ENABLE_TRACE_FILE
	}	
}
Exemplo n.º 14
0
Arquivo: logging.c Projeto: aosm/bind
void
log_vwrite(log_context lc, int category, int level, const char *format, 
	   va_list args) {
	log_channel_list lcl;
	int pri, debugging, did_vsprintf = 0;
	int original_category;
	FILE *stream;
	log_channel chan;
	struct timeval tv;
	struct tm *local_tm;
	const char *category_name;
	const char *level_str;
	char time_buf[256];
	char level_buf[256];

	REQUIRE(lc != NULL);

	debugging = (lc->flags & LOG_OPTION_DEBUG);

	/*
	 * If not debugging, short circuit debugging messages very early.
	 */
	if (level > 0 && !debugging)
		return;

	if (category < 0 || category > lc->num_categories)
		category = 0;		/* use default */
	original_category = category;
	lcl = lc->categories[category];
	if (lcl == NULL) {
		category = 0;
		lcl = lc->categories[0];
	}

	/*
	 * Get the current time and format it.
	 */
	time_buf[0]='\0';
	if (gettimeofday(&tv, NULL) < 0) {
		syslog(LOG_INFO, "gettimeofday failed in log_vwrite()");
	} else {
#ifdef HAVE_TIME_R
		localtime_r((time_t *)&tv.tv_sec, &local_tm);
#else
		local_tm = localtime((time_t *)&tv.tv_sec);
#endif
		if (local_tm != NULL) {
			sprintf(time_buf, "%02d-%s-%4d %02d:%02d:%02d.%03ld ",
				local_tm->tm_mday, months[local_tm->tm_mon],
				local_tm->tm_year+1900, local_tm->tm_hour,
				local_tm->tm_min, local_tm->tm_sec,
				(long)tv.tv_usec/1000);
		}
	}

	/*
	 * Make a string representation of the current category and level
	 */

	if (lc->category_names != NULL &&
	    lc->category_names[original_category] != NULL)
		category_name = lc->category_names[original_category];
	else
		category_name = "";

	if (level >= log_critical) {
		if (level >= 0) {
			sprintf(level_buf, "debug %d: ", level);
			level_str = level_buf;
		} else
			level_str = level_text[-level-1];
	} else {
		sprintf(level_buf, "level %d: ", level);
		level_str = level_buf;
	}

	/*
	 * Write the message to channels.
	 */
	for ( /* nothing */; lcl != NULL; lcl = lcl->next) {
		chan = lcl->channel;

		if (!log_check_channel(lc, level, chan))
			continue;

		if (!did_vsprintf) {
			if (VSPRINTF((lc->buffer, format, args)) >
			    LOG_BUFFER_SIZE) {
				syslog(LOG_CRIT,
				       "memory overrun in log_vwrite()");
				exit(1);
			}
			did_vsprintf = 1;
		}

		switch (chan->type) {
		case log_syslog:
			if (level >= log_critical)
				pri = (level >= 0) ? 0 : -level;
			else
				pri = -log_critical;
			syslog(chan->out.facility|syslog_priority[pri],
			       "%s%s%s%s",
			       (chan->flags & LOG_TIMESTAMP) ?	time_buf : "",
			       (chan->flags & LOG_PRINT_CATEGORY) ?
			       category_name : "",
			       (chan->flags & LOG_PRINT_LEVEL) ?
			       level_str : "",
			       lc->buffer);
			break;
		case log_file:
			stream = chan->out.file.stream;
			if (stream == NULL) {
				stream = log_open_stream(chan);
				if (stream == NULL)
					break;
			}
			if (chan->out.file.max_size != ULONG_MAX) {
				long pos;
				
				pos = ftell(stream);
				if (pos >= 0 &&
				    (unsigned long)pos >
				    chan->out.file.max_size) {
					/*
					 * try to roll over the log files,
					 * ignoring all all return codes
					 * except the open (we don't want
					 * to write any more anyway)
					 */
					log_close_stream(chan);
					version_rename(chan);
					stream = log_open_stream(chan);
					if (stream == NULL)
						break;
				}
			}
			fprintf(stream, "%s%s%s%s\n", 
				(chan->flags & LOG_TIMESTAMP) ?	time_buf : "",
				(chan->flags & LOG_PRINT_CATEGORY) ?
				category_name : "",
				(chan->flags & LOG_PRINT_LEVEL) ?
				level_str : "",
				lc->buffer);
			fflush(stream);
			break;
		case log_null:
			break;
		default:
			syslog(LOG_ERR,
			       "unknown channel type in log_vwrite()");
		}
	}
}