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.

211 lines
6.2 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1992 - 1994.
  5. //
  6. // File: eventlog.cxx
  7. //
  8. // Contents: CEventLog class
  9. //
  10. // History: 07-Jun-94 DwightKr Created
  11. //
  12. //--------------------------------------------------------------------------
  13. #include <pch.cxx>
  14. #pragma hdrstop
  15. #include <ciregkey.hxx>
  16. #include <cievtmsg.h>
  17. #include <pstore.hxx>
  18. #include <eventlog.hxx>
  19. #include <alocdbg.hxx>
  20. //+-------------------------------------------------------------------------
  21. //
  22. // Member: CEventItem::CEventItem
  23. //
  24. // Synopsis: consturcts a CEventItem object.
  25. //
  26. // Arguments: [fType ] - Type of event
  27. // [fCategory] - Event Category
  28. // [eventId] - Message file event identifier
  29. // [cArgs] - Number of substitution arguments to be added
  30. // [cbData ] - number of bytes in supplemental raw data.
  31. // [data ] - pointer to block of supplemental data.
  32. //
  33. // History: 12-30-96 mohamedn raw data
  34. //--------------------------------------------------------------------------
  35. CEventItem::CEventItem( WORD fType,
  36. WORD fCategory,
  37. DWORD eventId,
  38. WORD cArgs,
  39. DWORD dataSize,
  40. const void * data ) :
  41. _fType(fType),
  42. _fCategory(fCategory),
  43. _eventId(eventId),
  44. _cArgsTotal(cArgs),
  45. _pwcsData(0),
  46. _cArgsUsed(0),
  47. _dataSize(dataSize),
  48. _data(data)
  49. {
  50. _pwcsData = new WCHAR *[_cArgsTotal];
  51. memset( _pwcsData, 0, sizeof(WCHAR *) * _cArgsTotal );
  52. END_CONSTRUCTION(CEventItem);
  53. }
  54. //+-------------------------------------------------------------------------
  55. //
  56. //--------------------------------------------------------------------------
  57. CEventItem::~CEventItem()
  58. {
  59. for (WORD i=0; i<_cArgsTotal; i++)
  60. delete [] _pwcsData[i];
  61. delete [] _pwcsData;
  62. }
  63. //+-------------------------------------------------------------------------
  64. //
  65. //--------------------------------------------------------------------------
  66. void CEventItem::AddArg( const WCHAR * wcsString )
  67. {
  68. Win4Assert( _cArgsUsed < _cArgsTotal );
  69. Win4Assert( 0 != wcsString );
  70. _pwcsData[_cArgsUsed] = new WCHAR[wcslen(wcsString) + 1];
  71. wcscpy( _pwcsData[_cArgsUsed], wcsString );
  72. _cArgsUsed++;
  73. }
  74. //+-------------------------------------------------------------------------
  75. //
  76. //--------------------------------------------------------------------------
  77. void CEventItem::AddArg( const CHAR * pszString )
  78. {
  79. Win4Assert( _cArgsUsed < _cArgsTotal );
  80. Win4Assert( 0 != pszString );
  81. _pwcsData[_cArgsUsed] = new WCHAR[ strlen(pszString) + 1];
  82. mbstowcs(_pwcsData[_cArgsUsed], pszString, strlen(pszString) + 1);
  83. _cArgsUsed++;
  84. }
  85. //+-------------------------------------------------------------------------
  86. //
  87. //--------------------------------------------------------------------------
  88. void CEventItem::AddArg( const ULONG ulValue )
  89. {
  90. WCHAR wcsBuffer[20];
  91. _ultow( ulValue, wcsBuffer, 10 );
  92. AddArg(wcsBuffer);
  93. }
  94. //+---------------------------------------------------------------------------
  95. //
  96. // Member: CEventItem::AddError
  97. //
  98. // Synopsis: Adds the given error either as a HEX value or a decimal
  99. // value depending upon whether it is an NT error or WIN32 error.
  100. //
  101. // Arguments: [ulValue] - The error code to add.
  102. //
  103. // History: 5-27-96 srikants Created
  104. //
  105. // Notes:
  106. //
  107. //----------------------------------------------------------------------------
  108. void CEventItem::AddError( ULONG ulValue )
  109. {
  110. if ( !NT_SUCCESS(ulValue) )
  111. {
  112. WCHAR wszTemp[20];
  113. swprintf( wszTemp, L"0x%X", ulValue );
  114. AddArg( wszTemp );
  115. }
  116. else
  117. {
  118. AddArg( ulValue );
  119. }
  120. }
  121. //+-------------------------------------------------------------------------
  122. //
  123. // Method: CEventLog::CEventLog, public
  124. //
  125. // Purpose: Constructor
  126. //
  127. // History: 07-Jun-94 DwightKr Created
  128. //
  129. //--------------------------------------------------------------------------
  130. CEventLog::CEventLog( const WCHAR * wcsUNCServer, const WCHAR * wcsSource) : _hEventLog(0)
  131. {
  132. _hEventLog = RegisterEventSource( wcsUNCServer, wcsSource );
  133. if ( _hEventLog == NULL )
  134. {
  135. ciDebugOut( (DEB_ITRACE, "CEventLog: Could not register event source, rc=0x%x\n", GetLastError() ));
  136. THROW( CException() );
  137. }
  138. END_CONSTRUCTION( CEventLog );
  139. }
  140. //+-------------------------------------------------------------------------
  141. //
  142. // Method: CEventLog::~CEventLog, public
  143. //
  144. // Purpose: Destructor
  145. //
  146. // History: 07-Jun-94 DwightKr Created
  147. //
  148. //--------------------------------------------------------------------------
  149. CEventLog::~CEventLog()
  150. {
  151. DeregisterEventSource( _hEventLog );
  152. }
  153. //+-------------------------------------------------------------------------
  154. //
  155. // Method: CEventLog::ReportEvent
  156. //
  157. // Purpose: Allows writing records to event log
  158. //
  159. // History: 07-Jun-94 DwightKr Created
  160. // 31-Dec-96 mohamedn Added support for raw data in ReportEvent
  161. //
  162. //--------------------------------------------------------------------------
  163. void CEventLog::ReportEvent( CEventItem & event )
  164. {
  165. Win4Assert( event._cArgsTotal == event._cArgsUsed );
  166. //
  167. // No need to throw if ReportEvent returns an error because logging
  168. // is a low priority operation
  169. //
  170. if ( !::ReportEvent( _hEventLog,
  171. event._fType,
  172. event._fCategory,
  173. event._eventId,
  174. NULL, // Sid
  175. event._cArgsUsed,
  176. event._dataSize, // Sizeof RAW data
  177. (const WCHAR **) event._pwcsData,
  178. (void *)event._data )) // RAW data
  179. {
  180. ciDebugOut(( DEB_ERROR, "Error 0x%X while writing to eventlog\n",
  181. GetLastError() ));
  182. }
  183. }