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.

214 lines
5.2 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. DWORD status,
  59. int string_count,
  60. // _TCHAR *string
  61. ...)
  62. {
  63. WCHAR *strings[8];
  64. WCHAR statusStr[32];
  65. ASSERT(this != 0);
  66. ASSERT(event_source_handle != 0);
  67. ASSERT(string_count >= 0);
  68. if (event_source_handle == 0)
  69. {
  70. return false;
  71. }
  72. DWORD message_severity = MESSAGE_SEVERITY(event_id);
  73. #if (MIN_MESSAGE_SEVERITY > 0)
  74. if (message_severity < MIN_MESSAGE_SEVERITY)
  75. {
  76. return false;
  77. }
  78. #endif
  79. //
  80. // Generate the list of strings buffer
  81. //
  82. ASSERT((string_count+1) < (sizeof(strings)/sizeof(WCHAR *)));
  83. va_list ap;
  84. va_start(ap, string_count);
  85. for (int index = 0; index < string_count; index++)
  86. {
  87. strings[index] = va_arg(ap, _TCHAR *);
  88. ASSERT(strings[index] != 0);
  89. }
  90. va_end(ap);
  91. //
  92. // We are going to always insert the status string at the end of the
  93. // list.
  94. (void)StringCbPrintf(statusStr,sizeof(statusStr),L"%d",status);
  95. strings[index] = statusStr;
  96. WORD event_type = 0;
  97. switch (message_severity)
  98. {
  99. case MESSAGE_SEVERITY_SUCCESS:
  100. event_type = EVENTLOG_AUDIT_SUCCESS;
  101. break;
  102. case MESSAGE_SEVERITY_INFORMATIONAL:
  103. event_type = EVENTLOG_INFORMATION_TYPE;
  104. break;
  105. case MESSAGE_SEVERITY_WARNING:
  106. event_type = EVENTLOG_WARNING_TYPE;
  107. break;
  108. case MESSAGE_SEVERITY_ERROR:
  109. event_type = EVENTLOG_ERROR_TYPE;
  110. break;
  111. default:
  112. ASSERT(false);
  113. }
  114. //
  115. // Report the events. We always add one to the string count because
  116. // we always put the status string at the end of the buffer.
  117. //
  118. BOOL ok = ReportEvent(event_source_handle, event_type, 0, event_id,
  119. 0, (WORD)(string_count+1), 0, (LPCWSTR *)strings, 0);
  120. if (!ok)
  121. {
  122. DWORD err = GetLastError();
  123. PRINT_DEBUG_MSG((_T("GROVELER: ReportEvent() failed with error %d\n"), err));
  124. }
  125. return (ok != 0);
  126. }
  127. bool
  128. EventLog::setup_registry()
  129. {
  130. _TCHAR *log_path = 0;
  131. try
  132. {
  133. HKEY path_key = 0;
  134. DWORD disp;
  135. int bufSize = _tcslen(registry_log_path) + _tcslen(service_name) + 1;
  136. log_path = new _TCHAR[bufSize];
  137. (void)StringCchPrintf(log_path, bufSize, _T("%s%s"), registry_log_path, service_name);
  138. Registry::create_key_ex(HKEY_LOCAL_MACHINE, log_path, 0, 0,
  139. REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, 0, &path_key, &disp);
  140. ASSERT(log_path != 0);
  141. delete[] log_path;
  142. log_path = 0;
  143. try
  144. {
  145. Registry::set_value_ex(path_key, _T("EventMessageFile"), 0,
  146. REG_EXPAND_SZ, (BYTE *)message_filename,
  147. (_tcslen(message_filename) + 1) * sizeof(_TCHAR));
  148. Registry::set_value_ex(path_key, _T("TypesSupported"), 0,
  149. REG_DWORD, (BYTE *)&types_supported, sizeof(DWORD));
  150. }
  151. catch (DWORD result)
  152. {
  153. ASSERT(result != ERROR_SUCCESS);
  154. PRINT_DEBUG_MSG((_T("GROVELER: Registry::set_value_ex() failed with error %d\n"),
  155. result));
  156. ASSERT(path_key != 0);
  157. Registry::close_key(path_key);
  158. path_key = 0;
  159. return false;
  160. }
  161. ASSERT(path_key != 0);
  162. Registry::close_key(path_key);
  163. path_key = 0;
  164. }
  165. catch (DWORD result)
  166. {
  167. if (log_path != 0)
  168. {
  169. delete[] log_path;
  170. log_path = 0;
  171. }
  172. ASSERT(result != ERROR_SUCCESS);
  173. PRINT_DEBUG_MSG((_T("GROVELER: Registry::create_key_ex() or Registry::close_key() ")
  174. _T("failed with error %d\n"), result));
  175. return false;
  176. }
  177. return true;
  178. }