static void iwcmd_vprint_utf8(struct params_struct *p, const char *fmt, va_list ap) { char buf[500]; TCHAR buf2[500]; #ifdef IW_WINDOWS StringCbVPrintfA(buf,sizeof(buf),fmt,ap); #if _UNICODE iwcmd_utf8_to_tchar(buf,buf2,sizeof(buf2)/sizeof(TCHAR)); #else iw_utf8_to_ascii(buf,buf2,sizeof(buf2)/sizeof(TCHAR)); #endif _tprintf(_T("%s"),buf2); #else if(p->unicode_output) { vfprintf(stdout,fmt,ap); } else { vsnprintf(buf,sizeof(buf),fmt,ap); buf[499]='\0'; iw_utf8_to_ascii(buf,buf2,sizeof(buf2)); fputs(buf2,stdout); } #endif }
//////////////////////////////////////////////////////////////////////////////////// // // Formats Resource. // static DWORD FormatResource(_In_ LPSTR pszResource, _Out_ LPSTR *ppszBuffer, ...) { DWORD dwSize = (DWORD)strlen(pszResource) + MAX_PATH; DWORD dwLoop = 0; va_list vaList; HRESULT hResult = E_FAIL; va_start(vaList, ppszBuffer); // *ppszBuffer should be NULL when passed in. *ppszBuffer = NULL; // Allocate and format the string. do { if(NULL != *ppszBuffer) { delete[] *ppszBuffer; } *ppszBuffer = new CHAR[dwSize]; if(NULL == *ppszBuffer) { goto Cleanup; } hResult = StringCbVPrintfA(*ppszBuffer, dwSize, pszResource, vaList); if(STRSAFE_E_INSUFFICIENT_BUFFER == hResult) { dwSize *= 2; } } while ( FAILED(hResult) && (dwLoop++ < MAX_LOOP)); Cleanup: // Check to see if we hit error. if(FAILED(hResult)) { if(NULL != *ppszBuffer) { delete[] *ppszBuffer; *ppszBuffer = NULL; } } va_end(vaList); if(*ppszBuffer) { DWORD dwToReturn = 0; if(SUCCEEDED(SIZETToDWord(strlen(*ppszBuffer), &dwToReturn))) { return dwToReturn; } } return 0; }
VOID Trace( _In_ ULONG DebugPrintLevel, _In_ PCSTR DebugMessage, ... ) /*++ Routine Description: Debug print for the sample driver. Arguments: TraceEventsLevel - print level between 0 and 3, with 3 the most verbose Return Value: None. --*/ { #if DBG #define TEMP_BUFFER_SIZE 1024 va_list list; CHAR debugMessageBuffer[TEMP_BUFFER_SIZE]; HRESULT hr; UNREFERENCED_PARAMETER(DebugPrintLevel); va_start(list, DebugMessage); if (DebugMessage) { // // Using new safe string functions instead of _vsnprintf. // This function takes care of NULL terminating if the message // is longer than the buffer. // hr = StringCbVPrintfA( debugMessageBuffer, sizeof(debugMessageBuffer), DebugMessage, list ); if(!SUCCEEDED(hr)) { OutputDebugStringA("Unable to expand: "); OutputDebugStringA(DebugMessage); } else { OutputDebugStringA(_DRIVER_NAME_); OutputDebugStringA(debugMessageBuffer); } } va_end(list); #else UNREFERENCED_PARAMETER(DebugPrintLevel); UNREFERENCED_PARAMETER(DebugMessage); #endif }
const char* _Format( const char* String, ... ) { static char Buffer[ 512 ] = { 0 }; va_list Args = { 0 }; va_start( Args, String ); StringCbVPrintfA( Buffer, 512, String, Args ); return Buffer; };
void GL::Utils::log(LPCSTR format, ...) { CHAR buf[200]; va_list ap; va_start(ap, format); (void)StringCbVPrintfA(buf, sizeof(buf), format, ap); va_end(ap); OutputDebugStringA(buf); }
void DebugPrintf(const char* lpFormat, ...) { CHAR buf[1024]; va_list va; va_start(va, lpFormat); StringCbVPrintfA(buf, sizeof(buf), lpFormat, va); _pout(buf); }
void DebugPrintf(LPCSTR lpFormat, ...) { CHAR buf[1024]; va_list va; va_start(va, lpFormat); StringCbVPrintfA(buf, sizeof(buf), lpFormat, va); OutputDebugStringA(buf); }
void service_trace(const char *msg, ...) { va_list valist; char buf[512]; va_start(valist, msg); StringCbVPrintfA(buf, sizeof(buf), msg, valist); va_end(valist); send_msg("TRACE", buf); }
void service_ok(int cnd, const char *msg, ...) { va_list valist; char buf[512]; va_start(valist, msg); StringCbVPrintfA(buf, sizeof(buf), msg, valist); va_end(valist); send_msg(cnd ? "OK" : "FAIL", buf); }
/*---------------------------------------------------------------------------------------------*/ void axlog(unsigned int log_type, const char *format, ...) { axtrace_contex_s* ctx; va_list ptr = 0; HRESULT hr; size_t contents_byte_size, final_length; int send_len; /* buf for send , call send() once*/ char buf[sizeof(axtrace_log_s) + AXTRACE_MAX_TRACE_STRING_LENGTH] = { 0 }; axtrace_log_s* trace_head = (axtrace_log_s*)(buf); char* trace_string = (char*)(buf + sizeof(axtrace_log_s)); /* is init ok? */ ctx = _axtrace_get_thread_contex(0, 0); if (ctx == 0) return; /* Create String Contents*/ va_start(ptr, format); hr = StringCbVPrintfA(trace_string, AXTRACE_MAX_TRACE_STRING_LENGTH, format, ptr); va_end(ptr); /* failed ?*/ if (FAILED(hr)) return; /** get string length*/ hr = StringCbLengthA(trace_string, AXTRACE_MAX_TRACE_STRING_LENGTH - 1, &contents_byte_size); /* failed ?*/ if (FAILED(hr)) return; /* add '\0' ended */ contents_byte_size += 1; /* fill the trace head data */ final_length = sizeof(axtrace_log_s)+contents_byte_size; trace_head->head.length = (unsigned short)(final_length); trace_head->head.flag = 'A'; trace_head->head.type = AXTRACE_CMD_TYPE_LOG; trace_head->head.pid = GetCurrentProcessId(); trace_head->head.tid = GetCurrentThreadId(); trace_head->log_type = log_type; trace_head->code_page = ATC_ACP; trace_head->length = (unsigned short)contents_byte_size; /* send to axtrace server*/ send_len = send(ctx->sfd, buf, (int)final_length, MSG_DONTROUTE); /*TODO: check result, may be reconnect to server */ return; }
HRESULT StringCbPrintfA( LPSTR pszDest, size_t cbDest, LPCSTR pszFormat, ...) { va_list argList; HRESULT result; va_start(argList, pszFormat); result = StringCbVPrintfA(pszDest, cbDest, pszFormat, argList); va_end(argList); return result; }
void _TRACE(LPCSTR pszFormat, ...) { CHAR szBuffer[1024]; va_list ap; va_start(ap, pszFormat); StringCbVPrintfA(szBuffer, sizeof(szBuffer), pszFormat, ap); va_end(ap); // _OutputCounter(); OutputDebugStringA(szBuffer); }
void osi_LogEvent(char *a,char *b,char *c,...) { HANDLE h; char *ptbuf[1],buf[MAXBUF_+1]; va_list marker; if (!ISCLIENTTRACE(osi_TraceOption)) return; h = RegisterEventSource(NULL, AFS_DAEMON_EVENT_NAME); va_start(marker,c); StringCbVPrintfA(buf,MAXBUF_,c,marker); ptbuf[0] = buf; ReportEvent(h, EVENTLOG_INFORMATION_TYPE, 0, 0, NULL, 1, 0, (const char **)ptbuf, NULL);\ DeregisterEventSource(h); va_end(marker); }
/*++ Routine Name: RealDebugMessage Routine Description: This routine takes a debug message and va_list and outputs the message via OutputDebugString Arguments: dwSize - Maximum size of the debug message in number of characters pszMessage - The debug message string arglist - The arg list for the debug message string Return Value: BOOL TRUE - On success FALSE - On error --*/ BOOL RealDebugMessage( __in DWORD dwSize, __in PCSTR pszMessage, va_list arglist ) { HRESULT hr = S_OK; PSTR pszMsgBuf; if (NULL == pszMessage || 0 == dwSize) { hr = E_INVALIDARG; } if (SUCCEEDED(hr)) { // // Allocate memory for message buffer. // pszMsgBuf = new CHAR[dwSize + 1]; if (NULL != pszMsgBuf) { // // Pass the variable parameters to wvsprintf to be formated. // hr = StringCbVPrintfA(pszMsgBuf, (dwSize + 1) * sizeof(CHAR), pszMessage, arglist); // // Dump string to debug output. // OutputDebugStringA(pszMsgBuf); // // Clean up. // delete[] pszMsgBuf; pszMsgBuf = NULL; } else { hr = E_OUTOFMEMORY; } } return SUCCEEDED(hr); }
void print(_In_ const char* fmt, _In_ ...) { char log_buffer[2048]; va_list args; va_start(args, fmt); HRESULT hRes = StringCbVPrintfA(log_buffer, sizeof(log_buffer), fmt, args); if (S_OK != hRes) { fprintf(stderr, "%s, StringCbVPrintfA() failed. res = 0x%08x", __FUNCTION__, hRes); return; } OutputDebugStringA(log_buffer); fprintf(stdout, "%s \n", log_buffer); }
static BOOL DebugMessageV(LPCSTR lpszMessage, va_list arglist) { DWORD dwSize = DEBUG_BUFFER_SIZE; DWORD dwLoop = 0; LPSTR lpszMsgBuf = NULL; HRESULT hr; // Parameter checking. if( (NULL == lpszMessage) || (0 == dwSize) ) { return FALSE; } do { // Allocate memory for message buffer. if(NULL != lpszMsgBuf) { delete[] lpszMsgBuf; dwSize *= 2; } lpszMsgBuf = new CHAR[dwSize + 1]; if(NULL == lpszMsgBuf) { return FALSE; } hr = StringCbVPrintfA(lpszMsgBuf, (dwSize + 1) * sizeof(CHAR), lpszMessage, arglist); // Pass the variable parameters to wvsprintf to be formated. } while (FAILED(hr) && (STRSAFE_E_INSUFFICIENT_BUFFER == hr) && (++dwLoop < MAX_LOOP) ); // Dump string to Debug output. OutputDebugStringA(lpszMsgBuf); // Cleanup. delete[] lpszMsgBuf; return SUCCEEDED(hr); }
NTSTATUS Log_Print(LOG_LEVEL Level, LPCSTR pszFormat, ...) { NTSTATUS Status = STATUS_SUCCESS; LARGE_INTEGER SystemTime, LocalTime; TIME_FIELDS TimeFields; PLOG_COUNTED_STRING Line = NULL; va_list args; KIRQL Irql = KeGetCurrentIrql(); LPCSTR StrLevel = " :"; if (!LogFile) return STATUS_INVALID_DEVICE_STATE; if (Irql > DISPATCH_LEVEL) return STATUS_INVALID_LEVEL; KeQuerySystemTime(&SystemTime); ExSystemTimeToLocalTime(&SystemTime, &LocalTime); RtlTimeToTimeFields(&LocalTime, &TimeFields); Line = ExAllocatePoolWithTag(NonPagedPool, MAX_LOG_STRING_SIZE + FIELD_OFFSET(LOG_COUNTED_STRING, Data), LogAllocationTag); if (!Line) return STATUS_INSUFFICIENT_RESOURCES; switch (Level) { case LL_FATAL: #if DBG DbgBreakPointWithStatus(DBG_STATUS_FATAL); #endif StrLevel = "FATAL :"; break; case LL_ERROR: StrLevel = "ERROR :"; break; case LL_WARNING: StrLevel = "WARNING:"; break; case LL_INFO: StrLevel = "INFO :"; break; case LL_VERBOSE: StrLevel = "VERBOSE:"; break; case LL_DEBUG: StrLevel = "DEBUG :"; break; } Status = StringCbPrintfA(Line->Data, MAX_LOG_STRING_SIZE, "%04u.%02u.%02u %02u:%02u:%02u.%03u PR:0x%04X TH:0x%04X IL:%d %s ", TimeFields.Year, TimeFields.Month, TimeFields.Day, TimeFields.Hour, TimeFields.Minute, TimeFields.Second, TimeFields.Milliseconds, (ULONG)PsGetCurrentProcessId() & 0xFFFF, (ULONG)PsGetCurrentThreadId() & 0xFFFF, (ULONG)Irql, StrLevel); if (SUCCEEDED(Status)) { va_start(args, pszFormat); Line->DataLength = (USHORT)strlen(Line->Data); Status = StringCbVPrintfA(Line->Data + Line->DataLength, MAX_LOG_STRING_SIZE - Line->DataLength, pszFormat, args); if (SUCCEEDED(Status)) { Line->DataLength = (USHORT)strlen(Line->Data); if (Irql != PASSIVE_LEVEL) { PIO_WORKITEM pWorkItem = IoAllocateWorkItem(LogDeviceObject); InterlockedIncrement(&LogScheduledPrints); IoQueueWorkItemEx(pWorkItem, Log_WriteWorker, DelayedWorkQueue, Line); Status = STATUS_PENDING; } else { Status = Log_WriteLine(Line); } } va_end(args); } if (Status != STATUS_PENDING) ExFreePoolWithTag(Line, LogAllocationTag); return Status; }
inline virtual void WriteTextV( __in WORD Attributes, __in const char *fmt, __in va_list argptr ) /*++ Routine Description: This routine displays text to the log file and the debug console. The console output may have color attributes supplied, as per the standard SetConsoleTextAttribute API. Additionally, if a log file has been opened, a timestamped log entry is written to disk. Arguments: Attributes - Supplies color attributes for the text as per the standard SetConsoleTextAttribute API (e.g. FOREGROUND_RED). fmt - Supplies the printf-style format string to use to display text. argptr - Supplies format inserts. Return Value: None. Environment: User mode. --*/ { UNREFERENCED_PARAMETER( Attributes ); char buf[8193]; StringCbVPrintfA(buf, sizeof( buf ), fmt, argptr); if (g_Log != NULL) { time_t t; struct tm * tm; time( &t ); if ((tm = gmtime( &t )) != NULL) { fprintf( g_Log, "[%04lu-%02lu-%02lu %02lu:%02lu:%02lu] ", tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec); } vfprintf( g_Log, fmt, argptr ); fflush( g_Log ); } }