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.

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