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.

549 lines
13 KiB

  1. /*++
  2. Copyright (c) 2000-2001 Microsoft Corporation
  3. Module Name:
  4. Log.cpp
  5. Abstract:
  6. This DLL handles the logging for the SMVTest.
  7. Author:
  8. Diaa Fathalla (DiaaF) 27-Nov-2000
  9. Revision History:
  10. --*/
  11. #include "stdafx.h"
  12. #include "Log.h"
  13. #include "memory.h"
  14. BOOL APIENTRY DllMain( HANDLE hModule,
  15. DWORD ul_reason_for_call,
  16. LPVOID lpReserved
  17. )
  18. {
  19. switch (ul_reason_for_call)
  20. {
  21. case DLL_PROCESS_ATTACH:
  22. case DLL_THREAD_ATTACH:
  23. case DLL_THREAD_DETACH:
  24. case DLL_PROCESS_DETACH:
  25. break;
  26. }
  27. return TRUE;
  28. }
  29. //
  30. // Constructor of a class that is being exported.
  31. //
  32. CLog::CLog()
  33. {
  34. m_iProcessor = 0;
  35. m_iOperatingSys = 0;
  36. m_iNTProductType = 0;
  37. m_sMachineName = new TCHAR[MAX_COMPUTERNAME_LENGTH + 1];
  38. m_sBuildNumber = new TCHAR[NORMAL_SIZE];
  39. m_sCSDVersion = new TCHAR[NORMAL_SIZE];
  40. m_sProcessor = new TCHAR[NORMAL_SIZE];
  41. ZeroMemory(m_sMachineName,MAX_COMPUTERNAME_LENGTH + 1);
  42. ZeroMemory(m_sBuildNumber, NORMAL_SIZE);
  43. ZeroMemory(m_sCSDVersion, NORMAL_SIZE);
  44. ZeroMemory(m_sProcessor, NORMAL_SIZE);
  45. }
  46. //
  47. // Destructor of a class that is being exported.
  48. //
  49. CLog::~CLog()
  50. {
  51. if (m_sMachineName)
  52. delete m_sMachineName;
  53. if (m_sBuildNumber)
  54. delete m_sBuildNumber;
  55. if (m_sCSDVersion)
  56. delete m_sCSDVersion;
  57. if (m_sProcessor)
  58. delete m_sProcessor;
  59. }
  60. //
  61. // InitLogfile
  62. //
  63. BOOL LOG_API CLog::InitLogfile(LPCTSTR szLogfile, LPCTSTR szLogfileFolder)
  64. {
  65. if (!s_bFirstTime)
  66. s_bFirstTime = TRUE;
  67. else
  68. return SUCCESS;
  69. LPTSTR szBuffer;
  70. LPTSTR szLogfilePath = new TCHAR[MAX_PATH];
  71. LPTSTR szTempPath = new TCHAR[MAX_PATH];
  72. //Set Global path for the global
  73. SetLogfile(szLogfile);
  74. SetLogfileFolder(szLogfileFolder);
  75. _tcscpy(m_sLogfile, szLogfile);
  76. _tcscpy(m_sLogfileFolder, szLogfileFolder);
  77. GetWindowsDirectory(szLogfilePath, MAX_PATH);
  78. _tcscat(szLogfilePath, TEXT("\\"));
  79. _tcscat(szLogfilePath, m_sLogfileFolder);
  80. //Get local computer info
  81. GetLocalComputerInfo();
  82. //Make sure TestLogs folder exists
  83. WIN32_FIND_DATA pFindFileData;
  84. _tcscpy(szTempPath, szLogfilePath);
  85. _tcscat(szTempPath, TEXT("\\*.*"));
  86. HANDLE hFileHndl = FindFirstFile(szTempPath, &pFindFileData);
  87. if(hFileHndl != INVALID_HANDLE_VALUE)
  88. FindClose(hFileHndl);
  89. else
  90. CreateDirectory(szLogfilePath,NULL);
  91. //DeleteAllLogFiles();
  92. szTempPath = _tcscat(_tcscat(_tcscpy(szTempPath,szLogfilePath),TEXT("\\")),m_sLogfile);
  93. hFileHndl = CreateFile(szTempPath, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
  94. FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN, NULL);
  95. delete szLogfilePath;
  96. delete szTempPath;
  97. if(hFileHndl == NULL)
  98. {
  99. return FAILURE;
  100. }
  101. else
  102. {
  103. //Log file not found so create new one
  104. DWORD dwWritten = NULL;
  105. szBuffer = LogInitHeader();
  106. WriteFile(hFileHndl, (LPTSTR) szBuffer, _tcslen(szBuffer), &dwWritten, NULL);
  107. CloseHandle(hFileHndl);
  108. delete szBuffer;
  109. if (dwWritten == NULL)
  110. return FAILURE;
  111. }
  112. return SUCCESS;
  113. }
  114. //
  115. // InitLogfileInfo
  116. //
  117. BOOL LOG_API CLog::InitLogfileInfo(LPCTSTR szLogfile, LPCTSTR szLogfileFolder)
  118. {
  119. if (!s_bFirstTime)
  120. s_bFirstTime = TRUE;
  121. else
  122. return SUCCESS;
  123. LPTSTR szLogfilePath = new TCHAR[MAX_PATH];
  124. LPTSTR szTempPath = new TCHAR[MAX_PATH];
  125. //Set Global path for the global
  126. SetLogfile(szLogfile);
  127. SetLogfileFolder(szLogfileFolder);
  128. _tcscpy(m_sLogfile, szLogfile);
  129. _tcscpy(m_sLogfileFolder, szLogfileFolder);
  130. GetWindowsDirectory(szLogfilePath, MAX_PATH);
  131. _tcscat(szLogfilePath, TEXT("\\"));
  132. _tcscat(szLogfilePath, m_sLogfileFolder);
  133. //Get local computer info
  134. GetLocalComputerInfo();
  135. //Make sure TestLogs folder exists
  136. WIN32_FIND_DATA pFindFileData;
  137. szTempPath = _tcscat(_tcscat(_tcscpy(szTempPath,szLogfilePath),TEXT("\\")),m_sLogfile);
  138. HANDLE hFileHndl = FindFirstFile(szTempPath, &pFindFileData);
  139. if(hFileHndl != INVALID_HANDLE_VALUE)
  140. FindClose(hFileHndl);
  141. else
  142. {
  143. CreateDirectory(szLogfilePath,NULL);
  144. hFileHndl = CreateFile(szTempPath, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
  145. FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN, NULL);
  146. }
  147. delete szLogfilePath;
  148. delete szTempPath;
  149. return SUCCESS;
  150. }
  151. //
  152. // EndLogfile
  153. //
  154. BOOL CLog::EndLogfile()
  155. {
  156. if (s_bFirstTime)
  157. s_bFirstTime = FALSE;
  158. else
  159. return SUCCESS;
  160. LPCTSTR szShortLine = TEXT("------------------------------------------------------------");
  161. LPCTSTR szLEnd = TEXT("-----------------------------------");
  162. LPCTSTR szLogtext = TEXT("SMV : Shim Automation Verifier");
  163. LPTSTR szLine = new TCHAR[_tcslen(szShortLine) + _tcslen(szLEnd) + 1];
  164. LPTSTR szLogfilePath = new TCHAR[MAX_PATH];
  165. GetWindowsDirectory(szLogfilePath, MAX_PATH);
  166. _tcscat(szLogfilePath, TEXT("\\"));
  167. _tcscat(szLogfilePath, m_sLogfileFolder);
  168. _tcscat(szLogfilePath, TEXT("\\"));
  169. _tcscat(szLogfilePath, m_sLogfile);
  170. LPTSTR szBuffer = LogCompTail();
  171. HANDLE hFileHndl = CreateFile(szLogfilePath, GENERIC_WRITE, 0, NULL, OPEN_ALWAYS,
  172. FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN, NULL);
  173. SetFilePointer(hFileHndl, NULL, NULL, FILE_END);
  174. DWORD dwWritten = NULL;
  175. WriteFile(hFileHndl, (LPSTR)szBuffer, _tcslen(szBuffer), &dwWritten, NULL);
  176. WriteFile(hFileHndl, (LPSTR)szBuffer, _tcslen(szBuffer), &dwWritten, NULL);
  177. CloseHandle(hFileHndl);
  178. delete szBuffer;
  179. delete szLine;
  180. delete szLogfilePath;
  181. if (m_sLogfile)
  182. delete m_sLogfile;
  183. if (m_sLogfileFolder)
  184. delete m_sLogfileFolder;
  185. return SUCCESS;
  186. }
  187. //
  188. // DeleteAllLogFiles
  189. //
  190. void CLog::DeleteAllLogFiles()
  191. {
  192. LPTSTR szLogfileFolder = new TCHAR[MAX_PATH];
  193. _tcscpy(szLogfileFolder,m_sLogfileFolder);
  194. LPTSTR szSearchForFiles = new TCHAR[MAX_PATH];
  195. _tcscpy(szSearchForFiles, szLogfileFolder);
  196. _tcscat(szSearchForFiles, TEXT("\\*.*"));
  197. LPTSTR szLogfilePath = new TCHAR[MAX_PATH];
  198. _tcscpy(szLogfilePath, szLogfileFolder);
  199. _tcscat(szLogfilePath, TEXT("\\"));
  200. _tcscat(szLogfilePath, m_sLogfile);
  201. WIN32_FIND_DATA pFindFileData;
  202. HANDLE hFileToDelete;
  203. BOOL bFoundNextFile = FALSE;
  204. DWORD dwFindNextFileLastErr = 0;
  205. DWORD fErr = 0;
  206. hFileToDelete = FindFirstFile(szSearchForFiles, &pFindFileData);
  207. if(hFileToDelete != INVALID_HANDLE_VALUE)
  208. {
  209. do
  210. {
  211. if(FindNextFile(hFileToDelete, &pFindFileData))
  212. {
  213. if(pFindFileData.dwFileAttributes != FILE_ATTRIBUTE_DIRECTORY)
  214. //Do not delete the SMV log file
  215. if(strcmp(m_sLogfile,pFindFileData.cFileName) != 0)
  216. {
  217. LPTSTR szTemp = new TCHAR[MAX_PATH];
  218. _tcscpy(szTemp, m_sLogfileFolder);
  219. _tcscat(szTemp, TEXT("\\"));
  220. _tcscat(szTemp, pFindFileData.cFileName);
  221. DeleteFile(szTemp);
  222. delete szTemp;
  223. }
  224. }
  225. dwFindNextFileLastErr = GetLastError();
  226. }
  227. while (dwFindNextFileLastErr != ERROR_NO_MORE_FILES);
  228. FindClose(hFileToDelete);
  229. }
  230. delete szLogfileFolder;
  231. delete szSearchForFiles;
  232. delete szLogfilePath;
  233. }
  234. //
  235. // LogResults
  236. //
  237. BOOL CLog::LogResults(BOOL bPassed, LPCTSTR szText)
  238. {
  239. if (!s_bFirstTime)
  240. return FAILURE; //initLogFile is not initialized
  241. LPTSTR szLogfileFolder = new TCHAR[MAX_PATH];
  242. _tcscpy(szLogfileFolder, m_sLogfileFolder);
  243. LPTSTR szLogfilePath = new TCHAR[MAX_PATH];
  244. GetWindowsDirectory(szLogfilePath, MAX_PATH);
  245. szLogfilePath = _tcscat(szLogfilePath, TEXT("\\"));
  246. szLogfilePath = _tcscat(_tcscat(_tcscat(szLogfilePath,m_sLogfileFolder), TEXT("\\")), m_sLogfile);
  247. LPCTSTR szTab = TEXT("\t");
  248. LPCTSTR szSpace = TEXT("\r\n");
  249. LPCTSTR szPassed = TEXT("PASSED\t:");
  250. LPCTSTR szFailed = TEXT("FAILED\t:");
  251. LPTSTR szBuffer = new TCHAR[NORMAL_SIZE + _tcslen(szText)];
  252. _tcscpy(szBuffer, szTab);
  253. IncNumberOfTests();
  254. if (bPassed)
  255. szBuffer = _tcscat(_tcscat(szBuffer, szPassed), szText);
  256. else
  257. {
  258. IncNumberOfFailures();
  259. szBuffer = _tcscat(_tcscat(_tcscat(szBuffer, szFailed), szText), szSpace);
  260. }
  261. szBuffer = _tcscat(szBuffer, szSpace);
  262. HANDLE hFileHndl = CreateFile(szLogfilePath, GENERIC_WRITE, 0, NULL, OPEN_ALWAYS,
  263. FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN, NULL);
  264. SetFilePointer(hFileHndl, NULL, NULL, FILE_END);
  265. delete szLogfileFolder;
  266. delete szLogfilePath;
  267. if(hFileHndl == NULL)
  268. {
  269. delete szBuffer;
  270. return FAILURE;
  271. }
  272. else
  273. {
  274. DWORD dwWritten = NULL;
  275. WriteFile(hFileHndl, (LPSTR)szBuffer, _tcslen(szBuffer), &dwWritten, NULL);
  276. CloseHandle(hFileHndl);
  277. delete szBuffer;
  278. if (dwWritten == NULL)
  279. return FAILURE;
  280. }
  281. return SUCCESS;
  282. }
  283. //
  284. // LogInitHeader
  285. //
  286. LPTSTR CLog::LogInitHeader()
  287. {
  288. LPTSTR szBuffer = new TCHAR[NORMAL_SIZE*10];
  289. LPCTSTR szTab = TEXT("\t");
  290. LPCTSTR szSpace = TEXT("\r\n");
  291. LPCTSTR szShortLine = TEXT("------------------------------------------------------------");
  292. LPCTSTR szLEnd = TEXT("-----------------------------------");
  293. LPCTSTR szLogtext = TEXT("SMV-TEST : Shim Automation Verifier TEST");
  294. LPTSTR szLine = new TCHAR[_tcslen(szShortLine) + _tcslen(szLEnd) + 1];
  295. szLine = _tcscat(_tcscpy(szLine,szShortLine),szLEnd);
  296. LPTSTR szVersion = new TCHAR[NORMAL_SIZE*3];
  297. _tcscpy(szVersion, m_sBuildNumber);
  298. LPTSTR szMachineName = new TCHAR[NORMAL_SIZE*2];
  299. _tcscpy(szMachineName,TEXT("Machine Name:"));
  300. szMachineName = _tcscat(_tcscat(_tcscat(szMachineName, szTab), szTab), m_sMachineName);
  301. if (m_sCSDVersion != NULL)
  302. szVersion = _tcscat(_tcscat(_tcscat(szVersion, TEXT(" (")), m_sCSDVersion), _TEXT(")"));
  303. LPTSTR szBuildNumber = new TCHAR[NORMAL_SIZE*3];
  304. _tcscpy(szBuildNumber, _T("Build # :"));
  305. szBuildNumber = _tcscat(_tcscat(_tcscat(szBuildNumber, szTab), szTab), szVersion);
  306. szBuffer = _tcscat(_tcscpy(szBuffer, szLine), szSpace);
  307. szBuffer = _tcscat(_tcscat(_tcscat(szBuffer, szTab), szLogtext), szSpace);
  308. szBuffer = _tcscat(_tcscat(_tcscat(szBuffer, szLine), szSpace), szSpace);
  309. szBuffer = _tcscat(_tcscat(_tcscat(szBuffer, szTab), szMachineName), szSpace);
  310. szBuffer = _tcscat(_tcscat(_tcscat(szBuffer, szTab), szBuildNumber), szSpace);
  311. //SYSTEMTIME InitLogTime;
  312. //GetSystemTime(&InitLogTime);
  313. szBuffer = _tcscat(_tcscat(szBuffer, szLine), szSpace);
  314. szBuffer = _tcscat(_tcscat(szBuffer, szLine), szSpace);
  315. delete szLine;
  316. delete szVersion;
  317. delete szMachineName;
  318. delete szBuildNumber;
  319. return szBuffer;
  320. }
  321. //
  322. // LogCompTail
  323. //
  324. LPTSTR CLog::LogCompTail()
  325. {
  326. LPTSTR szTemp = new TCHAR[NORMAL_SIZE * 10];
  327. LPTSTR szLogfilePath = new TCHAR[MAX_PATH];
  328. szLogfilePath = _tcscat(_tcscat(_tcscpy(szLogfilePath,m_sLogfileFolder), TEXT("\\")), m_sLogfile);
  329. LPCTSTR szSpace = TEXT("\r\n");
  330. LPCTSTR szTab = TEXT("\t");
  331. LPCTSTR szShortLine = TEXT("------------------------------------------------------------");
  332. LPCTSTR szLEnd = TEXT("-----------------------------------");
  333. LPCTSTR szLogtext = TEXT("Shim Mechanism Verifier");
  334. LPTSTR szLine = new TCHAR[_tcslen(szShortLine) + _tcslen(szLEnd) + 1];
  335. szLine = _tcscat(_tcscpy(szLine,szShortLine),szLEnd);
  336. //CTime tmEnd = m_pScript->m_tmScriptEndTime;
  337. //CTime tmStart = m_pScript->m_tmScriptStartTime;
  338. //CString sDate1 = tmStart.Format("%A, %B %d, %Y\t%I:%M:%S%p");
  339. //CString sDate2 = tmEnd.Format("%A, %B %d, %Y\t%I:%M:%S%p");
  340. //sTemp += sTab + _T("Script Start Time :") + sTab + sDate1 + sSpace;
  341. //sTemp += sTab + _T("Script Finish Time:") + sTab + sDate2 + sSpace;
  342. szTemp = _tcscat(_tcscpy(szTemp,szLine), szSpace);
  343. szTemp = _tcscat(_tcscat(szTemp,szLine), szSpace);
  344. delete szLogfilePath;
  345. delete szLine;
  346. return szTemp;
  347. }
  348. //
  349. // DoesLogfileExist: Checks if ShimTest.log exists on the local machine.
  350. //
  351. BOOL CLog::DoesLogfileExist()
  352. {
  353. LPTSTR szPath = new TCHAR[MAX_PATH];
  354. _tcscat(szPath,m_sLogfileFolder);
  355. _tcscat(szPath,TEXT("\\"));
  356. _tcscat(szPath,m_sLogfile);
  357. WIN32_FIND_DATA pFindFileData;
  358. HANDLE hFileHndl = FindFirstFile(szPath, &pFindFileData);
  359. if(hFileHndl != INVALID_HANDLE_VALUE)
  360. {
  361. FindClose(hFileHndl);
  362. delete szPath;
  363. return TRUE;
  364. }
  365. delete szPath;
  366. return FALSE;
  367. }
  368. //
  369. // GetLocalComputerInfo()
  370. //
  371. void CLog::GetLocalComputerInfo()
  372. {
  373. OSVERSIONINFO OSVer;
  374. SYSTEM_INFO SysInf;
  375. DWORD dwNameSize = MAX_COMPUTERNAME_LENGTH + 1;
  376. TCHAR sMachineName[MAX_COMPUTERNAME_LENGTH + 1];
  377. // Get machine name and language of system
  378. GetComputerName(sMachineName, &dwNameSize);
  379. _tcscpy(m_sMachineName, TEXT(sMachineName));
  380. //Get OS and build #
  381. OSVer.dwOSVersionInfoSize = sizeof(OSVer);
  382. GetVersionEx(&OSVer);
  383. m_iOperatingSys = OSVer.dwPlatformId;
  384. wsprintf(m_sBuildNumber, TEXT("%d"), OSVer.dwBuildNumber);
  385. if (OSVer.szCSDVersion[0] != NULL)
  386. m_sCSDVersion = TEXT(OSVer.szCSDVersion);
  387. //Get processor type, and workstation or server
  388. GetSystemInfo(&SysInf);
  389. m_iProcessor = SysInf.wProcessorArchitecture;
  390. switch(m_iProcessor)
  391. {
  392. case 0: _tcscpy(m_sProcessor,TEXT("x86")); break;
  393. case 2: _tcscpy(m_sProcessor,TEXT("Alpha")); break;
  394. default: _tcscpy(m_sProcessor, TEXT("Unknown Processor Architcture")); break;
  395. }
  396. }
  397. //
  398. // SetLogfile
  399. //
  400. void CLog::SetLogfile(LPCTSTR szLogfile)
  401. {
  402. m_sLogfile = new TCHAR[MAX_PATH];
  403. _tcscpy(m_sLogfile,szLogfile);
  404. }
  405. //
  406. // SetLogfileFolder
  407. //
  408. void CLog::SetLogfileFolder(LPCTSTR szLogfileFolder)
  409. {
  410. m_sLogfileFolder = new TCHAR[MAX_PATH];
  411. _tcscpy(m_sLogfileFolder,szLogfileFolder);
  412. }
  413. //
  414. // IncNumberOfTests
  415. //
  416. void CLog::IncNumberOfTests()
  417. {
  418. s_NumberOfTests++;
  419. }
  420. //
  421. // IncNumberOfFailures
  422. //
  423. void CLog::IncNumberOfFailures()
  424. {
  425. s_NumberOfFailures++;
  426. }
  427. //
  428. // NumberOfTests
  429. //
  430. int CLog::NumberOfTests()
  431. {
  432. return s_NumberOfTests;
  433. }
  434. //
  435. // NumberOfFailures
  436. //
  437. int CLog::NumberOfFailures()
  438. {
  439. return s_NumberOfFailures;
  440. }