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.

269 lines
4.2 KiB

  1. /*++
  2. Copyright (c) 1996 Microsoft Corporation
  3. Module Name :
  4. errlog.cpp
  5. Abstract:
  6. Error logging object implementation. This object will log the link
  7. checking error according to the user options (CUserOptions)
  8. Author:
  9. Michael Cheuk (mcheuk)
  10. Project:
  11. Link Checker
  12. Revision History:
  13. --*/
  14. #include "stdafx.h"
  15. #include "errlog.h"
  16. #include "lcmgr.h"
  17. #ifdef _DEBUG
  18. #define new DEBUG_NEW
  19. #undef THIS_FILE
  20. static char THIS_FILE[] = __FILE__;
  21. #endif
  22. // Constant string (TODO: put this in resource)
  23. const CString strHeaderText_c(_T("Start Link Checker"));
  24. const CString strFooterText_c(_T("End Link Checker"));
  25. const CString strWininetError_c(_T("Internet Error"));
  26. CErrorLog::~CErrorLog(
  27. )
  28. /*++
  29. Routine Description:
  30. Destructor.
  31. Arguments:
  32. N/A
  33. Return Value:
  34. N/A
  35. --*/
  36. {
  37. if(m_LogFile.m_hFile != CFile::hFileNull)
  38. {
  39. try
  40. {
  41. m_LogFile.Close();
  42. }
  43. catch(CFileException* pEx)
  44. {
  45. ASSERT(FALSE);
  46. pEx->Delete();
  47. }
  48. }
  49. } // CErrorLog::~CErrorLog
  50. BOOL
  51. CErrorLog::Create(
  52. )
  53. /*++
  54. Routine Description:
  55. Create this object. You must call this before using CErrorLog
  56. Arguments:
  57. N/A
  58. Return Value:
  59. BOOL - TRUE if sucess. FALSE otherwise
  60. --*/
  61. {
  62. // Get the user input log filename
  63. const CString& strLogFilename = GetLinkCheckerMgr().GetUserOptions().GetLogFilename();
  64. // Create the file
  65. if(GetLinkCheckerMgr().GetUserOptions().IsLogToFile() &&
  66. !strLogFilename.IsEmpty())
  67. {
  68. if(m_LogFile.Open(
  69. strLogFilename,
  70. CFile::modeCreate | CFile::modeNoTruncate |
  71. CFile::shareDenyWrite | CFile::modeWrite))
  72. {
  73. try
  74. {
  75. m_LogFile.SeekToEnd();
  76. }
  77. catch(CFileException* pEx)
  78. {
  79. ASSERT(FALSE);
  80. pEx->Delete();
  81. return FALSE;
  82. }
  83. return TRUE;
  84. }
  85. else
  86. {
  87. return FALSE;
  88. }
  89. }
  90. return TRUE;
  91. } // CErrorLog::Create
  92. void
  93. CErrorLog::Write(
  94. const CLink& link)
  95. /*++
  96. Routine Description:
  97. Write to log
  98. Arguments:
  99. N/A
  100. Return Value:
  101. N/A
  102. --*/
  103. {
  104. // Make sure the link is invalid
  105. ASSERT(link.GetState() == CLink::eInvalidHTTP ||
  106. link.GetState() == CLink::eInvalidWininet);
  107. if(m_LogFile.m_hFile != CFile::hFileNull)
  108. {
  109. CString strDateTime = link.GetTime().Format("%x\t%X");
  110. CString strLog;
  111. if(link.GetState() == CLink::eInvalidHTTP)
  112. {
  113. strLog.Format(_T("%s\t%s\t%s\t%s\t%d\t%s\t%s\n"),
  114. link.GetBase(), m_strBrowser,
  115. m_strLanguage, strDateTime,
  116. link.GetStatusCode(), link.GetStatusText(), link.GetRelative());
  117. }
  118. else if(link.GetState() == CLink::eInvalidWininet)
  119. {
  120. strLog.Format(_T("%s\t%s\t%s\t%s\t%s\t%s\t%s\n"),
  121. link.GetBase(), m_strBrowser,
  122. m_strLanguage, strDateTime,
  123. strWininetError_c, link.GetStatusText(), link.GetRelative());
  124. }
  125. try
  126. {
  127. m_LogFile.Write(strLog, strLog.GetLength());
  128. m_LogFile.Flush();
  129. }
  130. catch(CFileException* pEx)
  131. {
  132. ASSERT(FALSE);
  133. pEx->Delete();
  134. }
  135. }
  136. } // CErrorLog::Write
  137. void
  138. CErrorLog::WriteHeader(
  139. )
  140. /*++
  141. Routine Description:
  142. Write the log header
  143. Arguments:
  144. N/A
  145. Return Value:
  146. N/A
  147. --*/
  148. {
  149. if(m_LogFile.m_hFile != CFile::hFileNull)
  150. {
  151. CString strLog;
  152. strLog.Format(_T("*** %s *** %s\n"), strHeaderText_c,
  153. CTime::GetCurrentTime().Format("%x\t%X"));
  154. try
  155. {
  156. m_LogFile.Write(strLog, strLog.GetLength());
  157. m_LogFile.Flush();
  158. }
  159. catch(CFileException* pEx)
  160. {
  161. ASSERT(FALSE);
  162. pEx->Delete();
  163. }
  164. }
  165. } // CErrorLog::WriteHeader
  166. void
  167. CErrorLog::WriteFooter(
  168. )
  169. /*++
  170. Routine Description:
  171. Write the log footer
  172. Arguments:
  173. N/A
  174. Return Value:
  175. N/A
  176. --*/
  177. {
  178. if(m_LogFile.m_hFile != CFile::hFileNull)
  179. {
  180. CString strLog;
  181. strLog.Format(_T("*** %s *** %s\n"), strFooterText_c,
  182. CTime::GetCurrentTime().Format(_T("%x\t%X")));
  183. try
  184. {
  185. m_LogFile.Write(strLog, strLog.GetLength());
  186. m_LogFile.Flush();
  187. }
  188. catch(CFileException* pEx)
  189. {
  190. ASSERT(FALSE);
  191. pEx->Delete();
  192. }
  193. }
  194. } // CErrorLog::WriteFooter