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.

110 lines
3.5 KiB

  1. #ifndef __DBGTIMER_H_INCLUDED
  2. #define __DBGTIMER_H_INCLUDED
  3. class CDebugTimer
  4. {
  5. private:
  6. FILETIME m_ftBeginTime;
  7. TCHAR m_szTitle[MAX_PATH];
  8. public:
  9. CDebugTimer( LPCTSTR pszTimerName=NULL )
  10. {
  11. Start( pszTimerName );
  12. }
  13. virtual ~CDebugTimer(void)
  14. {
  15. End();
  16. }
  17. void GetSystemTimeAsFileTime( FILETIME &ft )
  18. {
  19. SYSTEMTIME st;
  20. GetSystemTime( &st );
  21. SystemTimeToFileTime( &st, &ft );
  22. }
  23. void Start( LPCTSTR pszName )
  24. {
  25. lstrcpy( m_szTitle, TEXT("") );
  26. if (pszName && *pszName)
  27. {
  28. GetSystemTimeAsFileTime(m_ftBeginTime);
  29. lstrcpy( m_szTitle, pszName );
  30. }
  31. }
  32. void WriteToFile( LPCTSTR szMessage )
  33. {
  34. TCHAR szFilename[MAX_PATH];
  35. if (GetEnvironmentVariable(TEXT("WIADEBUGFILE"),szFilename,sizeof(szFilename)/sizeof(TCHAR)))
  36. {
  37. HANDLE m_hMutex = CreateMutex( NULL, FALSE, TEXT("CDebugTimerFileMutex") );
  38. if (m_hMutex)
  39. {
  40. DWORD dwResult = WaitForSingleObject( m_hMutex, 1000 );
  41. if (WAIT_OBJECT_0==dwResult)
  42. {
  43. HANDLE hFile = CreateFile( szFilename, GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL );
  44. if (INVALID_HANDLE_VALUE == hFile)
  45. {
  46. DWORD dwBytesWritten;
  47. SetFilePointer( hFile, 0, NULL, FILE_END );
  48. WriteFile( hFile, szMessage, lstrlen(szMessage) * sizeof(TCHAR), &dwBytesWritten, NULL );
  49. CloseHandle(hFile);
  50. }
  51. else
  52. {
  53. OutputDebugString(TEXT("CDebugTimer::WriteToFile: Unable to open log file\n"));
  54. }
  55. ReleaseMutex(m_hMutex);
  56. }
  57. else
  58. {
  59. OutputDebugString(TEXT("WaitForSingleObject failed\n"));
  60. }
  61. CloseHandle(m_hMutex);
  62. }
  63. else
  64. {
  65. OutputDebugString(TEXT("CDebugTimer::WriteToFile: Unable to create mutex\n"));
  66. }
  67. }
  68. }
  69. void Elapsed(void)
  70. {
  71. if (lstrlen(m_szTitle))
  72. {
  73. FILETIME ft;
  74. GetSystemTimeAsFileTime(ft);
  75. LARGE_INTEGER liStart, liEnd;
  76. liStart.LowPart = m_ftBeginTime.dwLowDateTime;
  77. liStart.HighPart = m_ftBeginTime.dwHighDateTime;
  78. liEnd.LowPart = ft.dwLowDateTime;
  79. liEnd.HighPart = ft.dwHighDateTime;
  80. LONGLONG nElapsedTime = liEnd.QuadPart - liStart.QuadPart;
  81. nElapsedTime /= 10000;
  82. int nMilliseconds = (int)(nElapsedTime % 1000);
  83. nElapsedTime /= 1000;
  84. int nSeconds = (int)(nElapsedTime);
  85. TCHAR szMessage[MAX_PATH];
  86. wsprintf(szMessage, TEXT("*TIMER* Elapsed time for [%s]: %d.%04d\n"), m_szTitle, nSeconds, nMilliseconds );
  87. OutputDebugString( szMessage );
  88. WriteToFile( szMessage );
  89. }
  90. }
  91. void End(void)
  92. {
  93. Elapsed();
  94. lstrcpy( m_szTitle, TEXT("") );
  95. }
  96. };
  97. #if defined(DBG) || defined(_DEBUG) || defined(DEBUG)
  98. #define WIA_TIMEFUNCTION(x) CDebugTimer _debugFunctionDebugTimer(TEXT(x))
  99. #define WIA_TIMERSTART(n,x) CDebugTimer _debugTimer##n(TEXT(x))
  100. #define WIA_TIMEREND(n) _debugTimer##n.End()
  101. #else
  102. #define WIA_TIMEFUNCTION(x)
  103. #define WIA_TIMERSTART(n,x)
  104. #define WIA_TIMEREND(n)
  105. #endif
  106. #endif