首页 > 代码库 > windows log 打印语句

windows log 打印语句

1、格式化字符串(Writes formatted data to the specified string)

  wchar_t szMessage[260];

  PWSTR pszFunction = L“Hello world !”;

  DWORD dwError = GetLastError();

  StringCchPrintfW(szMessage, ARRAYSIZE(szMessage),L"%s failed w/err 0x%08lx", pszFunction, dwError);

 

  StringCchPrintf is a replacement for the following functions:

  • sprintf, swprintf, _stprintf
  • wsprintf
  • wnsprintf
  • _snprintf, _snwprintf, _sntprintf

2、ReportEventW ,适用于服务应用,适用于在事件查看器中查看相关log  

 1 #ifdef _DEBUG 2  3 HANDLE hEventSource = NULL; 4 LPCWSTR lpszStrings[2] = { NULL, NULL }; 5  6 hEventSource = RegisterEventSourceW(NULL, L"DeviceMonitorService"); 7 if (hEventSource) 8 { 9 lpszStrings[0] = L"Source";//来源10 lpszStrings[1] = pszMessage;11 12 ReportEventW(hEventSource, // Event log handle13 wType, // Event type14 0, // Event category15 0, // Event identifier16 NULL, // No security identifier17 2, // Size of lpszStrings array18 0, // No binary data19 lpszStrings, // Array of strings20 NULL // No binary data21 );22 23 DeregisterEventSource(hEventSource);24 }25 #else26 #endif // DEBUG

3、OutputDebugStringA,适用于win32应用程序(非控制台程序)

void logoutput(const char * lpszFormat, ...){    //#ifdef _DEBUG    va_list argList;    va_start(argList, lpszFormat);    char chInput[512] = { 0 };    vsprintf_s(chInput, lpszFormat, argList);    va_end(argList);    OutputDebugStringA(chInput);    OutputDebugStringA("\n");    //#endif}

4、字符串操作(strsafe function)

The string functions give applications the means to copy, compare, sort, format, and convert character strings as well as the means to determine the character type of each character in a string.

使用安全字符:

  #include Strsafe.h 

   StringCchCopy

 

CRT String FunctionWindows String FunctionStrSafe Function
strcatlstrcat
StringCchCat
StringCchCatEx
StringCbCat
StringCbCatEx
strcmplstrcmp(no equivalent function)
strcpylstrcpy
StringCchCopy
StringCchCopyEx
StringCbCopy
StringCbCopyEx
strlenlstrlen
StringCchLength
StringCbLength

 

来源于http://msdn.microsoft.com/en-us/library/windows/desktop/ms647465(v=vs.85).aspx

 

windows log 打印语句