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.

123 lines
3.6 KiB

  1. //+----------------------------------------------------------------------------
  2. //
  3. // File:eventlog.cpp
  4. //
  5. // Module:
  6. //
  7. // Synopsis: Implement eventlog helper class CEventLog
  8. //
  9. // Copyright (C) Microsoft Corporation. All rights reserved.
  10. //
  11. // Author: Created 9/16/98
  12. //
  13. //+----------------------------------------------------------------------------
  14. #include <windows.h>
  15. #include "debug.h"
  16. #include "eventlog.h"
  17. //+----------------------------------------------------------------------------
  18. //
  19. // Function: CEventLog::Open
  20. //
  21. // Synopsis:
  22. // Register the specified event source.
  23. // Note that the registry entries must already exist.
  24. // HKLM\System\CurrentControlSet\Services\EventLog\Application\<pszEventSource>
  25. // Requires values "EventMessageFile" and "TypesSupported".
  26. //
  27. // Arguments: LPCTSTR lpSourceName - The source name must be a subkey of
  28. // a logfile entry under the EventLog key in the registry
  29. //
  30. // Returns: BOOL - TRUE if succeed
  31. //
  32. // History: Created Header 9/16/98
  33. //
  34. //+----------------------------------------------------------------------------
  35. BOOL CEventLog::Open(LPCWSTR lpSourceName)
  36. {
  37. ASSERT(lpSourceName != NULL);
  38. if (lpSourceName == NULL)
  39. {
  40. return FALSE;
  41. }
  42. ASSERT(m_hEventLog == NULL);
  43. m_hEventLog = ::RegisterEventSource(NULL, // local machine
  44. lpSourceName); // source name
  45. if (m_hEventLog == NULL)
  46. {
  47. TRACE2(("CEventLog::Open %ws failed, LastError = %d"), lpSourceName, GetLastError());
  48. }
  49. return m_hEventLog != NULL;
  50. }
  51. //+----------------------------------------------------------------------------
  52. //
  53. // Function: CEventLog::ReportEvent
  54. //
  55. // Synopsis: Writes an entry at the end of the event log. Support upto 3
  56. // parameters
  57. //
  58. // Arguments: WORD wType - see wType of ::ReportEvent
  59. // DWORD dwEventID - see dwEventID of ::ReportEvent
  60. // const TCHAR* pszS1 - The 1st string, default is NULL
  61. // const TCHAR* pszS2 - The 2st string, default is NULL
  62. // const TCHAR* pszS3 - The 3st string, default is NULL
  63. //
  64. // Returns: BOOL - TRUE is succeed
  65. //
  66. // History: Created Header 9/16/98
  67. //
  68. //+----------------------------------------------------------------------------
  69. BOOL CEventLog::ReportEvent(WORD wType, DWORD dwEventID,
  70. const TCHAR* pszS1,
  71. const TCHAR* pszS2,
  72. const TCHAR* pszS3)
  73. {
  74. //
  75. // Set up an array of strings
  76. //
  77. const TCHAR* arString[3] = {pszS1, pszS2, pszS3};
  78. int iNumString = 0; // number of parameters
  79. for (iNumString = 0; iNumString < 3; iNumString++)
  80. {
  81. if (arString[iNumString] == NULL)
  82. {
  83. break;
  84. }
  85. }
  86. ASSERT(m_hEventLog);
  87. if (m_hEventLog == NULL)
  88. {
  89. return FALSE;
  90. }
  91. BOOL fSucceed = ::ReportEvent(m_hEventLog,
  92. wType,
  93. 0, // event category
  94. dwEventID,
  95. NULL, // user security identifier
  96. (WORD) iNumString,// number of strings to merge with message
  97. 0, // size of binary data
  98. arString, // array of strings to merge with message
  99. NULL); // address of binary data
  100. if (!fSucceed)
  101. {
  102. TRACE2(("CEventLog::ReportEvent failed for event id %d, LastError = %d"),
  103. dwEventID, GetLastError());
  104. }
  105. return fSucceed;
  106. }