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.

329 lines
9.0 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1996.
  5. //
  6. // File: tracer.cxx
  7. //
  8. // Contents:
  9. //
  10. // Classes:
  11. //
  12. // Functions:
  13. //
  14. // Coupling:
  15. //
  16. // Notes:
  17. //
  18. // History: 10-28-1996 ericne Created
  19. //
  20. //----------------------------------------------------------------------------
  21. #include "pch.cxx"
  22. #include "tracer.hxx"
  23. //+---------------------------------------------------------------------------
  24. //
  25. // Member: CTracer::CTracer
  26. //
  27. // Synopsis:
  28. //
  29. // Arguments: (none)
  30. //
  31. // Returns:
  32. //
  33. // History: 10-28-1996 ericne Created
  34. //
  35. // Notes:
  36. //
  37. //----------------------------------------------------------------------------
  38. CTracer::CTracer( )
  39. : m_hTempFile( ),
  40. m_hMappedFile( ),
  41. m_tcsFileStart( NULL ),
  42. m_dwPut( 0 ),
  43. m_dwGet( 0 )
  44. {
  45. } //CTracer::CTracer
  46. //+---------------------------------------------------------------------------
  47. //
  48. // Member: CTracer::~CTracer
  49. //
  50. // Synopsis:
  51. //
  52. // Arguments: (none)
  53. //
  54. // Returns:
  55. //
  56. // History: 10-28-1996 ericne Created
  57. //
  58. // Notes:
  59. //
  60. //----------------------------------------------------------------------------
  61. CTracer::~CTracer( )
  62. {
  63. if( NULL != m_tcsFileStart )
  64. {
  65. if( ! UnmapViewOfFile( (LPCVOID) m_tcsFileStart ) )
  66. _tprintf( _T("Unable to unmap view to the temporary file.\r\n")
  67. _T("GetLastError return 0x%08X\r\n"), GetLastError());
  68. }
  69. } //CTracer::~CTracer
  70. //+---------------------------------------------------------------------------
  71. //
  72. // Member: CTracer::Init
  73. //
  74. // Synopsis:
  75. //
  76. // Arguments: (none)
  77. //
  78. // Returns: TRUE if successful, FALSE otherwise
  79. //
  80. // History: 10-28-1996 ericne Created
  81. //
  82. // Notes: CAutoHandle is a "smart" handle object that calls CloseHandle
  83. // in the destructor
  84. //
  85. //----------------------------------------------------------------------------
  86. BOOL CTracer::Init( )
  87. {
  88. TCHAR szFileName[ L_tmpnam ];
  89. CFileHandle FileHandle;
  90. CAutoHandle MapHandle;
  91. // Create a temporary file name
  92. if( NULL == _ttmpnam( szFileName ) )
  93. {
  94. return( FALSE );
  95. }
  96. // Open the file
  97. FileHandle = CreateFile( szFileName,
  98. GENERIC_READ | GENERIC_WRITE,
  99. 0,
  100. NULL,
  101. CREATE_NEW,
  102. FILE_FLAG_DELETE_ON_CLOSE,
  103. NULL );
  104. // MAke sure it was opened successfully
  105. if( INVALID_HANDLE_VALUE == FileHandle )
  106. {
  107. _ftprintf( stderr, _T("Unable to create temporary file\r\n")
  108. _T("GetLastError returns 0x%08X\r\n"), GetLastError() );
  109. return( FALSE );
  110. }
  111. // Create a file mapping
  112. MapHandle = CreateFileMapping( FileHandle.GetHandle(),
  113. NULL,
  114. PAGE_READWRITE,
  115. 0,
  116. dwMaxFileSize,
  117. NULL );
  118. // Make sure the file was mapped successfully
  119. if( NULL == MapHandle )
  120. {
  121. _ftprintf( stderr, _T("Unable to create mapped temp file\r\n")
  122. _T("GetLastError returns 0x%08X\r\n"), GetLastError() );
  123. return( FALSE );
  124. }
  125. // Map a view of the entire file
  126. m_tcsFileStart = (LPTSTR) MapViewOfFile( MapHandle.GetHandle(),
  127. FILE_MAP_ALL_ACCESS,
  128. 0,
  129. 0,
  130. 0 ); // Map the whole file
  131. // Make sure we have a valid view of the file
  132. if( NULL == m_tcsFileStart )
  133. {
  134. _ftprintf( stderr, _T("Unable to map view of temporary file\r\n")
  135. _T("GetLastError returns 0x%08X\r\n"), GetLastError() );
  136. return( FALSE );
  137. }
  138. // Make the first character in the file the EndOfEntry character
  139. m_tcsFileStart[ m_dwPut++ ] = tcsEndOfEntry;
  140. // Copy the file handle (increments the ref count)
  141. m_hTempFile = FileHandle;
  142. // Copy the map handle (increments the ref count)
  143. m_hMappedFile = MapHandle;
  144. return( TRUE );
  145. } //CTracer::Init
  146. //+---------------------------------------------------------------------------
  147. //
  148. // Member: CTracer::Trace
  149. //
  150. // Synopsis: Enter the message into the trace file
  151. //
  152. // Arguments: [szFormat] -- the format string
  153. //
  154. // Returns: (none)
  155. //
  156. // History: 10-28-1996 ericne Created
  157. //
  158. // Notes:
  159. //
  160. //----------------------------------------------------------------------------
  161. void CTracer::Trace( LPCTSTR szFormat, ... )
  162. {
  163. va_list va;
  164. if( m_dwPut >= dwMaxCharsInFile )
  165. return;
  166. va_start( va, szFormat );
  167. m_dwPut += _vsntprintf( &m_tcsFileStart[ m_dwPut ],
  168. dwMaxCharsInFile - m_dwPut,
  169. szFormat,
  170. va );
  171. m_tcsFileStart[ m_dwPut++ ] = tcsEndOfEntry;
  172. va_end( va );
  173. } //CTracer::Trace
  174. //+---------------------------------------------------------------------------
  175. //
  176. // Member: CTracer::Dump
  177. //
  178. // Synopsis: reads the past cEntries from the trace file and sends them
  179. // to pFile
  180. //
  181. // Arguments: [pFile] -- file stream pointer
  182. // [cEntries] -- count of entries to dump
  183. //
  184. // Returns: (none)
  185. //
  186. // History: 10-28-1996 ericne Created
  187. //
  188. // Notes: Passing in a negative number for cEntries dumps all the entries
  189. //
  190. //----------------------------------------------------------------------------
  191. void CTracer::Dump( FILE* pFile, int cEntries )
  192. {
  193. m_dwGet = m_dwPut - 1; // At last tcsEndOfEntry
  194. while( cEntries-- && 0 < m_dwGet )
  195. {
  196. // This cannot read before beginning of file since first
  197. // tchar in file is tcsEndOfEntry
  198. while( tcsEndOfEntry != m_tcsFileStart[ --m_dwGet ] )
  199. { }
  200. // This uses the fact that the EndOfEntry character is '\0'
  201. _ftprintf( pFile, _T("\r\n%s"), &m_tcsFileStart[ m_dwGet + 1 ] );
  202. }
  203. } //CTracer::Dump
  204. //+---------------------------------------------------------------------------
  205. //
  206. // Member: CTracer::Clear
  207. //
  208. // Synopsis: Flushes the contents of the trace file
  209. //
  210. // Arguments: (none)
  211. //
  212. // Returns: (none)
  213. //
  214. // History: 10-29-1996 ericne Created
  215. //
  216. // Notes:
  217. //
  218. //----------------------------------------------------------------------------
  219. void CTracer::Clear( )
  220. {
  221. m_dwPut = m_dwGet = 0;
  222. m_tcsFileStart[ m_dwPut++ ] = tcsEndOfEntry;
  223. } //CTracer::Clear
  224. //+---------------------------------------------------------------------------
  225. //
  226. // Member: CTracer::Interact
  227. //
  228. // Synopsis: Enters into an interactive dialog with the user, displays the
  229. // previous n entries in the trace file, and optionally sends
  230. // them to a user-specified file
  231. //
  232. // Arguments: (none)
  233. //
  234. // Returns: (none)
  235. //
  236. // History: 10-29-1996 ericne Created
  237. //
  238. // Notes:
  239. //
  240. //----------------------------------------------------------------------------
  241. void CTracer::Interact( )
  242. {
  243. const int nMaxCommentLength = 80;
  244. int cEntries = 0;
  245. FILE* pFile = NULL;
  246. TCHAR tcsComment[ nMaxCommentLength ];
  247. TCHAR tcsFileName[ MAX_PATH ];
  248. TCHAR tcsYesNo = _T('\0');
  249. while( 1 )
  250. {
  251. _tprintf( _T("\r\nHow many of the entries would you like see?\r\n")
  252. _T("( -1 for all, 0 to quit )\r\n") );
  253. fflush( stdin );
  254. _tscanf( _T("%i"), &cEntries );
  255. if( 0 == cEntries )
  256. break;
  257. Dump( stdout, cEntries );
  258. do
  259. {
  260. _tprintf( _T("\r\nWould you like to send ")
  261. _T("this to a file (y/n)? ") );
  262. fflush( stdin );
  263. tcsYesNo = (TCHAR) _totlower( _gettchar() );
  264. } while( _T('n') != tcsYesNo && _T('y') != tcsYesNo );
  265. if( _T('y') == tcsYesNo )
  266. {
  267. _tprintf( _T("\r\nEnter the name of the file ")
  268. _T("to which to append\r\n") );
  269. fflush( stdin );
  270. _getts( tcsFileName );
  271. pFile = _tfopen( tcsFileName, _T("a+") );
  272. _tprintf( _T("\r\nEnter a comment for this trace ")
  273. _T("(max %i chars)\r\n"), nMaxCommentLength );
  274. fflush( stdin );
  275. _getts( tcsComment );
  276. if( NULL != pFile )
  277. {
  278. _ftprintf( pFile, _T("\r\n\r\n%s\r\n"), tcsComment );
  279. Dump( pFile, cEntries );
  280. fclose( pFile );
  281. }
  282. }
  283. }
  284. } //CTracer::Interact