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.

234 lines
4.9 KiB

  1. /*++
  2. Copyright (c) 1989-2000 Microsoft Corporation
  3. Module Name:
  4. Log.cpp
  5. Abstract:
  6. This module implements the code for manipulating the log file.
  7. Author:
  8. clupu created 02/08/2001
  9. Revision History:
  10. --*/
  11. #include "stdafx.h"
  12. #include "Log.h"
  13. #include <stdio.h>
  14. #include <stdarg.h>
  15. BOOL g_bFileLogEnabled;
  16. TCHAR g_szFileLog[MAX_PATH];
  17. BOOL InitFileLogSupport( LPCTSTR lpszLogFileName )
  18. {
  19. HANDLE hFile = INVALID_HANDLE_VALUE;
  20. HANDLE hMap = NULL;
  21. PBYTE pMap;
  22. PISSUEREC pRecord;
  23. g_bFileLogEnabled = FALSE;
  24. //
  25. // The file log will be located on %windir%
  26. //
  27. GetSystemWindowsDirectory(g_szFileLog, MAX_PATH);
  28. lstrcat(g_szFileLog, _T("\\AppPatch\\"));
  29. lstrcat(g_szFileLog, lpszLogFileName);
  30. //
  31. // Try open the file. Create it if it doesn't exist.
  32. //
  33. hFile = CreateFile(g_szFileLog,
  34. GENERIC_READ | GENERIC_WRITE,
  35. 0,
  36. NULL,
  37. CREATE_ALWAYS,
  38. FILE_ATTRIBUTE_NORMAL,
  39. NULL);
  40. if ( hFile == INVALID_HANDLE_VALUE )
  41. {
  42. LogMessage(LOG_ERROR, _T("[InitFileLogSupport] Cannot create log file."));
  43. return FALSE;
  44. }
  45. hMap = CreateFileMapping(hFile,
  46. NULL,
  47. PAGE_READWRITE,
  48. 0,
  49. LOGFILESIZE,
  50. NULL);
  51. if ( hMap == NULL )
  52. {
  53. LogMessage(LOG_ERROR,
  54. _T("[LogAVStatus] CreateFileMapping failed. Status 0x%x"),
  55. GetLastError());
  56. goto CleanupAndFail;
  57. }
  58. pMap = (PBYTE)MapViewOfFile(hMap, FILE_MAP_WRITE, 0, 0, LOGFILESIZE);
  59. if ( pMap == NULL )
  60. {
  61. LogMessage(LOG_ERROR, _T("[LogAVStatus] MapViewOfFile failed."));
  62. goto CleanupAndFail;
  63. }
  64. ZeroMemory(pMap, LOGFILESIZE);
  65. //
  66. // Write the log header.
  67. //
  68. LOGFILEHEADER LogHeader;
  69. char szTemp[64];
  70. DWORD cbSize;
  71. ZeroMemory(&LogHeader, sizeof(LOGFILEHEADER));
  72. lstrcpyA(LogHeader.szMagic, LOG_FILE_MAGIC);
  73. LogHeader.dwRecords = MAX_ISSUES_COUNT;
  74. LogHeader.OSVersion.dwOSVersionInfoSize = sizeof(LogHeader.OSVersion);
  75. GetVersionExA((OSVERSIONINFOA*)&LogHeader.OSVersion);
  76. GetLocalTime(&LogHeader.time);
  77. cbSize = 64;
  78. GetUserNameA(szTemp, &cbSize);
  79. CopyMemory(LogHeader.szUserName, szTemp, 63);
  80. LogHeader.szUserName[63] = 0;
  81. cbSize = 64;
  82. GetComputerNameA(szTemp, &cbSize);
  83. CopyMemory(LogHeader.szMachineName, szTemp, 63);
  84. LogHeader.szUserName[63] = 0;
  85. //
  86. // Log the app start event.
  87. //
  88. pRecord = (ISSUEREC*)(pMap + sizeof(LOGFILEHEADER));
  89. pRecord += EVENTIND(AVS_APP_STARTED);
  90. (pRecord->dwOccurenceCount)++;
  91. FlushViewOfFile(pMap, 0);
  92. //
  93. // Set the global that tells file logging is enabled.
  94. //
  95. g_bFileLogEnabled = TRUE;
  96. CleanupAndFail:
  97. if ( pMap != NULL )
  98. {
  99. UnmapViewOfFile(pMap);
  100. }
  101. if ( hMap != NULL )
  102. {
  103. CloseHandle(hMap);
  104. }
  105. if ( hFile != INVALID_HANDLE_VALUE )
  106. {
  107. CloseHandle(hFile);
  108. }
  109. return g_bFileLogEnabled;
  110. }
  111. BOOL LogAVStatus( DWORD dwStatus )
  112. {
  113. HANDLE hFile = INVALID_HANDLE_VALUE;
  114. HANDLE hMap = NULL;
  115. PBYTE pMap = NULL;
  116. BOOL bReturn = FALSE;
  117. PISSUEREC pRecord;
  118. if ( !g_bFileLogEnabled )
  119. {
  120. return FALSE;
  121. }
  122. hFile = CreateFile(g_szFileLog,
  123. GENERIC_READ | GENERIC_WRITE,
  124. 0,
  125. NULL,
  126. OPEN_EXISTING,
  127. FILE_ATTRIBUTE_NORMAL | FILE_FLAG_RANDOM_ACCESS,
  128. NULL);
  129. if ( hFile == INVALID_HANDLE_VALUE )
  130. {
  131. LogMessage(LOG_ERROR, _T("[LogAVStatus] Cannot open the log file."));
  132. goto CleanupAndFail;
  133. }
  134. hMap = CreateFileMapping(hFile,
  135. NULL,
  136. PAGE_READWRITE,
  137. 0,
  138. LOGFILESIZE,
  139. NULL);
  140. if ( hMap == NULL )
  141. {
  142. LogMessage(LOG_ERROR, _T("[LogAVStatus] CreateFileMapping failed."));
  143. goto CleanupAndFail;
  144. }
  145. pMap = (PBYTE)MapViewOfFile(hMap, FILE_MAP_WRITE, 0, 0, LOGFILESIZE);
  146. if ( pMap == NULL )
  147. {
  148. LogMessage(LOG_ERROR, _T("[LogAVStatus] MapViewOfFile failed."));
  149. goto CleanupAndFail;
  150. }
  151. pRecord = (PISSUEREC)(pMap + sizeof(LOGFILEHEADER));
  152. pRecord += EVENTIND(dwStatus);
  153. (pRecord->dwOccurenceCount)++;
  154. FlushViewOfFile(pMap, 0);
  155. bReturn = TRUE;
  156. CleanupAndFail:
  157. if ( pMap != NULL )
  158. {
  159. UnmapViewOfFile(pMap);
  160. }
  161. if ( hMap != NULL )
  162. {
  163. CloseHandle(hMap);
  164. }
  165. if ( hFile != INVALID_HANDLE_VALUE )
  166. {
  167. CloseHandle(hFile);
  168. }
  169. return bReturn;
  170. }