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.

267 lines
8.0 KiB

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