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.

454 lines
11 KiB

  1. //////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright (c) 2000-2002 Microsoft Corporation
  4. //
  5. // Module Name:
  6. // Logger.cpp
  7. //
  8. // Description:
  9. // ClCfgSrv Logger implementation.
  10. //
  11. // Maintained By:
  12. // David Potter (DavidP) 11-DEC-2000
  13. //
  14. //////////////////////////////////////////////////////////////////////////////
  15. #include "Pch.h"
  16. #include "Logger.h"
  17. DEFINE_THISCLASS("CClCfgSrvLogger")
  18. //****************************************************************************
  19. //
  20. // Constructor / Destructor
  21. //
  22. //****************************************************************************
  23. //////////////////////////////////////////////////////////////////////////////
  24. //++
  25. //
  26. // CClCfgSrvLogger::S_HrCreateInstance
  27. //
  28. // Description:
  29. // Create an instance of this object.
  30. //
  31. // Arguments:
  32. // ppunkOut - IUnknown pointer to be returned.
  33. //
  34. // Return Values:
  35. // S_OK - Operation completed successfully.
  36. // E_OUTOFMEMORY - Error allocating memory.
  37. // Other HRESULTs.
  38. //
  39. //--
  40. //////////////////////////////////////////////////////////////////////////////
  41. HRESULT
  42. CClCfgSrvLogger::S_HrCreateInstance(
  43. IUnknown ** ppunkOut
  44. )
  45. {
  46. TraceFunc( "" );
  47. HRESULT hr = S_OK;
  48. CClCfgSrvLogger * pccsl = NULL;
  49. Assert( ppunkOut != NULL );
  50. if ( ppunkOut == NULL )
  51. {
  52. hr = THR( E_POINTER );
  53. goto Cleanup;
  54. } // if:
  55. pccsl = new CClCfgSrvLogger();
  56. if ( pccsl == NULL )
  57. {
  58. hr = THR( E_OUTOFMEMORY );
  59. goto Cleanup;
  60. } // if: error allocating object
  61. hr = THR( pccsl->HrInit() );
  62. if ( FAILED( hr ) )
  63. {
  64. goto Cleanup;
  65. } // if: HrInit() failed
  66. hr = THR( pccsl->TypeSafeQI( IUnknown, ppunkOut ) );
  67. if ( FAILED( hr ) )
  68. {
  69. goto Cleanup;
  70. }
  71. Cleanup:
  72. if ( pccsl != NULL )
  73. {
  74. pccsl->Release();
  75. } // if:
  76. HRETURN( hr );
  77. } //*** CClCfgSrvLogger::S_HrCreateInstance
  78. //////////////////////////////////////////////////////////////////////////////
  79. //++
  80. //
  81. // CClCfgSrvLogger::CClCfgSrvLogger
  82. //
  83. //--
  84. //////////////////////////////////////////////////////////////////////////////
  85. CClCfgSrvLogger::CClCfgSrvLogger( void )
  86. : m_cRef( 1 )
  87. {
  88. TraceFunc( "" );
  89. InterlockedIncrement( &g_cObjects );
  90. TraceFuncExit();
  91. } //*** CClCfgSrvLogger::CClCfgSrvLogger
  92. //////////////////////////////////////////////////////////////////////////////
  93. //
  94. // STDMETHODIMP
  95. // CClCfgSrvLogger::HrInit
  96. //
  97. //////////////////////////////////////////////////////////////////////////////
  98. STDMETHODIMP
  99. CClCfgSrvLogger::HrInit( void )
  100. {
  101. TraceFunc( "" );
  102. HRESULT hr = S_OK;
  103. Assert( m_cRef == 1 );
  104. // Open the log file.
  105. hr = THR( HrLogOpen() );
  106. if ( FAILED( hr ) )
  107. {
  108. goto Cleanup;
  109. } // if:
  110. // Release the critical section on the log file.
  111. hr = THR( HrLogRelease() );
  112. if ( FAILED( hr ) )
  113. {
  114. goto Cleanup;
  115. } // if:
  116. Cleanup:
  117. HRETURN( hr );
  118. } //*** CClCfgSrvLogger::HrInit
  119. //////////////////////////////////////////////////////////////////////////////
  120. //++
  121. //
  122. // CClCfgSrvLogger::~CClCfgSrvLogger
  123. //
  124. //--
  125. //////////////////////////////////////////////////////////////////////////////
  126. CClCfgSrvLogger::~CClCfgSrvLogger( void )
  127. {
  128. TraceFunc( "" );
  129. Assert( m_cRef == 0 );
  130. // Close the log file.
  131. THR( HrLogClose() );
  132. InterlockedDecrement( &g_cObjects );
  133. TraceFuncExit();
  134. } //*** CClCfgSrvLogger::~CClCfgSrvLogger
  135. //****************************************************************************
  136. //
  137. // IUnknown
  138. //
  139. //****************************************************************************
  140. //////////////////////////////////////////////////////////////////////////////
  141. //++
  142. //
  143. // CClCfgSrvLogger::QueryInterface
  144. //
  145. // Description:
  146. // Query this object for the passed in interface.
  147. //
  148. // Arguments:
  149. // riidIn
  150. // Id of interface requested.
  151. //
  152. // ppvOut
  153. // Pointer to the requested interface.
  154. //
  155. // Return Value:
  156. // S_OK
  157. // If the interface is available on this object.
  158. //
  159. // E_NOINTERFACE
  160. // If the interface is not available.
  161. //
  162. // E_POINTER
  163. // ppvOut was NULL.
  164. //
  165. // Remarks:
  166. // None.
  167. //
  168. //--
  169. //////////////////////////////////////////////////////////////////////////////
  170. STDMETHODIMP
  171. CClCfgSrvLogger::QueryInterface(
  172. REFIID riidIn
  173. , LPVOID * ppvOut
  174. )
  175. {
  176. TraceQIFunc( riidIn, ppvOut );
  177. HRESULT hr = S_OK;
  178. //
  179. // Validate arguments.
  180. //
  181. Assert( ppvOut != NULL );
  182. if ( ppvOut == NULL )
  183. {
  184. hr = THR( E_POINTER );
  185. goto Cleanup;
  186. }
  187. //
  188. // Handle known interfaces.
  189. //
  190. if ( IsEqualIID( riidIn, IID_IUnknown ) )
  191. {
  192. *ppvOut = static_cast< ILogger * >( this );
  193. } // if: IUnknown
  194. else if ( IsEqualIID( riidIn, IID_ILogger ) )
  195. {
  196. *ppvOut = TraceInterface( __THISCLASS__, ILogger, this, 0 );
  197. } // else if: ILogger
  198. else
  199. {
  200. *ppvOut = NULL;
  201. hr = E_NOINTERFACE;
  202. }
  203. //
  204. // Add a reference to the interface if successful.
  205. //
  206. if ( SUCCEEDED( hr ) )
  207. {
  208. ((IUnknown *) *ppvOut)->AddRef();
  209. } // if: success
  210. Cleanup:
  211. QIRETURN_IGNORESTDMARSHALLING( hr, riidIn );
  212. } //*** CClCfgSrvLogger::QueryInterface
  213. //////////////////////////////////////////////////////////////////////////////
  214. //
  215. // STDMETHODIMP_( ULONG )
  216. // CClCfgSrvLogger::AddRef
  217. //
  218. //////////////////////////////////////////////////////////////////////////////
  219. STDMETHODIMP_( ULONG )
  220. CClCfgSrvLogger::AddRef( void )
  221. {
  222. TraceFunc( "[IUnknown]" );
  223. InterlockedIncrement( &m_cRef );
  224. CRETURN( m_cRef );
  225. } //*** CClCfgSrvLogger::AddRef
  226. //////////////////////////////////////////////////////////////////////////////
  227. //
  228. // STDMETHODIMP_( ULONG )
  229. // CClCfgSrvLogger::Release
  230. //
  231. //////////////////////////////////////////////////////////////////////////////
  232. STDMETHODIMP_( ULONG )
  233. CClCfgSrvLogger::Release( void )
  234. {
  235. TraceFunc( "[IUnknown]" );
  236. LONG cRef;
  237. cRef = InterlockedDecrement( &m_cRef );
  238. if ( cRef == 0 )
  239. {
  240. TraceDo( delete this );
  241. }
  242. CRETURN( cRef );
  243. } //*** CClCfgSrvLogger::Release
  244. //****************************************************************************
  245. //
  246. // ILogger
  247. //
  248. //****************************************************************************
  249. //////////////////////////////////////////////////////////////////////////////
  250. //++
  251. //
  252. // CClCfgSrvLogger::LogMsg
  253. //
  254. // Description:
  255. // Write the message to the log.
  256. //
  257. // Arguments:
  258. // nLogEntryTypeIn - Log entry type.
  259. // pcszMsgIn - Message to write.
  260. //
  261. // Return Values:
  262. // S_OK - Operation completed successfully.
  263. //
  264. //--
  265. //////////////////////////////////////////////////////////////////////////////
  266. STDMETHODIMP
  267. CClCfgSrvLogger::LogMsg(
  268. DWORD nLogEntryTypeIn
  269. , LPCWSTR pcszMsgIn
  270. )
  271. {
  272. TraceFunc( "[ILogger]" );
  273. HRESULT hr = S_OK;
  274. ::LogMsg( nLogEntryTypeIn, pcszMsgIn );
  275. HRETURN( hr );
  276. } //*** CClCfgSrvLogger::LogMsg
  277. //****************************************************************************
  278. //
  279. // Global Functions
  280. //
  281. //****************************************************************************
  282. //////////////////////////////////////////////////////////////////////////////
  283. //++
  284. //
  285. // HRESULT
  286. // CClCfgSrvLogger::S_HrLogStatusReport(
  287. // LPCWSTR pcszNodeNameIn,
  288. // CLSID clsidTaskMajorIn,
  289. // CLSID clsidTaskMinorIn,
  290. // ULONG ulMinIn,
  291. // ULONG ulMaxIn,
  292. // ULONG ulCurrentIn,
  293. // HRESULT hrStatusIn,
  294. // LPCWSTR pcszDescriptionIn,
  295. // FILETIME * pftTimeIn,
  296. // LPCWSTR pcszReferenceIn
  297. // )
  298. //
  299. //////////////////////////////////////////////////////////////////////////////
  300. HRESULT
  301. CClCfgSrvLogger::S_HrLogStatusReport(
  302. ILogger * plLogger
  303. , LPCWSTR pcszNodeNameIn
  304. , CLSID clsidTaskMajorIn
  305. , CLSID clsidTaskMinorIn
  306. , ULONG ulMinIn
  307. , ULONG ulMaxIn
  308. , ULONG ulCurrentIn
  309. , HRESULT hrStatusIn
  310. , LPCWSTR pcszDescriptionIn
  311. , FILETIME * pftTimeIn
  312. , LPCWSTR pcszReferenceIn
  313. )
  314. {
  315. TraceFunc( "" );
  316. HRESULT hr = S_OK;
  317. FILETIME ft;
  318. BSTR bstrLogMsg = NULL;
  319. BSTR bstrDisplayName = NULL;
  320. OLECHAR wszTaskMajorGuid[ MAX_COM_GUID_STRING_LEN ] = { L'\0' };
  321. OLECHAR wszTaskMinorGuid[ MAX_COM_GUID_STRING_LEN ] = { L'\0' };
  322. PCWSTR pwcszNameToUse = NULL;
  323. //
  324. // TODO: 21 NOV 2000 GalenB
  325. //
  326. // Need to log the timestamp param.
  327. //
  328. if ( pftTimeIn == NULL )
  329. {
  330. GetSystemTimeAsFileTime( &ft );
  331. pftTimeIn = &ft;
  332. } // if: no file time specified
  333. //
  334. // If the node name is fully-qualified, use just the prefix.
  335. //
  336. if ( (pcszNodeNameIn != NULL) && (HrIsValidFQN( pcszNodeNameIn, true /* fAcceptNonRFCCharsIn */ ) == S_OK) )
  337. {
  338. THR( HrGetFQNDisplayName( pcszNodeNameIn, &bstrDisplayName ) );
  339. } // if: node name is a valid FQN
  340. if ( bstrDisplayName == NULL )
  341. {
  342. pwcszNameToUse = pcszNodeNameIn;
  343. }
  344. else
  345. {
  346. pwcszNameToUse = bstrDisplayName;
  347. }
  348. StringFromGUID2( clsidTaskMajorIn, wszTaskMajorGuid, ( int ) RTL_NUMBER_OF( wszTaskMajorGuid ) );
  349. StringFromGUID2( clsidTaskMinorIn, wszTaskMinorGuid, ( int ) RTL_NUMBER_OF( wszTaskMajorGuid ) );
  350. hr = THR( HrFormatStringIntoBSTR(
  351. L"%1!ws!: %2!ws! (hr=%3!#08x!, %4!ws!, %5!ws!, %6!u!, %7!u!, %8!u!), %9!ws!"
  352. , &bstrLogMsg
  353. , pwcszNameToUse // 1
  354. , pcszDescriptionIn // 2
  355. , hrStatusIn // 3
  356. , wszTaskMajorGuid // 4
  357. , wszTaskMinorGuid // 5
  358. , ulMinIn // 6
  359. , ulMaxIn // 7
  360. , ulCurrentIn // 8
  361. , pcszReferenceIn // 9
  362. ) );
  363. if ( SUCCEEDED( hr ) )
  364. {
  365. plLogger->LogMsg( hrStatusIn, bstrLogMsg );
  366. }
  367. else
  368. {
  369. hr = S_OK;
  370. }
  371. TraceSysFreeString( bstrLogMsg );
  372. TraceSysFreeString( bstrDisplayName );
  373. HRETURN( hr );
  374. } //*** CClCfgSrvLogger::S_HrLogStatusReport