ULONG __cdecl DbgPrintEntry( __in PCHAR Format, ... ) { PDBG_OUTPUTA Filter; PBTR_FILTER_RECORD Record; BTR_PROBE_CONTEXT Context; DBGPRINT Routine; size_t Length; ULONG UserLength; va_list arg; PULONG_PTR Sp; ULONG Result; char format[512]; char buffer[512]; BtrFltGetContext(&Context); Routine = Context.Destine; // // N.B. Maximum support 16 arguments // Sp = (PULONG_PTR)&Format + 1; Result = (*Routine)(Format, Sp[0], Sp[1], Sp[2], Sp[3], Sp[4], Sp[5], Sp[6], Sp[7], Sp[8], Sp[9], Sp[10], Sp[11], Sp[12], Sp[13], Sp[14]); BtrFltSetContext(&Context); __try { va_start(arg, Format); StringCchVPrintfA(format, 512, Format, arg); StringCchPrintfA(buffer, 512, "%s", format); Length = strlen(buffer) + 1; va_end(arg); UserLength = FIELD_OFFSET(DBG_OUTPUTA, Text[Length]); Record = BtrFltAllocateRecord(UserLength, DbgUuid, _DbgPrint); if (!Record) { return Result; } Filter = FILTER_RECORD_POINTER(Record, DBG_OUTPUTA); Filter->Length = Length; StringCchCopyA(Filter->Text, Length, buffer); BtrFltQueueRecord(Record); } __except (EXCEPTION_EXECUTE_HANDLER) { if (Record) { BtrFltFreeRecord(Record); } } return Result; }
void DbgPrintf(A_INT32 mask, A_CHAR * format, ...) { va_list argList; /* holds the arguement list passed in */ A_CHAR buffer[1024]; wchar_t wBuf[1024]; if (mask & printMask) { /* get the argument list */ va_start (argList, format); /* * using vprintf to perform the printing it is the same is printf, only * it takes a va_list or arguements */ if (S_OK != StringCchVPrintfA (buffer, sizeof (buffer), format, argList)) { return; } MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, buffer, -1, wBuf, 256 ); OutputDebugString (wBuf); } return; }
__inline void TraceA(const char *format, ...) { if(g_bDebug) { if (format) { va_list arglist; char str[4096]; va_start(arglist, format); StringCchVPrintfA(str, sizeof(str), format, arglist); StringCbCatA(str, sizeof(str), "\n"); if (g_fLogFile) { FILE *fout = fopen(g_fLogFile, "a+t"); if (fout) { fprintf(fout, str); fclose(fout); } } printf(str); if (g_bDebugString) { OutputDebugStringA(str); } va_end(arglist); } } }
void logPrintf(A_INT32 mask, A_CHAR * format, ...) { va_list argList; /* holds the arguement list passed in */ A_CHAR buffer[1024]; FILE *fStream = NULL; /* get the argument list */ va_start(argList, format); /* * using vprintf to perform the printing it is the same is printf, only * it takes a va_list or arguements */ if (S_OK != StringCchVPrintfA (buffer, sizeof (buffer), format, argList)) { return; } fStream = fopen("\\Windows\\ar6khost.log", "a+"); if (fStream) { fprintf(fStream,"%i: %s\n",GetTickCount(),buffer); fclose(fStream); } return; }
// // warning -- this function is implemented twice for ansi applications // linking to the unicode library // void WINAPI DbgLogInfo(DWORD Type,DWORD Level,__format_string LPCSTR pFormat,...) { /* Check the current level for this type combination */ BOOL bAccept = DbgCheckModuleLevel(Type,Level); if (bAccept == FALSE) { return; } TCHAR szInfo[2000]; /* Format the variable length parameter list */ va_list va; va_start(va, pFormat); (void)StringCchPrintf(szInfo, NUMELMS(szInfo), TEXT("%s(tid %x) %8d : "), m_ModuleName, GetCurrentThreadId(), timeGetTime() - dwTimeOffset); CHAR szInfoA[2000]; WideCharToMultiByte(CP_ACP, 0, szInfo, -1, szInfoA, NUMELMS(szInfoA), 0, 0); (void)StringCchVPrintfA(szInfoA + lstrlenA(szInfoA), NUMELMS(szInfoA) - lstrlenA(szInfoA), pFormat, va); (void)StringCchCatA(szInfoA, NUMELMS(szInfoA), "\r\n"); WCHAR wszOutString[2000]; MultiByteToWideChar(CP_ACP, 0, szInfoA, -1, wszOutString, NUMELMS(wszOutString)); DbgOutString(wszOutString); va_end(va); }
BOOL WINAPIV LSLogPrintf(int nLevel, LPCSTR pszModule, LPCSTR pszFormat, ...) { #if defined(LS_COMPAT_LOGGING) if (!pszModule || !pszFormat) { return FALSE; } BOOL bReturn = FALSE; char szMessage[MAX_LINE_LENGTH]; va_list argList; va_start(argList, pszFormat); if (SUCCEEDED(StringCchVPrintfA(szMessage, MAX_LINE_LENGTH, pszFormat, argList))) { bReturn = LSLog(nLevel, pszModule, szMessage); } va_end(argList); return bReturn; #else return TRUE; #endif // LS_COMPAT_LOGGING }
VOID MspWriteLogEntry( IN ULONG ProcessId, IN MSP_LOG_LEVEL Level, IN PSTR Format, ... ) { ULONG IsLogging; va_list Argument; PMSP_LOG_ENTRY Entry; CHAR Buffer[MSP_LOG_TEXT_LIMIT]; IsLogging = InterlockedCompareExchange(&MspLogObject.Flag, 0, 0); if (!IsLogging) { return; } va_start(Argument, Format); StringCchVPrintfA(Buffer, MAX_PATH, Format, Argument); va_end(Argument); Entry = (PMSP_LOG_ENTRY)MspMalloc(sizeof(MSP_LOG_ENTRY)); Entry->ProcessId = ProcessId; GetLocalTime(&Entry->TimeStamp); StringCchCopyA(Entry->Text, MSP_LOG_TEXT_LIMIT, Buffer); EnterCriticalSection(&MspLogObject.Lock); InsertTailList(&MspLogObject.ListHead, &Entry->ListEntry); MspLogObject.ListDepth += 1; LeaveCriticalSection(&MspLogObject.Lock); return; }
/*++ TraceFormat Sends the message to a debugger. Arguments: funct - Pointer to a null-terminated string that specifies the function. format - Pointer to a null-terminated printf-style format string. ... - Arguments to insert into "format". Return Values: None. --*/ VOID CCALL TraceFormat(const char *funct, const char *format, ...) { char *end; char output[1024]; DWORD error; size_t remaining; va_list argList; ASSERT(funct != NULL); ASSERT(format != NULL); // Preserve system error code error = GetLastError(); StringCchPrintfExA(output, ELEMENT_COUNT(output), &end, &remaining, 0, "| %4d | %17s | ", GetCurrentThreadId(), funct); va_start(argList, format); StringCchVPrintfA(end, remaining, format, argList); va_end(argList); OutputDebugStringA(output); // Restore system error code SetLastError(error); }
/******************************************************************** StrAnsiAllocFormattedArgs - allocates or reuses dynamic ANSI string memory and formats it with the passed in args NOTE: caller is responsible for freeing ppsz even if function fails ********************************************************************/ extern "C" HRESULT DAPI StrAnsiAllocFormattedArgs( __inout LPSTR* ppsz, __in LPCSTR szFormat, __in va_list args ) { Assert(ppsz && szFormat && *szFormat); HRESULT hr = S_OK; DWORD_PTR cch = *ppsz ? MemSize(*ppsz) / sizeof(CHAR) : 0; LPSTR pszOriginal = NULL; DWORD cchOriginal = 0; if (*ppsz) { cch = MemSize(*ppsz); // get the count in bytes so we can check if it failed (returns -1) if (-1 == cch) ExitOnFailure(hr = E_INVALIDARG, "failed to get size of destination string"); cch /= sizeof(CHAR); //convert the count in bytes to count in characters cchOriginal = lstrlenA(*ppsz); } if (0 == cch) // if there is no space in the string buffer { cch = 256; hr = StrAnsiAlloc(ppsz, cch); ExitOnFailure1(hr, "failed to allocate string to format: %s", szFormat); } // format the message (grow until it fits or there is a failure) do { hr = StringCchVPrintfA(*ppsz, cch, szFormat, args); if (STRSAFE_E_INSUFFICIENT_BUFFER == hr) { if (!pszOriginal) { // this allows you to pass the original string as a formatting argument and not crash // save the original string and free it after the printf is complete pszOriginal = *ppsz; *ppsz = NULL; // StringCchVPrintfW starts writing to the string... // NOTE: this hack only works with sprintf(&pwz, "%s ...", pwz, ...); pszOriginal[cchOriginal] = 0; } cch *= 2; hr = StrAnsiAlloc(ppsz, cch); ExitOnFailure1(hr, "failed to allocate string to format: %S", szFormat); hr = S_FALSE; } } while (S_FALSE == hr); ExitOnFailure(hr, "failed to format string"); LExit: ReleaseStr((void*) pszOriginal); return hr; }
/******************************************************************* Dutil_Trace *******************************************************************/ extern "C" void DAPI Dutil_Trace( __in LPCSTR szFile, __in int iLine, __in REPORT_LEVEL rl, __in LPCSTR szFormat, ... ) { AssertSz(REPORT_NONE != rl, "REPORT_NONE is not a valid tracing level"); HRESULT hr = S_OK; char szOutput[DUTIL_STRING_BUFFER]; char szMsg[DUTIL_STRING_BUFFER]; if (Dutil_rlCurrentTrace < rl) return; va_list args; va_start(args, szFormat); hr = StringCchVPrintfA(szOutput, countof(szOutput), szFormat, args); va_end(args); if (SUCCEEDED(hr)) { LPCSTR szPrefix = "Trace/u"; char szMsg[DUTIL_STRING_BUFFER]; switch (rl) { case REPORT_STANDARD: szPrefix = "Trace/s"; break; case REPORT_VERBOSE: szPrefix = "Trace/v"; break; case REPORT_DEBUG: szPrefix = "Trace/d"; break; } if (Dutil_fTraceFilenames) hr = StringCchPrintfA(szMsg, countof(szMsg), "%s [%s,%d]: %s\r\n", szPrefix, szFile, iLine, szOutput); else hr = StringCchPrintfA(szMsg, countof(szMsg), "%s: %s\r\n", szPrefix, szOutput); if (SUCCEEDED(hr)) OutputDebugStringA(szMsg); // else fall through to the case below } if (FAILED(hr)) { if (Dutil_fTraceFilenames) StringCchPrintfA(szMsg, countof(szMsg), "Trace [%s,%d]: message too long, skipping\r\n", szFile, iLine); else StringCchPrintfA(szMsg, countof(szMsg), "Trace: message too long, skipping\r\n"); OutputDebugStringA(szMsg); } }
void iw_vsnprintf(char *buf, size_t buflen, const char *fmt, va_list ap) { #ifdef IW_WINDOWS StringCchVPrintfA(buf,buflen,fmt,ap); #else vsnprintf(buf,buflen,fmt,ap); buf[buflen-1]='\0'; #endif }
void __cdecl DebugPrintA(__in_z __format_string LPCSTR lpcszFormat, ...) { va_list pArguments; char szDebugBuffer[1024]; va_start(pArguments, lpcszFormat); if (SUCCEEDED(StringCchVPrintfA(szDebugBuffer, _countof(szDebugBuffer), lpcszFormat, pArguments))) OutputDebugStringA(szDebugBuffer); va_end(pArguments); }
/******************************************************************** WcaLog() - outputs trace and log info *******************************************************************/ extern "C" void __cdecl WcaLog( __in LOGLEVEL llv, __in_z __format_string PCSTR fmt, ... ) { static char szFmt[LOG_BUFFER]; static char szBuf[LOG_BUFFER]; static bool fInLogPrint = false; // prevent re-entrant logprints. (recursion issues between assert/logging code) if (fInLogPrint) return; fInLogPrint = true; if (LOGMSG_STANDARD == llv || (LOGMSG_VERBOSE == llv && IsVerboseLoggingLite()) #ifdef DEBUG || LOGMSG_TRACEONLY == llv #endif ) { va_list args; va_start(args, fmt); LPCSTR szLogName = WcaGetLogName(); if (szLogName[0] != 0) StringCchPrintfA(szFmt, countof(szFmt), "%s: %s", szLogName, fmt); else StringCchCopyA(szFmt, countof(szFmt), fmt); StringCchVPrintfA(szBuf, countof(szBuf), szFmt, args); va_end(args); #ifdef DEBUG // always write to the log in debug #else if (llv == LOGMSG_STANDARD || (llv == LOGMSG_VERBOSE && IsVerboseLoggingLite())) #endif { PMSIHANDLE hrec = MsiCreateRecord(1); ::MsiRecordSetStringA(hrec, 0, szBuf); // TODO: Recursion on failure. May not be safe to assert from here. WcaProcessMessage(INSTALLMESSAGE_INFO, hrec); } #if DEBUG StringCchCatA(szBuf, countof(szBuf), "\n"); OutputDebugStringA(szBuf); #endif } fInLogPrint = false; return; }
static int debug_outputlnA_impl(const char * formatString, va_list args) { HRESULT hr; hr = StringCchVPrintfA(g_outputBuffer, OUTPUT_BUFFER_LEN, formatString, args); if (SUCCEEDED(hr) || hr == STRSAFE_E_INSUFFICIENT_BUFFER) { OutputDebugStringA(g_outputBuffer); } va_end(args); return SUCCEEDED(hr); }
void DebugMsg(const CHAR* pszFormat, ...) { CHAR buf[1024]; StringCchPrintfA(buf, sizeof(buf)/sizeof(CHAR), "(%lu): ", GetCurrentThreadId()); va_list arglist; va_start(arglist, pszFormat); StringCchVPrintfA(&buf[strlen(buf)], sizeof(buf)/sizeof(CHAR), pszFormat, arglist); va_end(arglist); StringCchCatA(buf, sizeof(buf)/sizeof(CHAR), "\n"); OutputDebugStringA(buf); }
void Trace(LPSTR format, ...) { char message[256]; va_list args = NULL; va_start(args, format); StringCchVPrintfA(message, 256, format, args); char buffer[256]; StringCchCopyA(buffer, 256, "ode: "); StringCchCatA(buffer, 256, message); OutputDebugStringA(buffer); }
static BOOL DebugMessageV(LPCSTR lpszMessage, va_list arglist) { DWORD dwSize = DEBUG_BUFFER_SIZE; LPSTR lpszMsgBuf = NULL; HRESULT hr = S_FALSE; // Parameter checking. if( (NULL == lpszMessage) || (0 == dwSize) ) { return FALSE; } do { // Allocate memory for message buffer. if(NULL != lpszMsgBuf) { delete[] lpszMsgBuf; dwSize *= 2; } // // Just to make sure we dont go into infinite loop and allocate // lots of memory, lets bail out if memory requirement becomes huge. // Multiplying by 8, i.e. will loop at most 4 times. // if ( dwSize > 8*DEBUG_BUFFER_SIZE ) { return FALSE; } lpszMsgBuf = new CHAR[dwSize + 1]; if(NULL == lpszMsgBuf) return FALSE; hr = StringCchVPrintfA ( lpszMsgBuf, dwSize, lpszMessage, arglist); // Pass the variable parameters to wvsprintf to be formated. } while (FAILED (hr) ); // Dump string to Debug output. OutputDebugStringA(lpszMsgBuf); // Cleanup. delete[] lpszMsgBuf; return TRUE; }
//------------------------------------------------------------------------------ // Function: LuaError // // Description: // // Raise a formatted error message. // // Returns: // // Notes: // // This function allows more elaborate format specifiers than luaL_error. // int LuaError( _In_ lua_State* L, _In_z_ const char* fmt, ...) { char buf[1024]; va_list arg; va_start(arg, fmt); StringCchVPrintfA(STRING_AND_CCH(buf), fmt, arg); va_end(arg); return luaL_error(L, "%s", buf); }
void DebugPrint( const char *format, ... ) { char output[1024]; va_list argList; va_start(argList, format); StringCchVPrintfA(output, ARRAYSIZE(output), format, argList); OutputDebugStringA(output); va_end(argList); }
LSAPI void GetResStrEx(HINSTANCE hInstance, UINT uIDText, LPSTR pszText, size_t cchText, LPCSTR pszDefText, ...) { char szFormat[MAX_LINE_LENGTH]; va_list vargs; if (pszText != NULL && cchText > 0) { GetResStrA(hInstance, uIDText, szFormat, MAX_LINE_LENGTH, pszDefText); va_start(vargs, pszDefText); StringCchVPrintfA(pszText, cchText, szFormat, vargs); va_end(vargs); } }
HRESULT StringCchPrintfA( LPSTR pszDest, size_t cchDest, LPCSTR pszFormat, ...) { va_list argList; HRESULT result; va_start(argList, pszFormat); result = StringCchVPrintfA(pszDest, cchDest, pszFormat, argList); va_end(argList); return result; }
void AddLog(const char* lpFormat, ...) { const int nBufLen = MAX_PATH * 2; char szBuf[nBufLen + 1] = {0}; va_list ap; va_start(ap, lpFormat); StringCchVPrintfA(szBuf, nBufLen, lpFormat, ap); va_end(ap); OutputDebugStringA(szBuf); std::wstring sTime = GetTimeHeader(); std::string sLine = CW2A(sTime.c_str(), CP_UTF8).m_psz; sLine.append(szBuf); AddMsgToList(sLine); }
void DbgMsg( char* szMessage, ... ) { char szFullMessage[MAX_PATH]; char szFormatMessage[MAX_PATH]; // format message va_list ap; va_start(ap, szMessage); (void)StringCchVPrintfA( szFormatMessage, NUMELMS(szFormatMessage), szMessage, ap ); va_end(ap); (void)StringCchCatA( szFormatMessage, NUMELMS(szFormatMessage), "\n" ); (void)StringCchCopyA( szFullMessage, NUMELMS(szFullMessage), "~*~*~*~*~*~ " ); (void)StringCchCatA( szFullMessage, NUMELMS(szFullMessage), szFormatMessage ); OutputDebugStringA( szFullMessage ); }
static void safe_debugstr(int max_size, const char* lpformat, ...) { do { string l_out_debug; l_out_debug.resize(max_size + 1); va_list l_va_list; va_start(l_va_list, lpformat); StringCchVPrintfA((char*)l_out_debug.c_str(), max_size + 1, lpformat, l_va_list); va_end(l_va_list); l_out_debug.resize(strlen(l_out_debug.c_str())); out_msg_base::showmsg(l_out_debug); } while (false); }
VOID DXUTOutputDebugStringA(LPCSTR strMsg, ...) { #if defined(DEBUG) || defined(_DEBUG) CHAR strBuffer[512]; va_list args; va_start(args, strMsg); StringCchVPrintfA(strBuffer, 512, strMsg, args); strBuffer[511] = '\0'; va_end(args); OutputDebugStringA(strBuffer); #else UNREFERENCED_PARAMETER(strMsg); #endif }
void hippoDebugLogU(const char *format, ...) { char buf[1024]; va_list vap; va_start (vap, format); StringCchVPrintfA(buf, sizeof(buf) / sizeof(buf[0]) - 2, format, vap); va_end (vap); StringCchCatA(buf, sizeof(buf) / sizeof(buf[0]) - 2, "\r\n"); HippoBSTR strW; strW.setUTF8(buf); OutputDebugStringW(strW); }
/*++ Title: vsntprintf Routine Description: Formats a string and returns a heap allocated string with the formated data. This routine can be used to for extremely long format strings. Note: If a valid pointer is returned the callng functions must release the data with a call to delete. Arguments: psFmt - format string pArgs - pointer to a argument list. Return Value: Pointer to formated string. NULL if error. --*/ LPSTR vsntprintf( IN LPCSTR szFmt, IN va_list pArgs ) { LPSTR pszBuff; UINT uSize = 256; for( ; ; ) { pszBuff = (LPSTR)AllocSplMem(sizeof(char) * uSize); if (!pszBuff) { break; } // // Attempt to format the string. If format succeeds, get out // of the loop. If it fails, increase buffer size and continue. // (assuming failure is due to less buffer size). // if (SUCCEEDED ( StringCchVPrintfA(pszBuff, uSize, szFmt, pArgs) ) ) { break; } FreeSplMem(pszBuff); pszBuff = NULL; // // Double the buffer size after each failure. // uSize *= 2; // // If the size is greater than 100k exit without formatting a string. // if (uSize > 100*1024) { break; } } return pszBuff; }
int __cdecl vfprintfEntry( FILE *stream, const char *format, va_list argptr ) { PDBG_OUTPUTA Filter; PBTR_FILTER_RECORD Record; BTR_PROBE_CONTEXT Context; VFPRINTF Routine; size_t Length; ULONG UserLength; ULONG Result; char buffer[512]; BtrFltGetContext(&Context); Routine = Context.Destine; Result = (*Routine)(stream, format, argptr); BtrFltSetContext(&Context); __try { StringCchVPrintfA(buffer, 512, format, argptr); Length = strlen(buffer) + 1; UserLength = FIELD_OFFSET(DBG_OUTPUTA, Text[Length]); Record = BtrFltAllocateRecord(UserLength, DbgUuid, _vfprintf); if (!Record) { return Result; } Filter = FILTER_RECORD_POINTER(Record, DBG_OUTPUTA); Filter->Length = Length; StringCchCopyA(Filter->Text, Length, buffer); BtrFltQueueRecord(Record); } __except (EXCEPTION_EXECUTE_HANDLER) { if (Record) { BtrFltFreeRecord(Record); } } return Result; }
/******************************************************************** WcaLogError() - called before ExitOnXXX() macro exists the function NOTE: writes the hresult and error string to the MSI log ********************************************************************/ extern "C" void WcaLogError( __in HRESULT hr, __in LPCSTR szMessage, ... ) { char szBuffer[LOG_BUFFER]; va_list dots; va_start(dots, szMessage); StringCchVPrintfA(szBuffer, countof(szBuffer), szMessage, dots); va_end(dots); // log the message if using Wca common layer if (WcaIsInitialized()) WcaLog(LOGMSG_STANDARD, "Error 0x%x: %s", hr, szBuffer); }
VOID ApsWriteLogEntry( __in ULONG ProcessId, __in APS_LOG_LEVEL Level, __in PSTR Format, ... ) { ULONG IsLogging; va_list Argument; PAPS_LOG_ENTRY Entry; CHAR Buffer[APS_LOG_TEXT_LIMIT]; // // Check whether logging's enabled // IsLogging = InterlockedCompareExchange(&ApsLogObject.Flag, 0, 0); if (!IsLogging) { return; } va_start(Argument, Format); StringCchVPrintfA(Buffer, MAX_PATH, Format, Argument); va_end(Argument); Entry = (PAPS_LOG_ENTRY)ApsMalloc(sizeof(APS_LOG_ENTRY)); Entry->ProcessId = ProcessId; Entry->Level = Level; GetLocalTime(&Entry->TimeStamp); StringCchCopyA(Entry->Text, APS_LOG_TEXT_LIMIT, Buffer); // // Queue record entry to logging queue, note that this routine can be // called concurrently, so logging object's lock must be acquired to // protect the log record queue // EnterCriticalSection(&ApsLogObject.Lock); InsertTailList(&ApsLogObject.ListHead, &Entry->ListEntry); ApsLogObject.ListDepth += 1; LeaveCriticalSection(&ApsLogObject.Lock); return; }