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.

181 lines
5.6 KiB

  1. // Copyright (c) 1996-1999 Microsoft Corporation
  2. /*
  3. Implementation of Win95 tracing facility to mimic that of NT. Works on both.
  4. */
  5. #pragma warning(disable:4201) // allows nameless structs and unions
  6. #pragma warning(disable:4514) // don't care when unreferenced inline functions are removed
  7. #pragma warning(disable:4706) // we are allowed to assign within a conditional
  8. #include "windows.h"
  9. #include <stdio.h>
  10. #include <stdarg.h>
  11. #include <process.h>
  12. #include "w95trace.h"
  13. #if defined( _DEBUG ) ||defined( DEBUG ) || defined( DBG )
  14. #ifdef __cplusplus
  15. extern "C" {
  16. #endif
  17. static HANDLE g_hSpewFile = INVALID_HANDLE_VALUE;
  18. __inline BOOL TestMutex()
  19. {
  20. HANDLE hTestMutex = OpenMutex( SYNCHRONIZE, FALSE, TEXT("oleacc-msaa-use-dbwin") );
  21. if( ! hTestMutex )
  22. return FALSE;
  23. CloseHandle( hTestMutex );
  24. return TRUE;
  25. }
  26. void OutputDebugStringW95( LPCTSTR lpOutputString, ...)
  27. {
  28. // Only produce output if this mutex is set...
  29. if (TestMutex())
  30. {
  31. HANDLE heventDBWIN; /* DBWIN32 synchronization object */
  32. HANDLE heventData; /* data passing synch object */
  33. HANDLE hSharedFile; /* memory mapped file shared data */
  34. LPTSTR lpszSharedMem;
  35. TCHAR achBuffer[500];
  36. /* create the output buffer */
  37. va_list args;
  38. va_start(args, lpOutputString);
  39. wvsprintf(achBuffer, lpOutputString, args);
  40. va_end(args);
  41. /*
  42. Do a regular OutputDebugString so that the output is
  43. still seen in the debugger window if it exists.
  44. This ifdef is necessary to avoid infinite recursion
  45. from the inclusion of W95TRACE.H
  46. */
  47. #ifdef UNICODE
  48. OutputDebugStringW(achBuffer);
  49. #else
  50. OutputDebugStringA(achBuffer);
  51. #endif
  52. /* bail if it's not Win95 */
  53. {
  54. OSVERSIONINFO VerInfo;
  55. VerInfo.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
  56. GetVersionEx(&VerInfo);
  57. if ( VerInfo.dwPlatformId != VER_PLATFORM_WIN32_WINDOWS )
  58. return;
  59. }
  60. /* make sure DBWIN is open and waiting */
  61. heventDBWIN = OpenEvent(EVENT_MODIFY_STATE, FALSE, TEXT("DBWIN_BUFFER_READY"));
  62. if ( !heventDBWIN )
  63. {
  64. //MessageBox(NULL, TEXT("DBWIN_BUFFER_READY nonexistent"), NULL, MB_OK);
  65. return;
  66. }
  67. /* get a handle to the data synch object */
  68. heventData = OpenEvent(EVENT_MODIFY_STATE, FALSE, TEXT("DBWIN_DATA_READY"));
  69. if ( !heventData )
  70. {
  71. // MessageBox(NULL, TEXT("DBWIN_DATA_READY nonexistent"), NULL, MB_OK);
  72. CloseHandle(heventDBWIN);
  73. return;
  74. }
  75. hSharedFile = CreateFileMapping((HANDLE)-1, NULL, PAGE_READWRITE, 0, 4096, TEXT("DBWIN_BUFFER"));
  76. if (!hSharedFile)
  77. {
  78. //MessageBox(NULL, TEXT("DebugTrace: Unable to create file mapping object DBWIN_BUFFER"), TEXT("Error"), MB_OK);
  79. CloseHandle(heventDBWIN);
  80. CloseHandle(heventData);
  81. return;
  82. }
  83. lpszSharedMem = (LPTSTR)MapViewOfFile(hSharedFile, FILE_MAP_WRITE, 0, 0, 512);
  84. if (!lpszSharedMem)
  85. {
  86. //MessageBox(NULL, "DebugTrace: Unable to map shared memory", "Error", MB_OK);
  87. CloseHandle(heventDBWIN);
  88. CloseHandle(heventData);
  89. return;
  90. }
  91. /* wait for buffer event */
  92. WaitForSingleObject(heventDBWIN, INFINITE);
  93. /* write it to the shared memory */
  94. *((LPDWORD)lpszSharedMem) = _getpid();
  95. wsprintf(lpszSharedMem + sizeof(DWORD), TEXT("%s"), achBuffer);
  96. /* signal data ready event */
  97. SetEvent(heventData);
  98. /* clean up handles */
  99. CloseHandle(hSharedFile);
  100. CloseHandle(heventData);
  101. CloseHandle(heventDBWIN);
  102. }
  103. return;
  104. }
  105. void SpewOpenFile(LPCTSTR pszSpewFile)
  106. {
  107. #ifdef UNICODE
  108. // Only produce output if this mutex is set...
  109. if (g_hSpewFile == INVALID_HANDLE_VALUE && TestMutex())
  110. {
  111. TCHAR szSpewFile[MAX_PATH];
  112. GetTempPath(MAX_PATH, szSpewFile);
  113. if (lstrlen(szSpewFile)+lstrlen(pszSpewFile) >= MAX_PATH)
  114. {
  115. MessageBox(NULL, TEXT("SpewOpenFile: Name will be longer than MAX_PATH"), TEXT("OOPS"), MB_OK);
  116. return;
  117. }
  118. lstrcat(szSpewFile, pszSpewFile);
  119. g_hSpewFile = CreateFile(szSpewFile, GENERIC_WRITE, 0, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
  120. if (INVALID_HANDLE_VALUE == g_hSpewFile)
  121. {
  122. // MessageBox(NULL, TEXT("SpewOpenFile: Unable to open spew file"), TEXT("Error"), MB_OK);
  123. }
  124. }
  125. #endif
  126. }
  127. void SpewToFile( LPCTSTR lpOutputString, ...)
  128. {
  129. #ifdef UNICODE
  130. if (g_hSpewFile != INVALID_HANDLE_VALUE && TestMutex())
  131. {
  132. TCHAR achBuffer[1025];
  133. CHAR achAnsiBuf[500];
  134. DWORD dwcBytesWr, dwcBytes;
  135. va_list args;
  136. va_start(args, lpOutputString);
  137. wvsprintf(achBuffer, lpOutputString, args);
  138. dwcBytes = WideCharToMultiByte(CP_ACP, 0, achBuffer, -1, achAnsiBuf, sizeof(achAnsiBuf)*sizeof(CHAR), NULL, NULL);
  139. if (!WriteFile(g_hSpewFile, achAnsiBuf, dwcBytes-1, &dwcBytesWr, NULL))
  140. {
  141. // MessageBox(NULL, TEXT("SpewToFile: Unable to write to spew file"), TEXT("Error"), MB_OK);
  142. }
  143. va_end(args);
  144. }
  145. #endif
  146. }
  147. void SpewCloseFile()
  148. {
  149. #ifdef UNICODE
  150. if (g_hSpewFile != INVALID_HANDLE_VALUE && TestMutex())
  151. CloseHandle(g_hSpewFile);
  152. #endif
  153. }
  154. #ifdef __cplusplus
  155. }
  156. #endif
  157. #endif