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.

250 lines
5.2 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. //
  5. // Copyright (C) Microsoft Corporation, 1997 - 1999
  6. //
  7. // File: eventlog.cpp
  8. //
  9. //--------------------------------------------------------------------------
  10. #include "pch.h"
  11. #pragma hdrstop
  12. #include "eventlog.h"
  13. CEventLog::~CEventLog(
  14. void
  15. )
  16. {
  17. Close();
  18. }
  19. //
  20. // Register the specified event source.
  21. // Note that the registry entries must already exist.
  22. // HKLM\System\CurrentControlSet\Services\EventLog\Application\<pszEventSource>
  23. // Requires values "EventMessageFile" and "TypesSupported".
  24. //
  25. HRESULT
  26. CEventLog::Initialize(
  27. LPCTSTR pszEventSource
  28. )
  29. {
  30. if (NULL != m_hLog)
  31. {
  32. return S_FALSE;
  33. }
  34. HRESULT hr = NOERROR;
  35. m_hLog = RegisterEventSource(NULL, pszEventSource);
  36. if (NULL == m_hLog)
  37. {
  38. hr = HRESULT_FROM_WIN32(GetLastError());
  39. }
  40. return hr;
  41. }
  42. //
  43. // Deregister the event source.
  44. //
  45. void
  46. CEventLog::Close(
  47. void
  48. )
  49. {
  50. if (NULL != m_hLog)
  51. {
  52. DeregisterEventSource(m_hLog);
  53. m_hLog = NULL;
  54. }
  55. }
  56. //
  57. // Report an event. No replaceable parameters explicitly specified.
  58. // If msg string contains replaceable parameters, use Push() to
  59. // build list of replacement strings.
  60. //
  61. HRESULT
  62. CEventLog::ReportEvent(
  63. WORD wType,
  64. WORD wCategory,
  65. DWORD dwEventID,
  66. PSID lpUserSid, // [optional]
  67. LPVOID pvRawData, // [optional]
  68. DWORD cbRawData // [optional]
  69. )
  70. {
  71. if (NULL == m_hLog)
  72. return E_FAIL;
  73. BOOL bResult = FALSE;
  74. HRESULT hr = NOERROR;
  75. if (!::ReportEvent(m_hLog,
  76. wType,
  77. wCategory,
  78. dwEventID,
  79. lpUserSid,
  80. (WORD)m_rgstrText.Count(),
  81. cbRawData,
  82. m_rgstrText,
  83. pvRawData))
  84. {
  85. //
  86. // Special-case ERROR_IO_PENDING. ::ReportEvent will fail with
  87. // this error code even when it succeeds. Don't know exactly why
  88. // but it does. Treat this as success so we don't get unnecessary
  89. // debugger output.
  90. //
  91. DWORD dwError = GetLastError();
  92. if (ERROR_IO_PENDING != dwError)
  93. {
  94. hr = HRESULT_FROM_WIN32(dwError);
  95. }
  96. }
  97. m_rgstrText.Clear();
  98. return hr;
  99. }
  100. //
  101. // Push an HRESULT value onto the stack of replacment strings.
  102. //
  103. void
  104. CEventLog::Push(
  105. HRESULT hr,
  106. eFmt fmt
  107. )
  108. {
  109. if (eFmtSysErr == fmt)
  110. {
  111. LPTSTR pszBuffer = NULL;
  112. int cchLoaded = ::FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM |
  113. FORMAT_MESSAGE_ALLOCATE_BUFFER,
  114. NULL,
  115. HRESULT_CODE(hr),
  116. 0,
  117. (LPTSTR)&pszBuffer,
  118. 1,
  119. NULL);
  120. if (NULL != pszBuffer)
  121. {
  122. if (0 != cchLoaded)
  123. {
  124. m_rgstrText.Append(pszBuffer);
  125. }
  126. LocalFree(pszBuffer);
  127. }
  128. }
  129. else
  130. {
  131. TCHAR szNumber[40];
  132. wsprintf(szNumber, eFmtDec == fmt ? TEXT("%d") : TEXT("0x%08X"), hr);
  133. m_rgstrText.Append(szNumber);
  134. }
  135. }
  136. //
  137. // Push a string onto the stack of replacement strings.
  138. //
  139. void
  140. CEventLog::Push(
  141. LPCTSTR psz
  142. )
  143. {
  144. m_rgstrText.Append(psz);
  145. }
  146. CEventLog::CStrArray::CStrArray(
  147. void
  148. ) : m_cEntries(0)
  149. {
  150. ZeroMemory(m_rgpsz, sizeof(m_rgpsz));
  151. }
  152. LPCTSTR
  153. CEventLog::CStrArray::Get(
  154. int iEntry
  155. ) const
  156. {
  157. TraceAssert(iEntry < m_cEntries);
  158. if (iEntry < m_cEntries)
  159. return m_rgpsz[iEntry];
  160. return NULL;
  161. }
  162. bool
  163. CEventLog::CStrArray::Append(
  164. LPCTSTR psz
  165. )
  166. {
  167. TraceAssert(m_cEntries < (ARRAYSIZE(m_rgpsz) - 1));
  168. if (m_cEntries < (ARRAYSIZE(m_rgpsz) - 1))
  169. {
  170. LPTSTR pszNew = new TCHAR[lstrlen(psz) + 1];
  171. if (NULL != pszNew)
  172. {
  173. lstrcpy(pszNew, psz);
  174. m_rgpsz[m_cEntries++] = pszNew;
  175. return true;
  176. }
  177. }
  178. return false;
  179. }
  180. void
  181. CEventLog::CStrArray::Destroy(
  182. void
  183. )
  184. {
  185. for (int i = 0; i < ARRAYSIZE(m_rgpsz); i++)
  186. {
  187. delete[] m_rgpsz[i];
  188. m_rgpsz[i] = NULL;
  189. }
  190. m_cEntries = 0;
  191. }
  192. //-----------------------------------------------------------------------------
  193. // CscuiEventLog member functions.
  194. //-----------------------------------------------------------------------------
  195. HRESULT
  196. CscuiEventLog::ReportEvent(
  197. WORD wType,
  198. DWORD dwEventID,
  199. int iMinLevel,
  200. PSID lpUserSid,
  201. LPVOID pvRawData,
  202. DWORD cbRawData
  203. )
  204. {
  205. int iLevel = CConfig::GetSingleton().EventLoggingLevel();
  206. if (SUCCEEDED(m_log.Initialize(TEXT("Offline Files"))))
  207. {
  208. if (iLevel >= iMinLevel)
  209. {
  210. return m_log.ReportEvent(wType,
  211. 0,
  212. dwEventID,
  213. lpUserSid,
  214. pvRawData,
  215. cbRawData);
  216. }
  217. }
  218. return S_FALSE;
  219. }