Leaked source code of windows server 2003
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.

245 lines
5.1 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 = ::FormatSystemError(&pszBuffer, HRESULT_CODE(hr));
  113. if (NULL != pszBuffer)
  114. {
  115. if (0 != cchLoaded)
  116. {
  117. m_rgstrText.Append(pszBuffer);
  118. }
  119. LocalFree(pszBuffer);
  120. }
  121. }
  122. else
  123. {
  124. TCHAR szNumber[40];
  125. wnsprintf(szNumber, ARRAYSIZE(szNumber), eFmtDec == fmt ? TEXT("%d") : TEXT("0x%08X"), hr);
  126. m_rgstrText.Append(szNumber);
  127. }
  128. }
  129. //
  130. // Push a string onto the stack of replacement strings.
  131. //
  132. void
  133. CEventLog::Push(
  134. LPCTSTR psz
  135. )
  136. {
  137. m_rgstrText.Append(psz);
  138. }
  139. CEventLog::CStrArray::CStrArray(
  140. void
  141. ) : m_cEntries(0)
  142. {
  143. ZeroMemory(m_rgpsz, sizeof(m_rgpsz));
  144. }
  145. LPCTSTR
  146. CEventLog::CStrArray::Get(
  147. int iEntry
  148. ) const
  149. {
  150. TraceAssert(iEntry < m_cEntries);
  151. if (iEntry < m_cEntries)
  152. return m_rgpsz[iEntry];
  153. return NULL;
  154. }
  155. bool
  156. CEventLog::CStrArray::Append(
  157. LPCTSTR psz
  158. )
  159. {
  160. TraceAssert(m_cEntries < (ARRAYSIZE(m_rgpsz) - 1));
  161. if (m_cEntries < (ARRAYSIZE(m_rgpsz) - 1))
  162. {
  163. LONG cch = lstrlen(psz) + 1;
  164. LPTSTR pszNew = new TCHAR[cch];
  165. if (NULL != pszNew)
  166. {
  167. StringCchCopy(pszNew, cch, psz);
  168. m_rgpsz[m_cEntries++] = pszNew;
  169. return true;
  170. }
  171. }
  172. return false;
  173. }
  174. void
  175. CEventLog::CStrArray::Destroy(
  176. void
  177. )
  178. {
  179. for (int i = 0; i < ARRAYSIZE(m_rgpsz); i++)
  180. {
  181. delete[] m_rgpsz[i];
  182. m_rgpsz[i] = NULL;
  183. }
  184. m_cEntries = 0;
  185. }
  186. //-----------------------------------------------------------------------------
  187. // CscuiEventLog member functions.
  188. //-----------------------------------------------------------------------------
  189. HRESULT
  190. CscuiEventLog::ReportEvent(
  191. WORD wType,
  192. DWORD dwEventID,
  193. int iMinLevel,
  194. PSID lpUserSid,
  195. LPVOID pvRawData,
  196. DWORD cbRawData
  197. )
  198. {
  199. int iLevel = CConfig::GetSingleton().EventLoggingLevel();
  200. if (SUCCEEDED(m_log.Initialize(TEXT("Offline Files"))))
  201. {
  202. if (iLevel >= iMinLevel)
  203. {
  204. return m_log.ReportEvent(wType,
  205. 0,
  206. dwEventID,
  207. lpUserSid,
  208. pvRawData,
  209. cbRawData);
  210. }
  211. }
  212. return S_FALSE;
  213. }