Source code of Windows XP (NT5)
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.

307 lines
9.4 KiB

  1. #include "stdafx.h"
  2. #include "resource.h"
  3. #include "log.h"
  4. // critical section needed to safely write to the logfile
  5. CRITICAL_SECTION critical_section;
  6. //***************************************************************************
  7. //*
  8. //* purpose: constructor
  9. //*
  10. //***************************************************************************
  11. MyLogFile::MyLogFile(void)
  12. {
  13. _tcscpy(m_szLogFileName, _T(""));
  14. _tcscpy(m_szLogFileName_Full, _T(""));
  15. _tcscpy(m_szLogPreLineInfo, _T(""));
  16. _tcscpy(m_szLogPreLineInfo2, _T(""));
  17. m_bDisplayTimeStamp = TRUE;
  18. m_bDisplayPreLineInfo = TRUE;
  19. m_bFlushLogToDisk = FALSE;
  20. m_hFile = NULL;
  21. // initialize the critical section
  22. INITIALIZE_CRITICAL_SECTION( &critical_section );
  23. }
  24. //***************************************************************************
  25. //*
  26. //* purpose: destructor
  27. //*
  28. //***************************************************************************
  29. MyLogFile::~MyLogFile(void)
  30. {
  31. DeleteCriticalSection( &critical_section );
  32. }
  33. //***************************************************************************
  34. //*
  35. //* purpose:
  36. //*
  37. //***************************************************************************
  38. int MyLogFile::LogFileCreate(TCHAR *lpLogFileName )
  39. {
  40. int iReturn = FALSE;
  41. TCHAR szDrive_only[_MAX_DRIVE];
  42. TCHAR szPath_only[_MAX_PATH];
  43. TCHAR szFilename_only[_MAX_PATH];
  44. TCHAR szFilename_bak[_MAX_PATH];
  45. LPWSTR pwsz = NULL;
  46. // because of the global flags and such, we'll make this critical
  47. EnterCriticalSection( &critical_section );
  48. if (lpLogFileName == NULL)
  49. {
  50. TCHAR szModuleFileName[_MAX_PATH];
  51. // if a logfilename was not specified then use the module name.
  52. if (0 == GetModuleFileName(NULL, szModuleFileName, _MAX_PATH))
  53. {
  54. _tcscpy(szFilename_only, _T("iis.log"));
  55. }
  56. else
  57. {
  58. // get only the filename
  59. _tsplitpath( szModuleFileName, NULL, NULL, szFilename_only, NULL);
  60. _tcscat(szFilename_only, _T(".LOG"));
  61. }
  62. _tcscpy(m_szLogFileName, szFilename_only);
  63. }
  64. else
  65. {
  66. _tcscpy(m_szLogFileName, lpLogFileName);
  67. }
  68. if (GetWindowsDirectory(m_szLogFileName_Full, sizeof(m_szLogFileName_Full)/sizeof(TCHAR)))
  69. {
  70. AddPath(m_szLogFileName_Full, m_szLogFileName);
  71. if (GetFileAttributes(m_szLogFileName_Full) != 0xFFFFFFFF)
  72. {
  73. // there is a current .log file already there.
  74. // if it is larger than 2megs then rename it.
  75. DWORD dwSize1 = ReturnFileSize(m_szLogFileName_Full);
  76. if (dwSize1 == 0xFFFFFFFF || dwSize1 > 2000000)
  77. {
  78. // unable to retrieve the size of one of those files
  79. // or the size is bigger than 2megs.
  80. // backup the old one.
  81. // Make a backup of the current log file
  82. _tsplitpath( m_szLogFileName_Full, szDrive_only, szPath_only, szFilename_only, NULL);
  83. _tcscpy(szFilename_bak, szDrive_only);
  84. _tcscat(szFilename_bak, szPath_only);
  85. _tcscat(szFilename_bak, szFilename_only);
  86. _tcscat(szFilename_bak, _T(".BAK"));
  87. SetFileAttributes(szFilename_bak, FILE_ATTRIBUTE_NORMAL);
  88. DeleteFile(szFilename_bak);
  89. if (MoveFile(m_szLogFileName_Full, szFilename_bak) == 0)
  90. {
  91. // This failed
  92. //MyMessageBox(NULL,_T("LogFile MoveFile Failed"),_T("LogFile Error"), MB_OK | MB_SETFOREGROUND);
  93. }
  94. }
  95. }
  96. #if defined(UNICODE) || defined(_UNICODE)
  97. pwsz = m_szLogFileName_Full;
  98. #else
  99. pwsz = MakeWideStrFromAnsi( m_szLogFileName_Full);
  100. #endif
  101. // Open existing file or create a new one.
  102. m_hFile = CreateFile(m_szLogFileName_Full,GENERIC_READ | GENERIC_WRITE,FILE_SHARE_READ | FILE_SHARE_WRITE,NULL,OPEN_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
  103. if (m_hFile == INVALID_HANDLE_VALUE)
  104. {
  105. m_hFile = NULL;
  106. //MyMessageBox(NULL, _T("Unable to create iis setup log file"), _T("LogFile Error"), MB_OK | MB_SETFOREGROUND);
  107. }
  108. else
  109. {
  110. SetFilePointer( m_hFile, NULL, NULL, FILE_END );
  111. iReturn = TRUE;
  112. }
  113. //LogFileTimeStamp();
  114. LogFileWrite(_T("LogFile Open. [***** Search on FAIL/MessageBox keywords for failures *****].\r\n"));
  115. }
  116. // safe to leave the critical section
  117. LeaveCriticalSection( &critical_section );
  118. return iReturn;
  119. }
  120. //***************************************************************************
  121. //*
  122. //* purpose:
  123. //*
  124. //***************************************************************************
  125. int MyLogFile::LogFileClose(void)
  126. {
  127. if (m_hFile)
  128. {
  129. LogFileWrite(_T("LogFile Close.\r\n"));
  130. CloseHandle(m_hFile);
  131. return TRUE;
  132. }
  133. return FALSE;
  134. }
  135. //***************************************************************************
  136. //*
  137. //* purpose: add stuff to logfile
  138. //*
  139. //***************************************************************************
  140. void MyLogFile::LogFileTimeStamp()
  141. {
  142. SYSTEMTIME SystemTime;
  143. GetLocalTime(&SystemTime);
  144. m_bDisplayTimeStamp = FALSE;
  145. m_bDisplayPreLineInfo = FALSE;
  146. LogFileWrite(_T("[%d/%d/%d %d:%d:%d]\r\n"),SystemTime.wMonth, SystemTime.wDay, SystemTime.wYear,SystemTime.wHour, SystemTime.wMinute, SystemTime.wSecond);
  147. m_bDisplayTimeStamp = TRUE;
  148. m_bDisplayPreLineInfo = TRUE;
  149. }
  150. //***************************************************************************
  151. //*
  152. //* purpose:
  153. //*
  154. //***************************************************************************
  155. #define LOG_STRING_LEN 1000
  156. void MyLogFile::LogFileWrite(TCHAR *pszFormatString, ...)
  157. {
  158. if (m_hFile)
  159. {
  160. // because of the global flags and such, we'll make this critical
  161. EnterCriticalSection( &critical_section );
  162. va_list args;
  163. TCHAR pszFullErrMsg[LOG_STRING_LEN];
  164. char pszFullErrMsgA[LOG_STRING_LEN];
  165. strcpy(pszFullErrMsgA, "");
  166. DWORD dwBytesWritten = 0;
  167. if (_tcslen(pszFormatString) > LOG_STRING_LEN)
  168. {
  169. // this will overrun our buffer, just get out
  170. goto MyLogFile_LogFileWrite_Exit;
  171. }
  172. __try
  173. {
  174. va_start(args, pszFormatString);
  175. if (!_vsntprintf(pszFullErrMsg, LOG_STRING_LEN, pszFormatString, args))
  176. {
  177. goto MyLogFile_LogFileWrite_Exit;
  178. }
  179. va_end(args);
  180. }
  181. __except(EXCEPTION_EXECUTE_HANDLER)
  182. {
  183. goto MyLogFile_LogFileWrite_Exit;
  184. }
  185. if (pszFullErrMsg)
  186. {
  187. #if defined(UNICODE) || defined(_UNICODE)
  188. // convert to ascii then write to stream
  189. WideCharToMultiByte( CP_ACP, 0, (TCHAR *)pszFullErrMsg, -1, pszFullErrMsgA, LOG_STRING_LEN, NULL, NULL );
  190. #else
  191. // the is already ascii so just copy the pointer
  192. strcpy(pszFullErrMsgA,pszFullErrMsg);
  193. #endif
  194. // If the Display timestap is set then display the timestamp
  195. if (m_bDisplayTimeStamp == TRUE)
  196. {
  197. // Get timestamp
  198. SYSTEMTIME SystemTime;
  199. GetLocalTime(&SystemTime);
  200. char szDateandtime[50];
  201. sprintf(szDateandtime,"[%d/%d/%d %d:%d:%d] ",SystemTime.wMonth, SystemTime.wDay, SystemTime.wYear,SystemTime.wHour, SystemTime.wMinute, SystemTime.wSecond);
  202. // Write time to stream
  203. if (m_hFile) {WriteFile(m_hFile,szDateandtime,strlen(szDateandtime),&dwBytesWritten,NULL);}
  204. }
  205. char szPrelineWriteString[100];
  206. char szPrelineWriteString2[100];
  207. // If the Display timestap is set then display the timestamp
  208. if (m_bDisplayPreLineInfo == TRUE)
  209. {
  210. if (_tcscmp(m_szLogPreLineInfo,_T("")) != 0)
  211. {
  212. #if defined(UNICODE) || defined(_UNICODE)
  213. // convert to ascii
  214. WideCharToMultiByte( CP_ACP, 0, (TCHAR *)m_szLogPreLineInfo, -1, szPrelineWriteString, 100, NULL, NULL );
  215. #else
  216. // the is already ascii so just copy
  217. strcpy(szPrelineWriteString, m_szLogPreLineInfo);
  218. #endif
  219. if (m_hFile) {WriteFile(m_hFile,szPrelineWriteString,strlen(szPrelineWriteString),&dwBytesWritten,NULL);}
  220. }
  221. if (_tcscmp(m_szLogPreLineInfo2,_T("")) != 0)
  222. {
  223. #if defined(UNICODE) || defined(_UNICODE)
  224. // convert to ascii
  225. WideCharToMultiByte( CP_ACP, 0, (TCHAR *)m_szLogPreLineInfo2, -1, szPrelineWriteString2, 100, NULL, NULL );
  226. #else
  227. // the is already ascii so just copy
  228. strcpy(szPrelineWriteString2, m_szLogPreLineInfo2);
  229. #endif
  230. if (m_hFile) {WriteFile(m_hFile,szPrelineWriteString2,strlen(szPrelineWriteString2),&dwBytesWritten,NULL);}
  231. }
  232. }
  233. // if it does not end if '\r\n' then make one.
  234. int nLen = strlen(pszFullErrMsgA);
  235. if (pszFullErrMsgA[nLen-1] != '\n')
  236. {strcat(pszFullErrMsgA, "\r\n");}
  237. else
  238. {
  239. if (pszFullErrMsgA[nLen-2] != '\r')
  240. {
  241. char * pPointer = NULL;
  242. pPointer = pszFullErrMsgA + (nLen-1);
  243. strcpy(pPointer, "\r\n");
  244. }
  245. }
  246. // Write Regular data to stream
  247. if (m_hFile)
  248. {
  249. WriteFile(m_hFile,pszFullErrMsgA,strlen(pszFullErrMsgA),&dwBytesWritten,NULL);
  250. // since setup can get the rug pulled out from under it from anything
  251. // make sure the file is flushed to disk
  252. if (m_bFlushLogToDisk)
  253. {
  254. FlushFileBuffers(m_hFile);
  255. }
  256. }
  257. }
  258. MyLogFile_LogFileWrite_Exit:
  259. // safe to leave the critical section
  260. LeaveCriticalSection( &critical_section );
  261. }
  262. }