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.

109 lines
3.1 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 (anEventId > ERRLOG_BASE) {
  50. event_type = EVENTLOG_WARNING_TYPE;
  51. }
  52. else {
  53. event_type = EVENTLOG_ERROR_TYPE;
  54. }
  55. // If the error value is anything other than ERROR_SUCCESS, add it
  56. // to the Event
  57. if (anErrVal == ERROR_SUCCESS) {
  58. ptr_data = NULL;
  59. data_size = 0;
  60. } else {
  61. ptr_data = &anErrVal;
  62. data_size = sizeof(anErrVal);
  63. }
  64. // Append any additional strings to the Event message.
  65. if (anInfoStr == NULL) {
  66. ptr_strings = NULL;
  67. num_strings = 0;
  68. } else {
  69. info_strings[0] = anInfoStr;
  70. ptr_strings = info_strings;
  71. num_strings = 1;
  72. }
  73. // Log the Event to the System Event log
  74. if (ReportEvent(
  75. _theUpsEventLogger, // handle
  76. event_type, // event type
  77. 0, // event category,
  78. anEventId, // message id
  79. NULL, // user id
  80. num_strings, // number of strings
  81. data_size, // number of data bytes
  82. ptr_strings, // array of strings
  83. ptr_data // data buffer
  84. )) {
  85. ret_val = TRUE;
  86. }
  87. else {
  88. // an error occured, return FALSE
  89. ret_val = FALSE;
  90. }
  91. return ret_val;
  92. }
  93. #ifdef __cplusplus
  94. }
  95. #endif