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.

271 lines
8.8 KiB

  1. //--------------------------------------------------------------------
  2. // Copyright (C) Microsoft Corporation, 1999 - 1999, All Rights Reserved
  3. //
  4. // eventlog.cpp
  5. //
  6. // Implementation of a simple event logging class.
  7. //
  8. //--------------------------------------------------------------------
  9. #include <windows.h>
  10. #include <stdio.h>
  11. #include "eventlog.h"
  12. #include <strsafe.h>
  13. //--------------------------------------------------------------------
  14. // EVENT_LOG::EVENT_LOG()
  15. //
  16. //--------------------------------------------------------------------
  17. EVENT_LOG::EVENT_LOG( WCHAR *pwsEventSourceName,
  18. DWORD *pdwStatus )
  19. {
  20. *pdwStatus = 0;
  21. m_hEventLog = RegisterEventSourceW( NULL, pwsEventSourceName );
  22. if (!m_hEventLog)
  23. {
  24. *pdwStatus = GetLastError();
  25. }
  26. }
  27. //--------------------------------------------------------------------
  28. // EVENT_LOG:;~EVENT_LOG()
  29. //
  30. //--------------------------------------------------------------------
  31. EVENT_LOG::~EVENT_LOG()
  32. {
  33. if (m_hEventLog)
  34. {
  35. DeregisterEventSource(m_hEventLog);
  36. }
  37. }
  38. //--------------------------------------------------------------------
  39. // EVENT_LOG::CheckConfiguration()
  40. //
  41. //--------------------------------------------------------------------
  42. DWORD EVENT_LOG::CheckConfiguration( WCHAR *pwszEventSourceName,
  43. WCHAR *pwszCatalogPath,
  44. DWORD dwCategoryCount,
  45. DWORD dwTypesSupported )
  46. {
  47. WCHAR wszRegKey[256];
  48. HKEY hKey;
  49. DWORD dwDisposition;
  50. DWORD dwStatus;
  51. StringCbCopyW(wszRegKey, sizeof(wszRegKey), WS_EVENTLOG_KEY);
  52. StringCbCatW(wszRegKey, sizeof(wszRegKey), L"\\");
  53. StringCbCatW(wszRegKey, sizeof(wszRegKey), pwszEventSourceName);
  54. //
  55. // First make sure the event source exists in the registry:
  56. //
  57. dwStatus = RegOpenKeyExW( HKEY_LOCAL_MACHINE,
  58. wszRegKey,
  59. 0,
  60. KEY_READ,
  61. &hKey );
  62. if (dwStatus == ERROR_SUCCESS)
  63. {
  64. //
  65. // Key is already present, so we are Ok, just quit...
  66. //
  67. RegCloseKey(hKey);
  68. return 0;
  69. }
  70. dwStatus = RegCreateKeyExW( HKEY_LOCAL_MACHINE,
  71. wszRegKey,
  72. 0,
  73. NULL,
  74. REG_OPTION_NON_VOLATILE,
  75. KEY_ALL_ACCESS,
  76. NULL,
  77. &hKey,
  78. &dwDisposition );
  79. if (dwStatus != ERROR_SUCCESS)
  80. {
  81. RegCloseKey(hKey);
  82. return dwStatus;
  83. }
  84. dwStatus = RegSetValueExW( hKey,
  85. WSZ_CATEGORY_COUNT,
  86. 0,
  87. REG_DWORD,
  88. (UCHAR*)&dwCategoryCount,
  89. sizeof(DWORD) );
  90. if (dwStatus != ERROR_SUCCESS)
  91. {
  92. RegCloseKey(hKey);
  93. return dwStatus;
  94. }
  95. dwStatus = RegSetValueExW( hKey,
  96. WSZ_TYPES_SUPPORTED,
  97. 0,
  98. REG_DWORD,
  99. (UCHAR*)&dwTypesSupported,
  100. sizeof(DWORD) );
  101. if (dwStatus != ERROR_SUCCESS)
  102. {
  103. RegCloseKey(hKey);
  104. return dwStatus;
  105. }
  106. DWORD dwSize = sizeof(WCHAR)*(1+wcslen(pwszCatalogPath));
  107. dwStatus = RegSetValueExW( hKey,
  108. WSZ_CATEGORY_MESSAGE_FILE,
  109. 0,
  110. REG_EXPAND_SZ,
  111. (UCHAR*)pwszCatalogPath,
  112. dwSize );
  113. if (dwStatus != ERROR_SUCCESS)
  114. {
  115. RegCloseKey(hKey);
  116. return dwStatus;
  117. }
  118. dwStatus = RegSetValueExW( hKey,
  119. WSZ_EVENT_MESSAGE_FILE,
  120. 0,
  121. REG_EXPAND_SZ,
  122. (UCHAR*)pwszCatalogPath,
  123. dwSize );
  124. RegCloseKey(hKey);
  125. return dwStatus;
  126. }
  127. //--------------------------------------------------------------------
  128. // EVENT_LOG::ReportError()
  129. //
  130. //--------------------------------------------------------------------
  131. DWORD EVENT_LOG::ReportError( WORD wCategoryId,
  132. DWORD dwEventId )
  133. {
  134. return ReportError( wCategoryId,
  135. dwEventId,
  136. 0,
  137. NULL,
  138. 0,
  139. NULL );
  140. }
  141. //--------------------------------------------------------------------
  142. // EVENT_LOG::ReportError()
  143. //
  144. //--------------------------------------------------------------------
  145. DWORD EVENT_LOG::ReportError( WORD wCategoryId,
  146. DWORD dwEventId,
  147. DWORD dwValue1 )
  148. {
  149. WCHAR wszValue[20];
  150. WCHAR *pwszValue = (WCHAR*)wszValue;
  151. StringCbPrintfW(wszValue, sizeof(wszValue), L"%d", dwValue1);
  152. return ReportError( wCategoryId,
  153. dwEventId,
  154. 1,
  155. &pwszValue,
  156. 0,
  157. NULL );
  158. }
  159. //--------------------------------------------------------------------
  160. // EVENT_LOG::ReportError()
  161. //
  162. //--------------------------------------------------------------------
  163. DWORD EVENT_LOG::ReportError( WORD wCategoryId,
  164. DWORD dwEventId,
  165. WCHAR *pwszString )
  166. {
  167. if (pwszString)
  168. {
  169. WCHAR **ppwszStrings = &pwszString;
  170. return ReportError( wCategoryId,
  171. dwEventId,
  172. 1,
  173. ppwszStrings,
  174. 0,
  175. NULL );
  176. }
  177. else
  178. {
  179. return ERROR_INVALID_PARAMETER;
  180. }
  181. }
  182. //--------------------------------------------------------------------
  183. // EVENT_LOG::ReportError()
  184. //
  185. //--------------------------------------------------------------------
  186. DWORD EVENT_LOG::ReportError( WORD wCategoryId,
  187. DWORD dwEventId,
  188. WORD wNumStrings,
  189. WCHAR **ppwszStrings )
  190. {
  191. return ReportError( wCategoryId,
  192. dwEventId,
  193. wNumStrings,
  194. ppwszStrings,
  195. 0,
  196. NULL );
  197. }
  198. //--------------------------------------------------------------------
  199. // EVENT_LOG::ReportError()
  200. //
  201. //--------------------------------------------------------------------
  202. DWORD EVENT_LOG::ReportError( WORD wCategoryId,
  203. DWORD dwEventId,
  204. WORD wNumStrings,
  205. WCHAR **ppwszStrings,
  206. DWORD dwDataSize,
  207. VOID *pvData )
  208. {
  209. if (! ::ReportEventW(m_hEventLog,
  210. EVENTLOG_ERROR_TYPE,
  211. wCategoryId, // Message ID for category.
  212. dwEventId, // Message ID for event.
  213. NULL, // pSID (not used).
  214. wNumStrings, // Number of strings.
  215. dwDataSize, // Binary Data Size.
  216. (const WCHAR**)ppwszStrings,
  217. pvData ) ) // Binary Data (none).
  218. {
  219. return GetLastError();
  220. }
  221. else
  222. {
  223. return 0;
  224. }
  225. }
  226. //--------------------------------------------------------------------
  227. // EVENT_LOG::ReportInfo()
  228. //
  229. //--------------------------------------------------------------------
  230. DWORD EVENT_LOG::ReportInfo( WORD wCategoryId,
  231. DWORD dwEventId )
  232. {
  233. if (! ::ReportEventW(m_hEventLog,
  234. EVENTLOG_INFORMATION_TYPE,
  235. wCategoryId, // Message ID for category.
  236. dwEventId, // Message ID for event.
  237. NULL, // pSID (not used).
  238. (WORD)0, // Number of strings.
  239. (DWORD)0, // Binary Data Size.
  240. NULL, // Array of strings.
  241. NULL ) ) // Binary Data (none).
  242. {
  243. return GetLastError();
  244. }
  245. else
  246. {
  247. return 0;
  248. }
  249. }