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.

155 lines
3.2 KiB

  1. /*++
  2. Copyright (c) 2000 Microsoft Corporation
  3. Module Name:
  4. LogCollect.h
  5. Abstract:
  6. Author:
  7. Hakki T. Bostanci (hakkib) 06-Apr-2000
  8. Revision History:
  9. --*/
  10. #ifndef _LOGCOLLECT_H_
  11. #define _LOGCOLLECT_H_
  12. //////////////////////////////////////////////////////////////////////////
  13. //
  14. //
  15. //
  16. extern CLogWindow g_LogWindow;
  17. //////////////////////////////////////////////////////////////////////////
  18. //
  19. //
  20. //
  21. class CLogCollector
  22. {
  23. public:
  24. CLogCollector(
  25. PCTSTR pLocalLogFileName,
  26. PCTSTR pRemoteBaseDirName
  27. )
  28. {
  29. // store the local log file name
  30. m_pLocalLogFile = _tcsdupc(pLocalLogFileName);
  31. // produce a remote file name based on time, user, machine name
  32. SYSTEMTIME SystemTime;
  33. GetLocalTime(&SystemTime);
  34. m_pRemoteLogFile = bufprintf(
  35. //LONG_PATH
  36. _T("%s\\%02d-%02d-%04d_%02d-%02d-%02d_%s_%s.log"),
  37. pRemoteBaseDirName,
  38. SystemTime.wMonth,
  39. SystemTime.wDay,
  40. SystemTime.wYear,
  41. SystemTime.wHour,
  42. SystemTime.wMinute,
  43. SystemTime.wSecond,
  44. (PCTSTR) CUserName(),
  45. (PCTSTR) CComputerName()
  46. );
  47. // create the worker thread
  48. // we don't want a test thread to crash while a log copy is in progress,
  49. // so make sure this thread runs in highest priority class
  50. m_Thread = CThread(
  51. CopyLogFileThreadProc,
  52. this,
  53. 0,
  54. 0,
  55. CREATE_SUSPENDED
  56. );
  57. SetThreadPriority(m_Thread, THREAD_PRIORITY_HIGHEST);
  58. ResumeThread(m_Thread);
  59. }
  60. ~CLogCollector()
  61. {
  62. // flush the log
  63. g_pLog = new CLog;
  64. // copy it to the server
  65. CopyLogFile();
  66. // wait for the worker thread to die
  67. m_Thread.WaitForSingleObject();
  68. }
  69. private:
  70. void CopyLogFile()
  71. {
  72. try
  73. {
  74. // copy (the last 8k of) the log file to the remote location
  75. CInFile InFile(m_pLocalLogFile, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE);
  76. COutFile OutFile(m_pRemoteLogFile, GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE);
  77. SetFilePointer(InFile, -1 * 8 * 1024, 0, FILE_END);
  78. CopyFileContents(InFile, OutFile);
  79. // get the current time
  80. SYSTEMTIME st;
  81. GetSystemTime(&st);
  82. FILETIME ft;
  83. SystemTimeToFileTime(&st, &ft);
  84. // update the remote log file time
  85. SetFileTime(OutFile, &ft, &ft, &ft);
  86. }
  87. catch (...)
  88. {
  89. // don't die on any exception...
  90. }
  91. }
  92. static DWORD WINAPI CopyLogFileThreadProc(PVOID pThreadParameter)
  93. {
  94. // copy the log file every 5 minutes until the app is closed
  95. CLogCollector *that = (CLogCollector *) pThreadParameter;
  96. do
  97. {
  98. that->CopyLogFile();
  99. }
  100. while (g_LogWindow.WaitForSingleObject(5 * 60 * 1000) == WAIT_TIMEOUT);
  101. return TRUE;
  102. }
  103. private:
  104. CCppMem<TCHAR> m_pLocalLogFile;
  105. CCppMem<TCHAR> m_pRemoteLogFile;
  106. CThread m_Thread;
  107. };
  108. #endif //_LOGCOLLECT_H_