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.

113 lines
3.3 KiB

  1. /* Copyright 1999 American Power Conversion, All Rights Reserved
  2. *
  3. * Description:
  4. * The file implements EventLogger. The EventLogger is reponsible
  5. * for logging information to the NT machine's System Event Log.
  6. *
  7. *
  8. * Revision History:
  9. * sberard 29Mar1999 initial revision.
  10. *
  11. */
  12. #include "eventlog.h"
  13. #ifdef __cplusplus
  14. extern "C" {
  15. #endif
  16. static HANDLE _theUpsEventLogger = NULL;
  17. /**
  18. * LogEvent
  19. *
  20. * Description:
  21. * This function is responsible for logging information to the NT machine's
  22. * System Event log. The Event to log is specified by the parameter
  23. * anEventId, which is defined in the file lmerrlog.h. The anInfoStr
  24. * parameter is used to specify additional information to be merged with
  25. * the Event message.
  26. *
  27. * Parameters:
  28. * anEventId - the id of the Event to log
  29. * anInfoStr - additional information to merge with the message
  30. * or NULL if there is no additional information.
  31. * anErrVal - the error code as reported by GetLastError().
  32. *
  33. * Returns:
  34. * TRUE - if the Event was logged successfully
  35. * FALSE - if there was an error logging the Event
  36. *
  37. */
  38. BOOL LogEvent(DWORD anEventId, LPTSTR anInfoStr, DWORD anErrVal) {
  39. BOOL ret_val = FALSE;
  40. WORD event_type; // type of event
  41. LPTSTR info_strings[1]; // array of strings to merge with the message
  42. WORD num_strings; // count of insertion strings
  43. LPTSTR *ptr_strings; // pointer to array of insertion strings
  44. DWORD data_size; // count of data (in bytes)
  45. LPVOID ptr_data; // pointer to data
  46. if (_theUpsEventLogger == NULL) {
  47. _theUpsEventLogger = RegisterEventSource(NULL, SZSERVICENAME);
  48. }
  49. if (_theUpsEventLogger == NULL) {
  50. return(FALSE);
  51. }
  52. if (anEventId > ERRLOG_BASE) {
  53. event_type = EVENTLOG_WARNING_TYPE;
  54. }
  55. else {
  56. event_type = EVENTLOG_ERROR_TYPE;
  57. }
  58. // If the error value is anything other than ERROR_SUCCESS, add it
  59. // to the Event
  60. if (anErrVal == ERROR_SUCCESS) {
  61. ptr_data = NULL;
  62. data_size = 0;
  63. } else {
  64. ptr_data = &anErrVal;
  65. data_size = sizeof(anErrVal);
  66. }
  67. // Append any additional strings to the Event message.
  68. if (anInfoStr == NULL) {
  69. ptr_strings = NULL;
  70. num_strings = 0;
  71. } else {
  72. info_strings[0] = anInfoStr;
  73. ptr_strings = info_strings;
  74. num_strings = 1;
  75. }
  76. // Log the Event to the System Event log
  77. if (ReportEvent(
  78. _theUpsEventLogger, // handle
  79. event_type, // event type
  80. 0, // event category,
  81. anEventId, // message id
  82. NULL, // user id
  83. num_strings, // number of strings
  84. data_size, // number of data bytes
  85. ptr_strings, // array of strings
  86. ptr_data // data buffer
  87. )) {
  88. ret_val = TRUE;
  89. }
  90. else {
  91. // an error occured, return FALSE
  92. ret_val = FALSE;
  93. }
  94. return ret_val;
  95. }
  96. #ifdef __cplusplus
  97. }
  98. #endif