Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

196 lines
4.8 KiB

  1. //=======================================================================
  2. //
  3. // Copyright (c) 2001 Microsoft Corporation. All Rights Reserved.
  4. //
  5. // File: debug.cpp
  6. //
  7. // Creator: PeterWi
  8. //
  9. // Purpose: debug functions
  10. //
  11. //=======================================================================
  12. #include "pch.h"
  13. #pragma hdrstop
  14. #ifdef DBG
  15. #define UNLEN 256
  16. #define TYPE_KEY TEXT("DebugType")
  17. #define LOGFILE 1
  18. #define DEBUGGEROUT 2
  19. void WriteLogFile(LPCSTR s);
  20. ///////////////////////////////////////////////////////////////////////////////
  21. //
  22. // Function : WuAUTrace
  23. // In : Variable number of arguments
  24. // Comments : If DEBUGGEROUT is defined, uses OutputDebugString to write
  25. // debug messages. If LOGFILEOUT is defined, uses WriteLogFile
  26. // to write to a file. The filename is founf in the registry
  27. //
  28. ///////////////////////////////////////////////////////////////////////////////
  29. void _cdecl WUAUTrace(char* pszFormat, ...)
  30. {
  31. USES_IU_CONVERSION;
  32. CHAR szBuf[1024+1];
  33. va_list ArgList;
  34. static DWORD dwType = 3;
  35. TCHAR szTimeString[80];
  36. SYSTEMTIME timeNow;
  37. CHAR szTemp[1040];
  38. LPSTR szTmp = NULL;
  39. if (! dwType || //on the second run this will be 0, 1 or 2
  40. NULL == pszFormat)
  41. {
  42. return;
  43. }
  44. va_start(ArgList, pszFormat);
  45. (void)StringCchVPrintfExA(szBuf, ARRAYSIZE(szBuf), NULL, NULL, MISTSAFE_STRING_FLAGS, pszFormat, ArgList);
  46. va_end(ArgList);
  47. if (dwType == 3) //first time
  48. {
  49. if ((FAILED(GetRegDWordValue(TYPE_KEY, &dwType))) || (!dwType))
  50. {
  51. dwType = 0;
  52. return; //no debug msg if no key or key==0
  53. }
  54. }
  55. GetLocalTime(&timeNow);
  56. if(SUCCEEDED(SystemTime2String(timeNow, szTimeString, ARRAYSIZE(szTimeString))))
  57. {
  58. szTmp = T2A(szTimeString);
  59. }
  60. (void)StringCchPrintfExA(
  61. szTemp,
  62. ARRAYSIZE(szTemp),
  63. NULL, NULL, MISTSAFE_STRING_FLAGS,
  64. "%lx %s : %s\r\n",
  65. GetCurrentThreadId(),
  66. NULL == szTmp ? "" : szTmp,
  67. szBuf);
  68. if (dwType==LOGFILE)
  69. {
  70. WriteLogFile(szTemp);
  71. }
  72. else
  73. {
  74. OutputDebugStringA(szTemp);
  75. }
  76. }
  77. ///////////////////////////////////////////////////////////////////////////////
  78. //
  79. // Function : CreateOrOpenDebugFile
  80. // Out : File Handle to open debug file. Must be closed by caller
  81. // Returns : TRUE for success, FALSE for failure
  82. // Comments : Creates a file "WinDir\wupd\username\wupdlog.txt"
  83. //
  84. ///////////////////////////////////////////////////////////////////////////////
  85. BOOL CreateOrOpenDebugFile(HANDLE& hFile)
  86. {
  87. TCHAR szDir[MAX_PATH+1], szUser[UNLEN+1];
  88. DWORD dwNameLen = ARRAYSIZE(szUser), dwErr;
  89. const TCHAR szWUPDDir[] = _T("wupd");
  90. const TCHAR szLogFileName[] = _T("wupdlog.txt");
  91. const TCHAR szWUDir[] = _T("C:\\Program Files\\WindowsUpdate");
  92. if (FAILED(PathCchCombine(
  93. szDir,
  94. ARRAYSIZE(szDir),
  95. _T('\0') == g_szWUDir[0] ? szWUDir : g_szWUDir,
  96. szWUPDDir)))
  97. {
  98. return FALSE;
  99. }
  100. if (! CreateDirectory(szDir, NULL))
  101. {
  102. dwErr = GetLastError();
  103. if ((dwErr != ERROR_ALREADY_EXISTS) && (dwErr != ERROR_FILE_EXISTS))
  104. {
  105. return FALSE;
  106. }
  107. }
  108. if (! GetUserName(szUser, &dwNameLen))
  109. {
  110. const TCHAR szDefault[] = _T("default");
  111. (void)StringCchCopyEx(szUser, ARRAYSIZE(szUser), szDefault, NULL, NULL, MISTSAFE_STRING_FLAGS);
  112. }
  113. if (FAILED(PathCchAppend(szDir, ARRAYSIZE(szDir), szUser)))
  114. {
  115. return FALSE;
  116. }
  117. if (! CreateDirectory(szDir, NULL))
  118. {
  119. dwErr = GetLastError();
  120. if ((dwErr != ERROR_ALREADY_EXISTS) && (dwErr != ERROR_FILE_EXISTS))
  121. {
  122. return FALSE;
  123. }
  124. }
  125. if (FAILED(PathCchAppend(szDir, ARRAYSIZE(szDir), szLogFileName)))
  126. {
  127. return FALSE;
  128. }
  129. // We now have directory "drive:program files\windowsupdate\username\"
  130. if ((hFile = CreateFile(szDir,
  131. GENERIC_WRITE,
  132. FILE_SHARE_READ,
  133. NULL,
  134. OPEN_ALWAYS,
  135. 0,
  136. NULL)) == INVALID_HANDLE_VALUE)
  137. {
  138. return FALSE;
  139. }
  140. return TRUE;
  141. }
  142. ///////////////////////////////////////////////////////////////////////////////
  143. //
  144. // Function : WriteLogFile
  145. // In : Variable number of arguments
  146. // Comments : If DEBUGGEROUT is defined, uses OutputDebugString to write
  147. // debug messages. If LOGFILEOUT is defined, uses WriteLogFile
  148. // to write to a file. The filename is found in the registry
  149. // If for some reason the reg value for filename is "", we
  150. // simply don't log.
  151. // Courtesy : darshats
  152. //
  153. ///////////////////////////////////////////////////////////////////////////////
  154. void WriteLogFile(LPCSTR s)
  155. {
  156. DWORD dwCurrSize = 0, cbWritten = 0;
  157. DWORD cbToWrite = lstrlenA(s);
  158. HANDLE hFile;
  159. if (!CreateOrOpenDebugFile(hFile))
  160. return;
  161. dwCurrSize = GetFileSize(hFile, NULL);
  162. SetFilePointer(hFile, dwCurrSize, NULL, FILE_BEGIN);
  163. (void) WriteFile(hFile, s, cbToWrite, &cbWritten, NULL);
  164. CloseHandle(hFile);
  165. }
  166. #endif // DBG