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.

197 lines
5.9 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 ULONG ulValue )
  78. {
  79. WCHAR wcsBuffer[20];
  80. _ultow( ulValue, wcsBuffer, 10 );
  81. AddArg(wcsBuffer);
  82. }
  83. //+---------------------------------------------------------------------------
  84. //
  85. // Member: CEventItem::AddError
  86. //
  87. // Synopsis: Adds the given error either as a HEX value or a decimal
  88. // value depending upon whether it is an NT error or WIN32 error.
  89. //
  90. // Arguments: [ulValue] - The error code to add.
  91. //
  92. // History: 5-27-96 srikants Created
  93. //
  94. // Notes:
  95. //
  96. //----------------------------------------------------------------------------
  97. void CEventItem::AddError( ULONG ulValue )
  98. {
  99. if ( !NT_SUCCESS(ulValue) )
  100. {
  101. WCHAR wszTemp[20];
  102. swprintf( wszTemp, L"0x%X", ulValue );
  103. AddArg( wszTemp );
  104. }
  105. else
  106. {
  107. AddArg( ulValue );
  108. }
  109. }
  110. //+-------------------------------------------------------------------------
  111. //
  112. // Method: CEventLog::CEventLog, public
  113. //
  114. // Purpose: Constructor
  115. //
  116. // History: 07-Jun-94 DwightKr Created
  117. //
  118. //--------------------------------------------------------------------------
  119. CEventLog::CEventLog( const WCHAR * wcsUNCServer, const WCHAR * wcsSource) : _hEventLog(0)
  120. {
  121. _hEventLog = RegisterEventSource( wcsUNCServer, wcsSource );
  122. if ( _hEventLog == NULL )
  123. {
  124. ciDebugOut( (DEB_ITRACE, "CEventLog: Could not register event source, rc=0x%x\n", GetLastError() ));
  125. THROW( CException() );
  126. }
  127. END_CONSTRUCTION( CEventLog );
  128. }
  129. //+-------------------------------------------------------------------------
  130. //
  131. // Method: CEventLog::~CEventLog, public
  132. //
  133. // Purpose: Destructor
  134. //
  135. // History: 07-Jun-94 DwightKr Created
  136. //
  137. //--------------------------------------------------------------------------
  138. CEventLog::~CEventLog()
  139. {
  140. DeregisterEventSource( _hEventLog );
  141. }
  142. //+-------------------------------------------------------------------------
  143. //
  144. // Method: CEventLog::ReportEvent
  145. //
  146. // Purpose: Allows writing records to event log
  147. //
  148. // History: 07-Jun-94 DwightKr Created
  149. // 31-Dec-96 mohamedn Added support for raw data in ReportEvent
  150. //
  151. //--------------------------------------------------------------------------
  152. void CEventLog::ReportEvent( CEventItem & event )
  153. {
  154. Win4Assert( event._cArgsTotal == event._cArgsUsed );
  155. //
  156. // No need to throw if ReportEvent returns an error because logging
  157. // is a low priority operation
  158. //
  159. if ( !::ReportEvent( _hEventLog,
  160. event._fType,
  161. event._fCategory,
  162. event._eventId,
  163. NULL, // Sid
  164. event._cArgsUsed,
  165. event._dataSize, // Sizeof RAW data
  166. (const WCHAR **) event._pwcsData,
  167. (void *)event._data )) // RAW data
  168. {
  169. ciDebugOut(( DEB_ERROR, "Error 0x%X while writing to eventlog\n",
  170. GetLastError() ));
  171. }
  172. }