Source code of Windows XP (NT5)
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.

261 lines
5.4 KiB

  1. /*++
  2. Copyright (c) 1995 Microsoft Corporation
  3. Module Name:
  4. debug.c
  5. Abstract:
  6. Diagnositc/debug routines for Windows NT Setup module.
  7. Author:
  8. Ted Miller (tedm) 31-Mar-1995
  9. Revision History:
  10. --*/
  11. #include "setupp.h"
  12. #pragma hdrstop
  13. //
  14. // This can be turned on in the debugger so that we get debug spew on free builds.
  15. //
  16. bWriteDebugSpew = FALSE;
  17. #if DBG
  18. VOID
  19. AssertFail(
  20. IN PSTR FileName,
  21. IN UINT LineNumber,
  22. IN PSTR Condition
  23. )
  24. {
  25. int i;
  26. CHAR Name[MAX_PATH];
  27. PCHAR p;
  28. CHAR Msg[4096];
  29. //
  30. // Use dll name as caption
  31. //
  32. GetModuleFileNameA(MyModuleHandle,Name,MAX_PATH);
  33. if(p = strrchr(Name,'\\')) {
  34. p++;
  35. } else {
  36. p = Name;
  37. }
  38. wsprintfA(
  39. Msg,
  40. "Assertion failure at line %u in file %s: %s\n\nCall DebugBreak()?",
  41. LineNumber,
  42. FileName,
  43. Condition
  44. );
  45. i = MessageBoxA(
  46. NULL,
  47. Msg,
  48. p,
  49. MB_YESNO | MB_TASKMODAL | MB_ICONSTOP | MB_SETFOREGROUND
  50. );
  51. if(i == IDYES) {
  52. DebugBreak();
  53. }
  54. }
  55. #endif
  56. VOID
  57. pSetupDebugPrint(
  58. PWSTR FileName,
  59. ULONG LineNumber,
  60. PWSTR TagStr,
  61. PWSTR FormatStr,
  62. ...
  63. )
  64. {
  65. static WCHAR buf[4096];
  66. static HANDLE hFile = NULL;
  67. va_list arg_ptr;
  68. ULONG Bytes;
  69. PWSTR s,p;
  70. PSTR str;
  71. SYSTEMTIME CurrTime;
  72. DWORD Result;
  73. //
  74. // Note: If hFile is NULL, that means it's the first time we've been called,
  75. // and we may want to open the log file. If we set hFile to
  76. // INVALID_HANDLE_VALUE, that means we've decided not to write to the file.
  77. //
  78. #if DBG
  79. {
  80. //
  81. // If OobeSetup is FALSE when we are first called, and becomes TRUE at
  82. // some later point, logging doesn't work. This ASSERT makes sure that
  83. // doesn't happen.
  84. //
  85. static BOOL OobeSetOnFirstCall = FALSE;
  86. if ( hFile == NULL ) {
  87. OobeSetOnFirstCall = OobeSetup;
  88. }
  89. MYASSERT( OobeSetOnFirstCall == OobeSetup );
  90. }
  91. #endif
  92. GetLocalTime( &CurrTime );
  93. if (hFile == NULL) {
  94. if ( IsSetup || OobeSetup ) {
  95. Result = GetWindowsDirectory( buf, sizeof(buf)/sizeof(WCHAR) );
  96. if(Result == 0) {
  97. MYASSERT(FALSE);
  98. return;
  99. }
  100. pSetupConcatenatePaths( buf, L"setuplog.txt", sizeof(buf)/sizeof(WCHAR), NULL );
  101. //
  102. // If we're in OOBE, we want to append to the file that already exists
  103. //
  104. hFile = CreateFile(
  105. buf,
  106. GENERIC_READ | GENERIC_WRITE,
  107. FILE_SHARE_READ | FILE_SHARE_WRITE,
  108. NULL,
  109. OobeSetup ? OPEN_ALWAYS : CREATE_ALWAYS,
  110. FILE_ATTRIBUTE_NORMAL | FILE_FLAG_WRITE_THROUGH,
  111. NULL
  112. );
  113. if (hFile != INVALID_HANDLE_VALUE)
  114. {
  115. if (OobeSetup) {
  116. SetFilePointer (hFile, 0, NULL, FILE_END);
  117. }
  118. swprintf(buf, L"Time,File,Line,Tag,Message\r\n");
  119. Bytes = wcslen(buf) + 4;
  120. str = MyMalloc(Bytes);
  121. if (str != NULL)
  122. {
  123. WideCharToMultiByte(
  124. CP_ACP,
  125. 0,
  126. buf,
  127. -1,
  128. str,
  129. Bytes,
  130. NULL,
  131. NULL
  132. );
  133. WriteFile(
  134. hFile,
  135. str,
  136. wcslen(buf),
  137. &Bytes,
  138. NULL
  139. );
  140. MyFree( str );
  141. }
  142. buf[0] = '\0';
  143. }
  144. } else { // !IsSetup
  145. //
  146. // Don't write to file, just do DbgPrintEx
  147. //
  148. hFile = INVALID_HANDLE_VALUE;
  149. }
  150. }
  151. _try {
  152. p = buf;
  153. *p = 0;
  154. swprintf( p, L"%02d/%02d/%04d %02d:%02d:%02d,%s,%d,%s,",
  155. CurrTime.wMonth,
  156. CurrTime.wDay,
  157. CurrTime.wYear,
  158. CurrTime.wHour,
  159. CurrTime.wMinute,
  160. CurrTime.wSecond,
  161. (NULL != FileName) ? FileName : L"",
  162. LineNumber,
  163. (NULL != TagStr) ? TagStr : L""
  164. );
  165. p += wcslen(p);
  166. va_start( arg_ptr, FormatStr );
  167. _vsnwprintf( p, 2048, FormatStr, arg_ptr );
  168. va_end( arg_ptr );
  169. p += wcslen(p);
  170. wcscat( p, L"\r\n" );
  171. } except(EXCEPTION_EXECUTE_HANDLER) {
  172. buf[0] = 0;
  173. }
  174. if (buf[0] == 0) {
  175. return;
  176. }
  177. Bytes = (wcslen( buf )*2) + 4;
  178. str = MyMalloc( Bytes );
  179. if (str == NULL) {
  180. return;
  181. }
  182. WideCharToMultiByte(
  183. CP_ACP,
  184. 0,
  185. buf,
  186. -1,
  187. str,
  188. Bytes,
  189. NULL,
  190. NULL
  191. );
  192. //
  193. // Write out the string to the debugger if the process is being debugged, or the
  194. // debug filter allows it.
  195. //
  196. if ( bWriteDebugSpew ) {
  197. OutputDebugString( buf );
  198. } else {
  199. #if DBG
  200. DbgPrintEx( DPFLTR_SETUP_ID, DPFLTR_INFO_LEVEL, str );
  201. #endif
  202. }
  203. if (hFile == INVALID_HANDLE_VALUE) {
  204. MyFree( str );
  205. return;
  206. }
  207. WriteFile(
  208. hFile,
  209. str,
  210. wcslen(buf),
  211. &Bytes,
  212. NULL
  213. );
  214. MyFree( str );
  215. return;
  216. }