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.

205 lines
6.3 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. BOOL fLogCriticalSectionInited = FALSE;
  7. //***************************************************************************
  8. //*
  9. //* purpose: constructor
  10. //*
  11. //***************************************************************************
  12. MyLogFile::MyLogFile(void)
  13. {
  14. _tcscpy(m_szLogFileName, _T(""));
  15. _tcscpy(m_szLogFileName_Full, _T(""));
  16. m_hFile = NULL;
  17. // initialize the critical section
  18. fLogCriticalSectionInited = FALSE;
  19. if( InitializeCriticalSectionAndSpinCount( &critical_section, 0 ) )
  20. fLogCriticalSectionInited = TRUE;
  21. }
  22. //***************************************************************************
  23. //*
  24. //* purpose: destructor
  25. //*
  26. //***************************************************************************
  27. MyLogFile::~MyLogFile(void)
  28. {
  29. if (fLogCriticalSectionInited)
  30. DeleteCriticalSection( &critical_section );
  31. fLogCriticalSectionInited = FALSE;
  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. if (!fLogCriticalSectionInited) return FALSE;
  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. GetModuleFileName(NULL, szModuleFileName, _MAX_PATH);
  53. // get only the filename
  54. _tsplitpath( szModuleFileName, NULL, NULL, szFilename_only, NULL);
  55. _tcscat(szFilename_only, _T(".LOG"));
  56. _tcscpy(m_szLogFileName, szFilename_only);
  57. }
  58. else
  59. {
  60. _tcscpy(m_szLogFileName, lpLogFileName);
  61. }
  62. if (GetWindowsDirectory(m_szLogFileName_Full, sizeof(m_szLogFileName_Full)/sizeof(m_szLogFileName_Full[0])))
  63. {
  64. AddPath(m_szLogFileName_Full, m_szLogFileName);
  65. if (GetFileAttributes(m_szLogFileName_Full) != 0xFFFFFFFF)
  66. {
  67. // Make a backup of the current log file
  68. _tsplitpath( m_szLogFileName_Full, szDrive_only, szPath_only, szFilename_only, NULL);
  69. _tcscpy(szFilename_bak, szDrive_only);
  70. _tcscat(szFilename_bak, szPath_only);
  71. _tcscat(szFilename_bak, szFilename_only);
  72. _tcscat(szFilename_bak, _T(".BAK"));
  73. SetFileAttributes(szFilename_bak, FILE_ATTRIBUTE_NORMAL);
  74. DeleteFile(szFilename_bak);
  75. if (MoveFile(m_szLogFileName_Full, szFilename_bak) == 0)
  76. {
  77. // This failed
  78. MyMessageBox(NULL,_T("LogFile MoveFile Failed"),_T("LogFile Error"), MB_OK | MB_SETFOREGROUND);
  79. }
  80. }
  81. // Open existing file or create a new one.
  82. m_hFile = CreateFile(m_szLogFileName_Full,GENERIC_READ | GENERIC_WRITE,FILE_SHARE_READ | FILE_SHARE_WRITE,NULL,OPEN_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
  83. if (m_hFile == INVALID_HANDLE_VALUE)
  84. {
  85. m_hFile = NULL;
  86. MyMessageBox(NULL, _T("Unable to create log file iis5.log"), _T("LogFile Error"), MB_OK | MB_SETFOREGROUND);
  87. }
  88. else
  89. {
  90. iReturn = TRUE;
  91. }
  92. LogFileWrite(_T("LogFile Open.\r\n"));
  93. }
  94. // safe to leave the critical section
  95. LeaveCriticalSection( &critical_section );
  96. return iReturn;
  97. }
  98. //***************************************************************************
  99. //*
  100. //* purpose:
  101. //*
  102. //***************************************************************************
  103. int MyLogFile::LogFileClose(void)
  104. {
  105. if (m_hFile)
  106. {
  107. LogFileWrite(_T("LogFile Close.\r\n"));
  108. CloseHandle(m_hFile);
  109. return TRUE;
  110. }
  111. return FALSE;
  112. }
  113. //***************************************************************************
  114. //*
  115. //* purpose:
  116. //*
  117. //***************************************************************************
  118. void MyLogFile::LogFileWrite(TCHAR *pszFormatString, ...)
  119. {
  120. if (m_hFile)
  121. {
  122. // because of the global flags and such, we'll make this critical
  123. EnterCriticalSection( &critical_section );
  124. va_list args;
  125. const DWORD cchBufferSize = 1000;
  126. TCHAR pszFullErrMsg[cchBufferSize];
  127. char pszFullErrMsgA[cchBufferSize+2]; // Room for the CRLF, just in case
  128. pszFullErrMsgA[0] = '\0';
  129. DWORD dwBytesWritten = 0;
  130. va_start(args, pszFormatString);
  131. _vsntprintf(pszFullErrMsg, cchBufferSize, pszFormatString, args);
  132. pszFullErrMsg[cchBufferSize-1] = '\0';
  133. va_end(args);
  134. if (*pszFullErrMsg)
  135. {
  136. // convert to ascii then write to stream
  137. WideCharToMultiByte( CP_ACP, 0, (TCHAR *)pszFullErrMsg, -1, pszFullErrMsgA, sizeof(pszFullErrMsgA), NULL, NULL );
  138. // Get timestamp
  139. SYSTEMTIME SystemTime;
  140. GetLocalTime(&SystemTime);
  141. char szDateandtime[50];
  142. sprintf(szDateandtime,"[%d/%d/%d %2.2d:%2.2d:%2.2d] ",SystemTime.wMonth, SystemTime.wDay, SystemTime.wYear,SystemTime.wHour, SystemTime.wMinute, SystemTime.wSecond);
  143. // Write time to stream
  144. if (m_hFile) {
  145. WriteFile(m_hFile,szDateandtime,strlen(szDateandtime),&dwBytesWritten,NULL);
  146. }
  147. // if it does not end if '\r\n' then make one.
  148. int nLen = strlen(pszFullErrMsgA);
  149. if (nLen < 2) {
  150. nLen = 2;
  151. strcpy(pszFullErrMsgA, "\r\n");
  152. } else if (pszFullErrMsgA[nLen-1] != '\n') {
  153. strcat(pszFullErrMsgA, "\r\n");
  154. } else if (pszFullErrMsgA[nLen-2] != '\r') {
  155. char * pPointer = NULL;
  156. pPointer = pszFullErrMsgA + (nLen-1);
  157. strcpy(pPointer, "\r\n");
  158. }
  159. // Write Regular data to stream
  160. if (m_hFile) {
  161. WriteFile(m_hFile,pszFullErrMsgA,strlen(pszFullErrMsgA),&dwBytesWritten,NULL);
  162. }
  163. }
  164. // safe to leave the critical section
  165. LeaveCriticalSection( &critical_section );
  166. }
  167. }