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.

103 lines
3.2 KiB

  1. //
  2. // MODULE: Event.cpp
  3. //
  4. // PURPOSE: Fully implements class CEvent: Event Logging
  5. //
  6. // PROJECT: Generic Troubleshooter DLL for Microsoft AnswerPoint
  7. //
  8. // COMPANY: Saltmine Creative, Inc. (206)-284-7511 [email protected]
  9. //
  10. // AUTHOR: Roman Mach
  11. //
  12. // ORIGINAL DATE: 8-2-96
  13. //
  14. // NOTES:
  15. //
  16. // Version Date By Comments
  17. //--------------------------------------------------------------------
  18. // V3.0 9/18/98 JM Abstracted as a class. Previously, global.
  19. //
  20. //////////////////////////////////////////////////////////////////////
  21. #include "stdafx.h"
  22. #include "Event.h"
  23. bool CEvent::s_bUseEventLog = false;// Online Troubleshooter, will promptly set this
  24. // true in DLLMain. For Local Troubleshooter,
  25. // we leave this false.
  26. bool CEvent::s_bLogAll = true; // can be changed by RegistryMonitor
  27. CAbstractCounter * const CEvent::s_pcountErrors = &(g_ApgtsCounters.m_LoggedErrors);
  28. inline WORD evtype(DWORD e) {return (1 << (3 - ((e >> 30))));}
  29. inline WORD evcode(DWORD e) {return (WORD)(e & 0xFFFF);}
  30. /*static*/ void CEvent::SetUseEventLog(bool bUseEventLog)
  31. {
  32. s_bUseEventLog = bUseEventLog;
  33. }
  34. // ReportWFEvent (Based on Microsoft code)
  35. //
  36. // report an event to the NT event watcher
  37. // pass 1, 2 or 3 strings
  38. //
  39. // no return value
  40. // NOTE: inefficient: could RegisterEventSource in DLLMain code for DLL_PROCESS_ATTACH
  41. // & unregister in DLL_PROCESS_DETACH. Then could use handle as a global.
  42. /* static */ void CEvent::ReportWFEvent(
  43. LPCTSTR string1, // if there is a throw, this is throw file & line
  44. // otherwise, file and line where ReportWFEvent is called
  45. LPCTSTR string2, // always file and line where ReportWFEvent is called
  46. LPCTSTR string3, // use may differ for different log entries
  47. LPCTSTR string4, // use may differ for different log entries
  48. DWORD eventID)
  49. {
  50. if (!s_bUseEventLog)
  51. return;
  52. HANDLE hEvent;
  53. LPCTSTR pszaStrings[4];
  54. WORD cStrings;
  55. WORD type = evtype(eventID);
  56. WORD code = evcode(eventID);
  57. if (s_bLogAll
  58. || type == EVENTLOG_ERROR_TYPE
  59. || type == EVENTLOG_WARNING_TYPE
  60. || code == EV_GTS_PROCESS_START
  61. || code == EV_GTS_PROCESS_STOP)
  62. {
  63. cStrings = 0;
  64. if ((pszaStrings[0] = string1) && (string1[0]))
  65. cStrings = 1;
  66. if ((pszaStrings[1] = string2) && (string2[0]))
  67. cStrings = 2;
  68. if ((pszaStrings[2] = string3) && (string3[0]))
  69. cStrings = 3;
  70. if ((pszaStrings[3] = string4) && (string4[0]))
  71. cStrings = 4;
  72. if (cStrings == 0)
  73. return;
  74. hEvent = ::RegisterEventSource(
  75. NULL, // server name for source (NULL means this computer)
  76. REG_EVT_ITEM_STR); // source name for registered handle
  77. if (hEvent)
  78. {
  79. ::ReportEvent(hEvent, // handle returned by RegisterEventSource
  80. type, // event type to log
  81. 0, // event category
  82. eventID, // event identifier
  83. 0, // user security identifier (optional)
  84. cStrings, // number of strings to merge with message
  85. 0, // size of binary data, in bytes
  86. (LPCTSTR *)pszaStrings, // array of strings to merge with message
  87. NULL); // address of binary data
  88. ::DeregisterEventSource(hEvent);
  89. }
  90. if (evtype(eventID) == EVENTLOG_ERROR_TYPE)
  91. s_pcountErrors->Increment();
  92. }
  93. }