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.

275 lines
7.6 KiB

  1. ///////////////////////////////////////////////////////////////////////////////
  2. //
  3. // File Name
  4. // EventLogger.CPP
  5. //
  6. // Description
  7. //
  8. // Revision: 2.01
  9. //
  10. // Copyright (c) 1996-1999 Acotec, Inc.
  11. // All rights reserved.
  12. //
  13. //
  14. // Portions Copyright (c) 1999 Microsoft Corporation. All rights reserved.
  15. //
  16. #include "clogger.h"
  17. ///////////////////////////////////////////////////////////////////////////////
  18. // CEventLogger::CEventLogger()
  19. //
  20. // Parameters
  21. //
  22. // Purpose
  23. //
  24. // Return Value
  25. //
  26. CEventLogger::CEventLogger()
  27. {
  28. InitializeCriticalSection (&m_csObj);
  29. m_hEventLog = NULL;
  30. m_wServiceCategory = 0;
  31. m_LoggingLevel = LOGGING_LEVEL_1;
  32. m_hLogMutex = NULL;
  33. m_fLogEvent = TRUE;
  34. m_hMsgSource = NULL;
  35. ZeroMemory (m_szCat, sizeof(m_szCat));
  36. }
  37. ///////////////////////////////////////////////////////////////////////////////
  38. // CEventLogger::CEventLogger()
  39. //
  40. // Parameters
  41. //
  42. // Purpose
  43. //
  44. // Return Value
  45. //
  46. CEventLogger::CEventLogger(LPTSTR szEventSource, WORD wEventsCategory)
  47. {
  48. CEventLogger();
  49. InitEventLog (szEventSource, wEventsCategory);
  50. }
  51. ///////////////////////////////////////////////////////////////////////////////
  52. // CEventLogger::~CEventLogger()
  53. //
  54. // Parameters
  55. //
  56. // Purpose
  57. //
  58. // Return Value
  59. //
  60. CEventLogger::~CEventLogger()
  61. {
  62. if (m_hLogMutex)
  63. {
  64. CloseHandle (m_hLogMutex);
  65. }
  66. if (m_hEventLog)
  67. {
  68. DeregisterEventSource (m_hEventLog);
  69. }
  70. DeleteCriticalSection (&m_csObj);
  71. }
  72. ///////////////////////////////////////////////////////////////////////////////
  73. // CEventLogger::InitEventLog()
  74. //
  75. // Parameters
  76. //
  77. // Purpose
  78. //
  79. // Return Value
  80. //
  81. DWORD WINAPI CEventLogger::InitEventLog (LPTSTR szEventSource,
  82. WORD wEventsCategory,
  83. LPTSTR szRegKey)
  84. {
  85. EnterCriticalSection (&m_csObj);
  86. if (m_hEventLog)
  87. {
  88. DeregisterEventSource (m_hEventLog);
  89. m_hEventLog = NULL;
  90. }
  91. m_wServiceCategory = wEventsCategory;
  92. m_LoggingLevel = LOGGING_LEVEL_1;
  93. HKEY hKey;
  94. DWORD dwValue=LOGGING_LEVEL_1, dwError = 0, dwSize = sizeof(DWORD);
  95. m_hEventLog = RegisterEventSource (NULL, szEventSource);
  96. if (!m_hEventLog)
  97. {
  98. dwError = GetLastError();
  99. m_LoggingLevel = LOGGING_LEVEL_0;
  100. }
  101. else
  102. if (szRegKey &&
  103. ERROR_SUCCESS == (dwError=RegOpenKeyEx (HKEY_LOCAL_MACHINE,
  104. szRegKey,
  105. 0,
  106. KEY_READ,
  107. &hKey)))
  108. {
  109. dwError=RegQueryValueEx (hKey,
  110. REGVAL_LOGGING_LEVEL,
  111. NULL,
  112. NULL,
  113. (LPBYTE)&dwValue,
  114. &dwSize);
  115. if(ERROR_SUCCESS == dwError )
  116. {
  117. if (dwValue > (DWORD)LOGGING_LEVEL_3)
  118. {
  119. dwValue = (DWORD)LOGGING_LEVEL_3;
  120. }
  121. m_LoggingLevel = (LOGLEVEL)dwValue;
  122. }
  123. RegCloseKey (hKey);
  124. }
  125. LeaveCriticalSection (&m_csObj);
  126. return dwError;
  127. }
  128. ///////////////////////////////////////////////////////////////////////////////
  129. // CEventLogger::SetLoggingLevel()
  130. //
  131. // Parameters
  132. //
  133. // Purpose
  134. //
  135. // Return Value
  136. //
  137. void WINAPI CEventLogger::SetLoggingLevel (LOGLEVEL NewLoggingLevel)
  138. {
  139. EnterCriticalSection (&m_csObj);
  140. m_LoggingLevel = NewLoggingLevel;
  141. LeaveCriticalSection (&m_csObj);
  142. }
  143. ///////////////////////////////////////////////////////////////////////////////
  144. // CEventLogger::LogEvent()
  145. //
  146. // Parameters
  147. //
  148. // Purpose
  149. //
  150. // Return Value
  151. //
  152. void WINAPI CEventLogger::LogEvent (LOGTYPE Type,
  153. DWORD dwEventID,
  154. DWORD dwData,
  155. LPCTSTR rgszMsgs[],
  156. WORD wCount,
  157. DWORD cbData,
  158. LPVOID pvData)
  159. {
  160. if (LOGGING_LEVEL_0 == m_LoggingLevel &&
  161. LOGTYPE_FORCE_ERROR != Type &&
  162. LOGTYPE_FORCE_WARNING != Type &&
  163. LOGTYPE_FORCE_INFORMATION != Type)
  164. {
  165. return;
  166. }
  167. if (!m_fLogEvent)
  168. return;
  169. TCHAR szBuffer[64];
  170. LPCTSTR rgszError[] =
  171. {
  172. szBuffer
  173. };
  174. LPTSTR pszType;
  175. WORD wEventType;
  176. switch (Type)
  177. {
  178. case LOGTYPE_ERR_CRITICAL :
  179. case LOGTYPE_FORCE_ERROR :
  180. pszType = _T("ERROR");
  181. wEventType = EVENTLOG_ERROR_TYPE;
  182. if (NULL == rgszMsgs)
  183. {
  184. _stprintf (szBuffer, _T("%d"), dwData);
  185. rgszMsgs = rgszError;
  186. wCount = 1;
  187. }
  188. break;
  189. case LOGTYPE_ERR_WARNING :
  190. if (m_LoggingLevel < LOGGING_LEVEL_2) { return; }
  191. case LOGTYPE_FORCE_WARNING :
  192. pszType = _T("WARNING");
  193. wEventType = EVENTLOG_WARNING_TYPE;
  194. if (dwData && NULL == rgszMsgs)
  195. {
  196. _stprintf (szBuffer, _T("%d"), dwData);
  197. rgszMsgs = rgszError;
  198. wCount = 1;
  199. }
  200. break;
  201. case LOGTYPE_AUDIT_SECURITY_ACCESS :
  202. case LOGTYPE_AUDIT_SECURITY_DENIED :
  203. pszType = _T("SECURITY");
  204. case LOGTYPE_INFORMATION :
  205. if (m_LoggingLevel < LOGGING_LEVEL_3) { return; }
  206. case LOGTYPE_FORCE_INFORMATION :
  207. switch (Type)
  208. {
  209. case LOGTYPE_AUDIT_SECURITY_ACCESS : wEventType = EVENTLOG_AUDIT_SUCCESS; break;
  210. case LOGTYPE_AUDIT_SECURITY_DENIED : wEventType = EVENTLOG_AUDIT_FAILURE; break;
  211. case LOGTYPE_FORCE_INFORMATION :
  212. case LOGTYPE_INFORMATION :
  213. wEventType = EVENTLOG_INFORMATION_TYPE;
  214. pszType = _T("INFORMATION");
  215. break;
  216. }
  217. break;
  218. case LOGTYPE_VERBOSE :
  219. case LOGTYPE_VERBOSE_ERROR :
  220. case LOGTYPE_VERBOSE_WARNING :
  221. if (m_LoggingLevel < LOGGING_LEVEL_3) { return; }
  222. if ((LOGTYPE_VERBOSE_ERROR == Type || LOGTYPE_VERBOSE_WARNING == Type) &&
  223. dwData && NULL == rgszMsgs)
  224. {
  225. _stprintf (szBuffer, _T("%d"), dwData);
  226. rgszMsgs = rgszError;
  227. wCount = 1;
  228. }
  229. switch (Type)
  230. {
  231. case LOGTYPE_VERBOSE :
  232. wEventType = EVENTLOG_INFORMATION_TYPE;
  233. pszType = _T("V_INFORMATION");
  234. break;
  235. case LOGTYPE_VERBOSE_ERROR :
  236. wEventType = EVENTLOG_ERROR_TYPE;
  237. pszType = _T("V_ERROR");
  238. break;
  239. case LOGTYPE_VERBOSE_WARNING :
  240. wEventType = EVENTLOG_WARNING_TYPE;
  241. pszType = _T("V_WARNING");
  242. break;
  243. }
  244. break;
  245. default :
  246. return;
  247. }
  248. if (m_hEventLog)
  249. {
  250. ReportEvent (m_hEventLog,
  251. wEventType,
  252. m_wServiceCategory,
  253. dwEventID,
  254. NULL,
  255. wCount,
  256. cbData,
  257. rgszMsgs,
  258. pvData);
  259. }
  260. }