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.

175 lines
5.3 KiB

  1. // Copyright (c) 1996-1999 Microsoft Corporation
  2. //-----------------------------------------------------------------------------
  3. //
  4. // File: eventlog.cxx
  5. //
  6. // Contents: Utilities to report events.
  7. //
  8. // Histories: 08/06/97 created by weiruc
  9. //
  10. //-----------------------------------------------------------------------------
  11. #include <pch.cxx>
  12. #pragma hdrstop
  13. #include "trklib.hxx"
  14. #include "netevent.h"
  15. #define MAX_STRINGS 100
  16. const extern TCHAR* g_ptszEventSource;
  17. //------------------------------------------------------------------------------
  18. //
  19. // Function: TrkReportRawEvent
  20. //
  21. // Synopsis: Report an event using the event logging service.
  22. // It's "raw" because an lpRawData parameter may be passed.
  23. //
  24. // Input: [in] dwEventId
  25. // Event id as defined in eventmsg.h.
  26. // [in] wType
  27. // Type of event. Choices are:
  28. // EVENTLOG_ERROR_TYPE Error event
  29. // EVENTLOG_WARNING_TYPE Warning event
  30. // EVENTLOG_INFORMATION_TYPE Information event
  31. // EVENTLOG_AUDIT_SUCCESS Success Audit event
  32. // EVENTLOG_AUDIT_FAILURE Failure Audit event
  33. // [in] ...
  34. // Any string the caller wants to log.
  35. //
  36. // Requirement: Because this function does not copy the string parameters
  37. // into internal buffers. So the input strings can not be
  38. // modified when the function is being called.
  39. // The last string parameter passed in MUST be NULL to
  40. // mark the end of the argument list. Any arguments passed
  41. // in after a NULL argument are going to be ignored.
  42. //
  43. //------------------------------------------------------------------------------
  44. HRESULT TrkReportRawEvent(DWORD dwEventId,
  45. WORD wType,
  46. DWORD cbRawData,
  47. const void *pvRawData,
  48. va_list pargs )
  49. {
  50. HANDLE hEventLog = INVALID_HANDLE_VALUE;
  51. HRESULT hr = S_OK;
  52. const TCHAR* rgtszStrings[MAX_STRINGS];
  53. WORD wCounter = 0;
  54. // initialize the insertion string array
  55. memset(rgtszStrings, 0, sizeof(rgtszStrings));
  56. // build the insertion string array
  57. wCounter = 0;
  58. while(TRUE)
  59. {
  60. if(wCounter >= MAX_STRINGS)
  61. {
  62. break;
  63. }
  64. rgtszStrings[wCounter] = va_arg(pargs, const TCHAR*);
  65. if(NULL == rgtszStrings[wCounter])
  66. {
  67. break;
  68. }
  69. else
  70. {
  71. wCounter++;
  72. }
  73. }
  74. va_end(pargs);
  75. // open registry
  76. hEventLog = RegisterEventSource(NULL, g_ptszEventSource);
  77. if(NULL == hEventLog)
  78. {
  79. hr = HRESULT_FROM_WIN32(GetLastError());
  80. TrkLog((TRKDBG_ERROR, TEXT("Can't open registry (%s), hr = %08x"),
  81. g_ptszEventSource, hr));
  82. goto Exit;
  83. }
  84. // write event log
  85. if(!ReportEvent(hEventLog,
  86. wType,
  87. 0,
  88. dwEventId,
  89. NULL,
  90. wCounter,
  91. cbRawData,
  92. rgtszStrings,
  93. const_cast<void*>(pvRawData) ))
  94. {
  95. hr = HRESULT_FROM_WIN32(GetLastError());
  96. TrkLog((TRKDBG_ERROR, TEXT("ReportEvent failed. hr = %08x"), hr));
  97. }
  98. // close registry
  99. if(!DeregisterEventSource(hEventLog) && S_OK == hr)
  100. {
  101. hr = HRESULT_FROM_WIN32(GetLastError());
  102. TrkLog((TRKDBG_ERROR, TEXT("Can't close registry (%s), hr = %08x"),
  103. g_ptszEventSource, hr));
  104. }
  105. Exit:
  106. return hr;
  107. }
  108. //+----------------------------------------------------------------------------
  109. //
  110. // TrkReportInternalError
  111. //
  112. // Report an event that should never happen. The file number and line
  113. // number are put into the hidden data blob.
  114. //
  115. //+----------------------------------------------------------------------------
  116. HRESULT TrkReportInternalError(DWORD dwFileNo,
  117. DWORD dwLineNo,
  118. HRESULT hrErrorNo,
  119. const TCHAR* ptszData)
  120. {
  121. HRESULT hr = S_OK;
  122. TCHAR tszHr[9];
  123. struct
  124. {
  125. DWORD dwFileNo;
  126. DWORD dwLineNo;
  127. TCHAR tszData[ MAX_PATH + 1 ];
  128. } sRawData;
  129. DWORD cbRawData = 0;
  130. sRawData.dwFileNo = dwFileNo;
  131. sRawData.dwLineNo = dwLineNo;
  132. cbRawData = sizeof(sRawData.dwFileNo) + sizeof(sRawData.dwLineNo);
  133. if( NULL != ptszData )
  134. {
  135. _tcsncpy( sRawData.tszData, ptszData, MAX_PATH );
  136. cbRawData += 2 * _tcslen( ptszData );
  137. }
  138. // build the insertion strings
  139. _stprintf(tszHr, TEXT("%08x"), hrErrorNo);
  140. // This is just a special case of TrkReportEvent.
  141. hr = TrkReportRawEventWrapper( EVENT_TRK_INTERNAL_ERROR,
  142. EVENTLOG_ERROR_TYPE,
  143. cbRawData,
  144. reinterpret_cast<void*>(&sRawData),
  145. tszHr, TRKREPORT_LAST_PARAM );
  146. return hr;
  147. }