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.

365 lines
8.5 KiB

  1. //*************************************************************
  2. //
  3. // Events.cpp - Routines to handle the event log
  4. //
  5. // Microsoft Confidential
  6. // Copyright (c) Microsoft Corporation 1995
  7. // All rights reserved
  8. //
  9. //*************************************************************
  10. #include "stdafx.h"
  11. #include "rsopdbg.h"
  12. #include "events.h"
  13. HANDLE hEventLog = NULL;
  14. TCHAR EventSourceName[] = TEXT("GPDAS");
  15. TCHAR MessageResourceFile[] = TEXT("%systemroot%\\system32\\rsopprov.exe");
  16. //*************************************************************
  17. //
  18. // InitializeEvents()
  19. //
  20. // Purpose: Opens the event log
  21. //
  22. // Parameters: void
  23. //
  24. // Return: TRUE if successful
  25. // FALSE if an error occurs
  26. //
  27. // Comments:
  28. //
  29. // History: Date Author Comment
  30. // 7/17/95 ericflo Created
  31. //
  32. //*************************************************************
  33. BOOL InitializeEvents (void)
  34. {
  35. //
  36. // Open the event source
  37. //
  38. hEventLog = RegisterEventSource(NULL, EventSourceName);
  39. if (hEventLog) {
  40. return TRUE;
  41. }
  42. dbg.Msg( DEBUG_MESSAGE_WARNING, TEXT("InitializeEvents: Could not open event log. Error = %d"), GetLastError());
  43. return FALSE;
  44. }
  45. //*************************************************************
  46. //
  47. // Implementation of CEvents
  48. //
  49. //*************************************************************
  50. //*************************************************************
  51. // CEvents::CEvents
  52. // Purpose: Constructor
  53. //
  54. // Parameters:
  55. // bError - Error or informational
  56. // dwId - Id of the eventlog msg
  57. //
  58. //
  59. // allocates a default sized array for the messages
  60. //*************************************************************
  61. #define DEF_ARG_SIZE 10
  62. CEvents::CEvents(BOOL bError, DWORD dwId ) :
  63. m_cStrings(0), m_cAllocated(0), m_bInitialised(FALSE),
  64. m_bError(bError), m_dwId(dwId), m_bFailed(TRUE)
  65. {
  66. //
  67. // Allocate a default size for the message
  68. //
  69. m_xlpStrings = (LPTSTR *)LocalAlloc(LPTR, sizeof(LPTSTR)*DEF_ARG_SIZE);
  70. m_cAllocated = DEF_ARG_SIZE;
  71. if (!m_xlpStrings) {
  72. dbg.Msg( DEBUG_MESSAGE_WARNING, TEXT("CEvent::CEvent Cannot log event, failed to allocate memory, error %d"), GetLastError());
  73. return;
  74. }
  75. //
  76. // Initialise eventlog if it is not already initialised
  77. //
  78. if (!hEventLog) {
  79. if (!InitializeEvents()) {
  80. dbg.Msg( DEBUG_MESSAGE_WARNING, TEXT("CEvent::CEvent Cannot log event, no handle"));
  81. return;
  82. }
  83. }
  84. m_bInitialised = TRUE;
  85. m_bFailed = FALSE;
  86. }
  87. //*************************************************************
  88. // CEvents::~CEvents()
  89. //
  90. // Purpose: Destructor
  91. //
  92. // Parameters: void
  93. //
  94. // frees the memory
  95. //*************************************************************
  96. CEvents::~CEvents()
  97. {
  98. for (int i = 0; i < m_cStrings; i++)
  99. if (m_xlpStrings[i])
  100. LocalFree(m_xlpStrings[i]);
  101. }
  102. //*************************************************************
  103. //
  104. // CEvents::ReallocArgStrings
  105. //
  106. // Purpose: Reallocates the buffer for storing arguments in case
  107. // the buffer runs out
  108. //
  109. // Parameters: void
  110. //
  111. // reallocates
  112. //*************************************************************
  113. BOOL CEvents::ReallocArgStrings()
  114. {
  115. XPtrLF<LPTSTR> aStringsNew;
  116. //
  117. // first allocate a larger buffer
  118. //
  119. aStringsNew = (LPTSTR *)LocalAlloc(LPTR, sizeof(LPTSTR)*(m_cAllocated+DEF_ARG_SIZE));
  120. if (!aStringsNew) {
  121. dbg.Msg( DEBUG_MESSAGE_WARNING, TEXT("CEvent::CEvent Couldn't allocate memory"));
  122. m_bFailed = TRUE;
  123. return FALSE;
  124. }
  125. //
  126. // copy the arguments
  127. //
  128. for (int i = 0; i < (m_cAllocated); i++) {
  129. aStringsNew[i] = m_xlpStrings[i];
  130. }
  131. m_xlpStrings = aStringsNew.Acquire();
  132. m_cAllocated+= DEF_ARG_SIZE;
  133. return TRUE;
  134. }
  135. //*************************************************************
  136. //
  137. // CEvents::AddArg
  138. //
  139. // Purpose: Add arguments appropriately formatted
  140. //
  141. // Parameters:
  142. //
  143. //*************************************************************
  144. BOOL CEvents::AddArg(LPTSTR szArg)
  145. {
  146. if ((!m_bInitialised) || (m_bFailed)) {
  147. dbg.Msg( DEBUG_MESSAGE_WARNING, TEXT("CEvent::AddArg: Cannot log event, not initialised or failed before"));
  148. return FALSE;
  149. }
  150. if (m_cStrings == m_cAllocated) {
  151. if (!ReallocArgStrings())
  152. return FALSE;
  153. }
  154. m_xlpStrings[m_cStrings] = (LPTSTR)LocalAlloc(LPTR, sizeof(TCHAR)*(lstrlen(szArg)+1));
  155. if (!m_xlpStrings[m_cStrings]) {
  156. dbg.Msg( DEBUG_MESSAGE_WARNING, TEXT("CEvent::AddArg Cannot allocate memory, error = %d"), GetLastError());
  157. m_bFailed = TRUE;
  158. return FALSE;
  159. }
  160. lstrcpy(m_xlpStrings[m_cStrings], szArg);
  161. m_cStrings++;
  162. return TRUE;
  163. }
  164. //*************************************************************
  165. //
  166. // CEvents::AddArg
  167. //
  168. // Purpose: Add arguments appropriately formatted
  169. //
  170. // Parameters:
  171. //
  172. //*************************************************************
  173. BOOL CEvents::AddArg(DWORD dwArg)
  174. {
  175. if ((!m_bInitialised) || (m_bFailed)) {
  176. dbg.Msg( DEBUG_MESSAGE_WARNING, TEXT("CEvent::AddArg(dw): Cannot log event, not initialised or failed before"));
  177. return FALSE;
  178. }
  179. if (m_cStrings == m_cAllocated) {
  180. if (!ReallocArgStrings())
  181. return FALSE;
  182. }
  183. // 2^32 < 10^10
  184. m_xlpStrings[m_cStrings] = (LPTSTR)LocalAlloc(LPTR, sizeof(TCHAR)*20);
  185. if (!m_xlpStrings[m_cStrings]) {
  186. dbg.Msg( DEBUG_MESSAGE_WARNING, TEXT("CEvent::AddArg(dw) Cannot allocate memory, error = %d"), GetLastError());
  187. m_bFailed = TRUE;
  188. return FALSE;
  189. }
  190. wsprintf(m_xlpStrings[m_cStrings], TEXT("%d"), dwArg);
  191. m_cStrings++;
  192. return TRUE;
  193. }
  194. //*************************************************************
  195. //
  196. // CEvents::AddArg
  197. //
  198. // Purpose: Add arguments appropriately formatted
  199. //
  200. // Parameters:
  201. //
  202. //*************************************************************
  203. BOOL CEvents::AddArgHex(DWORD dwArg)
  204. {
  205. if ((!m_bInitialised) || (m_bFailed)) {
  206. dbg.Msg( DEBUG_MESSAGE_WARNING, TEXT("CEvent::AddArgHex: Cannot log event, not initialised or failed before"));
  207. return FALSE;
  208. }
  209. if (m_cStrings == m_cAllocated) {
  210. if (!ReallocArgStrings())
  211. return FALSE;
  212. }
  213. m_xlpStrings[m_cStrings] = (LPTSTR)LocalAlloc(LPTR, sizeof(TCHAR)*20);
  214. if (!m_xlpStrings[m_cStrings]) {
  215. dbg.Msg( DEBUG_MESSAGE_WARNING, TEXT("CEvent::AddArgHex Cannot allocate memory, error = %d"), GetLastError());
  216. m_bFailed = TRUE;
  217. return FALSE;
  218. }
  219. wsprintf(m_xlpStrings[m_cStrings], TEXT("%#x"), dwArg);
  220. m_cStrings++;
  221. return TRUE;
  222. }
  223. //*************************************************************
  224. //
  225. // CEvents::Report
  226. //
  227. // Purpose: Actually collectes all the arguments and reports it to
  228. // the eventlog
  229. //
  230. // Parameters: void
  231. //
  232. //*************************************************************
  233. BOOL CEvents::Report()
  234. {
  235. PSID pSid = NULL; // no sid being reportewd currently
  236. WORD wType=0;
  237. BOOL bResult = TRUE;
  238. if ((!m_bInitialised) || (m_bFailed)) {
  239. dbg.Msg( DEBUG_MESSAGE_WARNING, TEXT("CEvents::Report: Cannot log event, not initialised or failed before"));
  240. return FALSE;
  241. }
  242. if ( m_bError ) {
  243. wType = EVENTLOG_ERROR_TYPE;
  244. } else {
  245. wType = EVENTLOG_INFORMATION_TYPE;
  246. }
  247. if (!ReportEvent(hEventLog, wType, 0, m_dwId, pSid, m_cStrings, 0, (LPCTSTR *)((LPTSTR *)m_xlpStrings), NULL)) {
  248. dbg.Msg( DEBUG_MESSAGE_WARNING, TEXT("CEvents::Report: ReportEvent failed. Error = %d"), GetLastError());
  249. bResult = FALSE;
  250. }
  251. return bResult;
  252. }
  253. //*************************************************************
  254. //
  255. // ShutdownEvents()
  256. //
  257. // Purpose: Stops the event log
  258. //
  259. // Parameters: void
  260. //
  261. // Return: TRUE if successful
  262. // FALSE if an error occurs
  263. //
  264. // Comments:
  265. //
  266. // History: Date Author Comment
  267. // 7/17/95 ericflo Created
  268. //
  269. //*************************************************************
  270. BOOL ShutdownEvents (void)
  271. {
  272. BOOL bRetVal = TRUE;
  273. if (hEventLog) {
  274. bRetVal = DeregisterEventSource(hEventLog);
  275. hEventLog = NULL;
  276. }
  277. return bRetVal;
  278. }