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.

171 lines
5.1 KiB

  1. /****************************************************************************
  2. Copyright information : Copyright (c) 1998-1999 Microsoft Corporation
  3. File Name : WMICliLog.cpp
  4. Project Name : WMI Command Line
  5. Author Name : Ch. Sriramachandramurthy
  6. Date of Creation (dd/mm/yy) : 4th-October-2000
  7. Version Number : 1.0
  8. Brief Description : This class encapsulates the functionality needed
  9. for logging the input and output.
  10. Revision History :
  11. Last Modified By : Ch. Sriramachandramurthy
  12. Last Modified Date : 28th-December-2000
  13. *****************************************************************************/
  14. // WMICliLog.cpp : implementation file
  15. #include "Precomp.h"
  16. #include "WmiCliLog.h"
  17. /*------------------------------------------------------------------------
  18. Name :CWMICliLog
  19. Synopsis :Constructor
  20. Type :Constructor
  21. Input parameter :None
  22. Output parameters :None
  23. Return Type :None
  24. Global Variables :None
  25. Calling Syntax :None
  26. Notes :None
  27. ------------------------------------------------------------------------*/
  28. CWMICliLog::CWMICliLog()
  29. {
  30. m_pszLogFile = NULL;
  31. m_bCreate = FALSE;
  32. }
  33. /*------------------------------------------------------------------------
  34. Name :~CWMICliLog
  35. Synopsis :Destructor
  36. Type :Destructor
  37. Input parameter :None
  38. Output parameters :None
  39. Return Type :None
  40. Global Variables :None
  41. Calling Syntax :None
  42. Notes :None
  43. ------------------------------------------------------------------------*/
  44. CWMICliLog::~CWMICliLog()
  45. {
  46. //Close the File handle
  47. if (m_bCreate)
  48. CloseHandle(m_hFile);
  49. //Delete the file
  50. SAFEDELETE(m_pszLogFile);
  51. }
  52. /*------------------------------------------------------------------------
  53. Name :WriteToLog
  54. Synopsis :Log the input to the logfile created
  55. Type :Member Function
  56. Input parameter :
  57. pszMsg - string, contents to be written to the log file
  58. Output parameters :None
  59. Return Type :void
  60. Global Variables :None
  61. Calling Syntax :WriteToLog(pszInput)
  62. Notes :None
  63. ------------------------------------------------------------------------*/
  64. void CWMICliLog::WriteToLog(LPSTR pszMsg) throw (WMICLIINT)
  65. {
  66. int j = 0;
  67. if(pszMsg && ((j = strlen(pszMsg)) > 0))
  68. {
  69. //if the file has not been created
  70. if (!m_bCreate)
  71. {
  72. try
  73. {
  74. //creates the log file
  75. CreateLogFile();
  76. }
  77. catch(WMICLIINT nErr)
  78. {
  79. if (nErr == WIN32_FUNC_ERROR)
  80. throw(WIN32_FUNC_ERROR);
  81. }
  82. m_bCreate = TRUE;
  83. }
  84. //No of bytes written into the file .
  85. DWORD dwNumberOfBytes = 0;
  86. //writes data to a file
  87. if (!WriteFile(m_hFile, pszMsg, j,
  88. &dwNumberOfBytes, NULL))
  89. {
  90. DisplayWin32Error();
  91. throw(WIN32_FUNC_ERROR);
  92. }
  93. }
  94. }
  95. /*------------------------------------------------------------------------
  96. Name :CreateLogFile
  97. Synopsis :Create the log file
  98. Type :Member Function
  99. Input parameter :None
  100. Output parameters :None
  101. Return Type :void
  102. Global Variables :None
  103. Calling Syntax :CreateLogFile()
  104. Notes :None
  105. ------------------------------------------------------------------------*/
  106. void CWMICliLog::CreateLogFile() throw(WMICLIINT)
  107. {
  108. //Create a file and returns the handle
  109. m_hFile = CreateFile(m_pszLogFile, GENERIC_WRITE, 0,
  110. NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL,
  111. NULL);
  112. if (m_hFile == INVALID_HANDLE_VALUE)
  113. {
  114. DisplayWin32Error();
  115. throw(WIN32_FUNC_ERROR);
  116. }
  117. }
  118. /*------------------------------------------------------------------------
  119. Name :SetLogFilePath
  120. Synopsis :This function sets the m_pszLogFile name with the
  121. input
  122. Type :Member Function
  123. Input parameter :
  124. pszLogFile - String type,Contains the log file name
  125. Return Type :void
  126. Global Variables :None
  127. Calling Syntax :SetLogFilePath(pszLogFile)
  128. Notes :None
  129. ------------------------------------------------------------------------*/
  130. void CWMICliLog::SetLogFilePath(_TCHAR* pszLogFile) throw (WMICLIINT)
  131. {
  132. SAFEDELETE(m_pszLogFile);
  133. m_pszLogFile = new _TCHAR [lstrlen(pszLogFile) + 1];
  134. if (m_pszLogFile)
  135. {
  136. //Copy the input argument into the log file name
  137. lstrcpy(m_pszLogFile, pszLogFile);
  138. }
  139. else
  140. throw(OUT_OF_MEMORY);
  141. }
  142. /*------------------------------------------------------------------------
  143. Name :CloseLogFile
  144. Synopsis :Closes the the log file
  145. Type :Member Function
  146. Input parameter :None
  147. Output parameters :None
  148. Return Type :void
  149. Global Variables :None
  150. Calling Syntax :CloseLogFile()
  151. Notes :None
  152. ------------------------------------------------------------------------*/
  153. void CWMICliLog::CloseLogFile()
  154. {
  155. //Close the File handle
  156. if (m_bCreate)
  157. {
  158. CloseHandle(m_hFile);
  159. m_bCreate = FALSE;
  160. }
  161. }