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.

194 lines
3.9 KiB

  1. /*++
  2. Copyright (c) 1998 Microsoft Corporation
  3. Module Name:
  4. eventlog.cpp
  5. Abstract:
  6. SIS Groveler eventlog interface
  7. Authors:
  8. John Douceur, 1998
  9. Environment:
  10. User Mode
  11. Revision History:
  12. --*/
  13. #include "all.hxx"
  14. #ifndef MIN_MESSAGE_SEVERITY
  15. #define MIN_MESSAGE_SEVERITY 0
  16. #endif
  17. const _TCHAR *
  18. EventLog::service_name = _T("Groveler");
  19. const _TCHAR *
  20. EventLog::message_filename = _T("%SystemRoot%\\System32\\grovmsg.dll");
  21. static const _TCHAR *
  22. registry_log_path =
  23. _T("SYSTEM\\CurrentControlSet\\Services\\EventLog\\Application\\");
  24. const DWORD
  25. EventLog::types_supported =
  26. EVENTLOG_INFORMATION_TYPE |
  27. EVENTLOG_WARNING_TYPE |
  28. EVENTLOG_ERROR_TYPE;
  29. EventLog::EventLog()
  30. {
  31. ASSERT(this != 0);
  32. setup_registry();
  33. event_source_handle = RegisterEventSource(0, service_name);
  34. if (event_source_handle == 0)
  35. {
  36. DWORD err = GetLastError();
  37. PRINT_DEBUG_MSG((_T("GROVELER: RegisterEventSource() failed with error %d\n"),
  38. err));
  39. }
  40. }
  41. EventLog::~EventLog()
  42. {
  43. ASSERT(this != 0);
  44. if (event_source_handle != 0)
  45. {
  46. int ok = DeregisterEventSource(event_source_handle);
  47. if (!ok)
  48. {
  49. DWORD err = GetLastError();
  50. PRINT_DEBUG_MSG((_T("GROVELER: DeregisterEventSource() failed with error %d\n"),
  51. err));
  52. }
  53. }
  54. }
  55. bool
  56. EventLog::report_event(
  57. DWORD event_id,
  58. int string_count,
  59. // _TCHAR *string
  60. ...)
  61. {
  62. ASSERT(this != 0);
  63. ASSERT(event_source_handle != 0);
  64. ASSERT(string_count >= 0);
  65. if (event_source_handle == 0)
  66. {
  67. return false;
  68. }
  69. DWORD message_severity = MESSAGE_SEVERITY(event_id);
  70. if (message_severity < MIN_MESSAGE_SEVERITY)
  71. {
  72. return false;
  73. }
  74. const _TCHAR **strings = 0;
  75. if (string_count > 0)
  76. {
  77. strings = new const _TCHAR *[string_count];
  78. va_list ap;
  79. va_start(ap, string_count);
  80. for (int index = 0; index < string_count; index++)
  81. {
  82. strings[index] = va_arg(ap, _TCHAR *);
  83. ASSERT(strings[index] != 0);
  84. }
  85. va_end(ap);
  86. }
  87. WORD event_type = 0;
  88. switch (message_severity)
  89. {
  90. case MESSAGE_SEVERITY_SUCCESS:
  91. event_type = EVENTLOG_AUDIT_SUCCESS;
  92. break;
  93. case MESSAGE_SEVERITY_INFORMATIONAL:
  94. event_type = EVENTLOG_INFORMATION_TYPE;
  95. break;
  96. case MESSAGE_SEVERITY_WARNING:
  97. event_type = EVENTLOG_WARNING_TYPE;
  98. break;
  99. case MESSAGE_SEVERITY_ERROR:
  100. event_type = EVENTLOG_ERROR_TYPE;
  101. break;
  102. default:
  103. ASSERT(false);
  104. }
  105. BOOL ok = ReportEvent(event_source_handle, event_type, 0, event_id,
  106. 0, (WORD)string_count, 0, strings, 0);
  107. if (!ok)
  108. {
  109. DWORD err = GetLastError();
  110. PRINT_DEBUG_MSG((_T("GROVELER: ReportEvent() failed with error %d\n"), err));
  111. }
  112. if (strings != 0)
  113. {
  114. ASSERT(string_count > 0);
  115. delete[] strings;
  116. strings = 0;
  117. }
  118. return (ok != 0);
  119. }
  120. bool
  121. EventLog::setup_registry()
  122. {
  123. _TCHAR *log_path = 0;
  124. try
  125. {
  126. HKEY path_key = 0;
  127. DWORD disp;
  128. log_path =
  129. new _TCHAR[_tcslen(registry_log_path) + _tcslen(service_name) + 1];
  130. _stprintf(log_path, _T("%s%s"), registry_log_path, service_name);
  131. Registry::create_key_ex(HKEY_LOCAL_MACHINE, log_path, 0, 0,
  132. REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, 0, &path_key, &disp);
  133. ASSERT(log_path != 0);
  134. delete[] log_path;
  135. log_path = 0;
  136. try
  137. {
  138. Registry::set_value_ex(path_key, _T("EventMessageFile"), 0,
  139. REG_EXPAND_SZ, (BYTE *)message_filename,
  140. (_tcslen(message_filename) + 1) * sizeof(_TCHAR));
  141. Registry::set_value_ex(path_key, _T("TypesSupported"), 0,
  142. REG_DWORD, (BYTE *)&types_supported, sizeof(DWORD));
  143. }
  144. catch (DWORD result)
  145. {
  146. ASSERT(result != ERROR_SUCCESS);
  147. PRINT_DEBUG_MSG((_T("GROVELER: Registry::set_value_ex() failed with error %d\n"),
  148. result));
  149. ASSERT(path_key != 0);
  150. Registry::close_key(path_key);
  151. path_key = 0;
  152. return false;
  153. }
  154. ASSERT(path_key != 0);
  155. Registry::close_key(path_key);
  156. path_key = 0;
  157. }
  158. catch (DWORD result)
  159. {
  160. if (log_path != 0)
  161. {
  162. delete[] log_path;
  163. log_path = 0;
  164. }
  165. ASSERT(result != ERROR_SUCCESS);
  166. PRINT_DEBUG_MSG((_T("GROVELER: Registry::create_key_ex() or Registry::close_key() ")
  167. _T("failed with error %d\n"), result));
  168. return false;
  169. }
  170. return true;
  171. }