BOOL CLIENT::LogMessageV(PCHAR pszMsg, ...) { DWORD cbWritten = 0; CHAR szBuf[1024]; PCHAR pcchEnd = szBuf + ARRAYSIZE(szBuf) - 2; PCHAR pcchCur = szBuf; HRESULT hr; va_list args; va_start(args, pszMsg); hr = StringCchVPrintfExA(pcchCur, pcchEnd - pcchCur, &pcchCur, NULL, STRSAFE_NULL_ON_FAILURE, pszMsg, args); va_end(args); if (FAILED(hr)) { goto cleanup; } hr = StringCchPrintfExA(pcchCur, szBuf + (ARRAYSIZE(szBuf)) - pcchCur, &pcchCur, NULL, STRSAFE_NULL_ON_FAILURE, "\n"); cleanup: WriteFile(hFile, szBuf, (DWORD)(pcchCur - szBuf), &cbWritten, NULL); return TRUE; }
VOID SCALL LogFileFormatV(const CHAR *format, va_list argList) { DWORD errorCode; LOG_ENTRY *entry; size_t remaining; // Preserve system error code errorCode = GetLastError(); ASSERT(format != NULL); // Retrieve a spare entry structure (also checks log status) entry = GetSpareEntry(); if (entry != NULL) { GetLocalTime(&entry->time); StringCchVPrintfExA(entry->message, ELEMENT_COUNT(entry->message), NULL, &remaining, 0, format, argList); entry->length = ELEMENT_COUNT(entry->message) - remaining; // Insert log entry into the write queue QueueInsert(entry); } // Restore system error code SetLastError(errorCode); }
VOID SCALL LogFileTraceV(const CHAR *file, const CHAR *func, INT line, const CHAR *format, va_list argList) { #if 0 CHAR location[MAX_PATH]; #endif CHAR *messageEnd; DWORD errorCode; DWORD processId; DWORD threadId; LOG_ENTRY *entry; size_t remaining; // Preserve system error code errorCode = GetLastError(); ASSERT(file != NULL); ASSERT(func != NULL); ASSERT(format != NULL); processId = GetCurrentProcessId(); threadId = GetCurrentThreadId(); // Retrieve a spare entry structure (also checks log status) entry = GetSpareEntry(); if (entry != NULL) { // // Available trace information: // // file - Source file // func - Function name in the source file // line - Line number in the source file // processId - Current process ID // threadId - Current thread ID // GetLocalTime(&entry->time); #if 0 StringCchPrintfA(location, ELEMENT_COUNT(location), "%s:%d", LogFileName(file), line); StringCchPrintfExA(entry->message, ELEMENT_COUNT(entry->message), &messageEnd, &remaining, 0, "%-20s - %-20s - ", location, func); #else StringCchPrintfExA(entry->message, ELEMENT_COUNT(entry->message), &messageEnd, &remaining, 0, "%s - ", func); #endif StringCchVPrintfExA(messageEnd, remaining, NULL, &remaining, 0, format, argList); entry->length = ELEMENT_COUNT(entry->message) - remaining; // Insert log entry into the write queue QueueInsert(entry); } // Restore system error code SetLastError(errorCode); }
template <class Tracer> inline void Trace(const char* pszFormat, ...) { ASSERT_ISNOTNULL(pszFormat); va_list args; va_start(args, pszFormat); char szBuffer[512]; StringCchVPrintfExA(szBuffer, 512, NULL, NULL, STRSAFE_NULL_ON_FAILURE, pszFormat, args); Tracer::Output(szBuffer); va_end(args); }
HRESULT StringCchPrintfExA( LPSTR pszDest, size_t cchDest, LPSTR * ppszDestEnd, size_t * pcchRemaining, DWORD dwFlags, LPCSTR pszFormat, ...) { va_list argList; HRESULT result; va_start(argList, pszFormat); result = StringCchVPrintfExA(pszDest, cchDest, ppszDestEnd, pcchRemaining, dwFlags, pszFormat, argList); va_end(argList); return result; }
void operator()(const char* pszFormat, ...) { ASSERT_ISNOTNULL(pszFormat); char szFormatBuffer[512] = { 0 }; StringCchPrintfExA(szFormatBuffer, 512, NULL, NULL, STRSAFE_NULL_ON_FAILURE, "%s (%u) : %s", m_pszFile, m_uLine, pszFormat); va_list args; va_start(args, pszFormat); char szBuffer[4096] = { 0 }; StringCchVPrintfExA(szBuffer, 4096, NULL, NULL, STRSAFE_NULL_ON_FAILURE, szFormatBuffer, args); Tracer::Output(szBuffer); va_end(args); }