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.

157 lines
3.5 KiB

  1. /******************************************************************************
  2. Copyright (c) 2001 Microsoft Corporation
  3. Module Name:
  4. Logger.cpp
  5. Abstract:
  6. This file contains the implementation of the Taxonomy::Logger class,
  7. which is used during database updates.
  8. Revision History:
  9. Davide Massarenti (Dmassare) 24/03/2001
  10. created
  11. ******************************************************************************/
  12. #include "stdafx.h"
  13. Taxonomy::Logger::Logger()
  14. {
  15. // MPC::FileLog m_obj;
  16. m_dwLogging = 0; // DWORD m_dwLogging;
  17. }
  18. Taxonomy::Logger::~Logger()
  19. {
  20. if(m_dwLogging)
  21. {
  22. WriteLog( E_FAIL, L"Forcing closure of log file." );
  23. EndLog ( );
  24. }
  25. }
  26. HRESULT Taxonomy::Logger::StartLog( /*[in]*/ LPCWSTR szLocation )
  27. {
  28. __HCP_FUNC_ENTRY( "Taxonomy::Logger::StartLog" );
  29. HRESULT hr;
  30. if(m_dwLogging++ == 0)
  31. {
  32. MPC::wstring szFile( szLocation ? szLocation : HC_HCUPDATE_LOGNAME ); MPC::SubstituteEnvVariables( szFile );
  33. // Attempt to open the log for writing
  34. __MPC_EXIT_IF_METHOD_FAILS(hr, m_obj.SetLocation( szFile.c_str() ));
  35. // write it out to log file
  36. __MPC_EXIT_IF_METHOD_FAILS(hr, WriteLog( -1, L"===========================================\nHCUPDATE Log started\n===========================================" ));
  37. }
  38. hr = S_OK;
  39. __HCP_FUNC_CLEANUP;
  40. __HCP_FUNC_EXIT(hr);
  41. }
  42. HRESULT Taxonomy::Logger::EndLog()
  43. {
  44. __HCP_FUNC_ENTRY( "Taxonomy::Logger::EndLog" );
  45. HRESULT hr;
  46. if(m_dwLogging > 0)
  47. {
  48. if(m_dwLogging == 1)
  49. {
  50. (void)WriteLog( -1, L"===========================================\nHCUPDATE Log ended\n===========================================" );
  51. __MPC_EXIT_IF_METHOD_FAILS(hr, m_obj.Terminate());
  52. }
  53. m_dwLogging--;
  54. }
  55. hr = S_OK;
  56. __HCP_FUNC_CLEANUP;
  57. __HCP_FUNC_EXIT(hr);
  58. }
  59. HRESULT Taxonomy::Logger::WriteLogV( /*[in]*/ HRESULT hrRes ,
  60. /*[in]*/ LPCWSTR szLogFormat ,
  61. /*[in]*/ va_list arglist )
  62. {
  63. __HCP_FUNC_ENTRY( "Taxonomy::Logger::WriteLogV" );
  64. HRESULT hr;
  65. WCHAR rgLine[256];
  66. WCHAR* pLine = NULL;
  67. WCHAR* pLinePtr;
  68. if(_vsnwprintf( rgLine, MAXSTRLEN(rgLine), szLogFormat, arglist ) == -1)
  69. {
  70. const int iSizeMax = 8192;
  71. __MPC_EXIT_IF_ALLOC_FAILS(hr, pLine, new WCHAR[iSizeMax]);
  72. _vsnwprintf( pLine, iSizeMax-1, szLogFormat, arglist ); pLine[iSizeMax-1] = 0;
  73. pLinePtr = pLine;
  74. }
  75. else
  76. {
  77. rgLine[MAXSTRLEN(rgLine)] = 0;
  78. pLinePtr = rgLine;
  79. }
  80. if(hrRes == -2)
  81. {
  82. hrRes = HRESULT_FROM_WIN32(::GetLastError());
  83. }
  84. if(hrRes == -1)
  85. {
  86. hrRes = S_OK;
  87. if(m_dwLogging)
  88. {
  89. __MPC_EXIT_IF_METHOD_FAILS(hr, m_obj.LogRecord( L"%s", pLinePtr ));
  90. }
  91. }
  92. else
  93. {
  94. if(m_dwLogging)
  95. {
  96. __MPC_EXIT_IF_METHOD_FAILS(hr, m_obj.LogRecord( L"%x - %s", hrRes, pLinePtr ));
  97. }
  98. }
  99. hr = S_OK;
  100. __HCP_FUNC_CLEANUP;
  101. delete [] pLine;
  102. if(FAILED(hrRes)) hr = hrRes;
  103. __HCP_FUNC_EXIT(hr);
  104. }
  105. HRESULT Taxonomy::Logger::WriteLog( /*[in]*/ HRESULT hrRes ,
  106. /*[in]*/ LPCWSTR szLogFormat ,
  107. /*[in]*/ ... )
  108. {
  109. va_list arglist;
  110. va_start( arglist, szLogFormat );
  111. return WriteLogV( hrRes, szLogFormat, arglist );
  112. }