home *** CD-ROM | disk | FTP | other *** search
/ Computer Shopper 217 / DPCS0306DVD.ISO / Toolkit / Internet / FileZilla / Server / FileZilla_Server-0.9.11.exe / source / FileLogger.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2004-06-06  |  6.6 KB  |  240 lines

  1. // FileZilla Server - a Windows ftp server
  2.  
  3. // Copyright (C) 2002-2004 - Tim Kosse <tim.kosse@gmx.de>
  4.  
  5. // This program is free software; you can redistribute it and/or
  6. // modify it under the terms of the GNU General Public License
  7. // as published by the Free Software Foundation; either version 2
  8. // of the License, or (at your option) any later version.
  9.  
  10. // This program is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. // GNU General Public License for more details.
  14.  
  15. // You should have received a copy of the GNU General Public License
  16. // along with this program; if not, write to the Free Software
  17. // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  18.  
  19. #include "stdafx.h"
  20. #include "FileLogger.h"
  21. #include "Options.h"
  22.  
  23. //////////////////////////////////////////////////////////////////////
  24. // Konstruktion/Destruktion
  25. //////////////////////////////////////////////////////////////////////
  26.  
  27. CFileLogger::CFileLogger(COptions *pOptions)
  28. {
  29.     m_hLogFile = INVALID_HANDLE_VALUE;
  30.     m_pOptions = pOptions;
  31.  
  32.     m_pFileName = NULL;
  33.  
  34.     CheckLogFile();
  35. }
  36.  
  37. CFileLogger::~CFileLogger()
  38. {
  39.     if (m_hLogFile != INVALID_HANDLE_VALUE)
  40.         CloseHandle(m_hLogFile);
  41.  
  42.     delete [] m_pFileName;
  43. }
  44.  
  45. BOOL CFileLogger::Log(LPCTSTR msg)
  46. {
  47.     if (m_hLogFile==INVALID_HANDLE_VALUE)
  48.         return TRUE;
  49.  
  50.     DWORD numwritten;
  51.     if (!WriteFile(m_hLogFile, msg, _tcslen(msg), &numwritten, 0) || !WriteFile(m_hLogFile, "\r\n", 2, &numwritten, 0))
  52.     {
  53.         CloseHandle(m_hLogFile);
  54.         m_hLogFile = INVALID_HANDLE_VALUE;
  55.         return FALSE;
  56.     }
  57.  
  58.     return TRUE;
  59. }
  60.  
  61. BOOL CFileLogger::CheckLogFile()
  62. {
  63.     if (!m_pOptions->GetOptionVal(OPTION_ENABLELOGGING))
  64.     {
  65.         if (m_hLogFile != INVALID_HANDLE_VALUE)
  66.         {
  67.             CloseHandle(m_hLogFile);
  68.             m_hLogFile = INVALID_HANDLE_VALUE;
  69.         }
  70.         return TRUE;
  71.     }
  72.  
  73.     //Get logfile path
  74.     TCHAR path[MAX_PATH + 1000]; //Make it large enough
  75.     GetModuleFileName( 0, path, MAX_PATH );
  76.     LPTSTR pos=_tcsrchr(path, '\\');
  77.     if (pos)
  78.         *++pos=0;
  79.     _tcscat(path, _T("Logs\\"));
  80.     
  81.     //Get logfile name
  82.     _int64 nLogType = m_pOptions->GetOptionVal(OPTION_LOGTYPE);        
  83.     TCHAR filename[MAX_PATH + 1];
  84.     if (!nLogType)
  85.     {
  86.         _tcscpy(filename, _T("FileZilla Server.log"));
  87.     }
  88.     else
  89.     {
  90.         SYSTEMTIME time;
  91.         GetSystemTime(&time);
  92.         _stprintf(filename, _T("fzs-%d-%02d-%02d.log"), time.wYear, time.wMonth, time.wDay);
  93.     }
  94.  
  95.     if (m_hLogFile == INVALID_HANDLE_VALUE || (m_pFileName && _tcscmp(m_pFileName, filename)))
  96.     {
  97.         TCHAR buffer[MAX_PATH + 1000]; //Make it large enough
  98.         _tcscpy(buffer, path);
  99.         CreateDirectory(buffer, NULL);
  100.  
  101.         if (m_pFileName)
  102.             delete [] m_pFileName;
  103.         m_pFileName = new TCHAR[_tcslen(filename)+1];
  104.         _tcscpy(m_pFileName, filename);    
  105.         _tcscat(buffer, filename);
  106.         
  107.         if (m_hLogFile != INVALID_HANDLE_VALUE)
  108.             CloseHandle(m_hLogFile);
  109.         m_hLogFile = CreateFile(buffer, GENERIC_WRITE|GENERIC_READ, FILE_SHARE_READ, 0, OPEN_ALWAYS, 0, 0);
  110.         if (m_hLogFile == INVALID_HANDLE_VALUE)
  111.             return FALSE;
  112.         
  113.         SetFilePointer(m_hLogFile, 0, 0, FILE_END);
  114.     }
  115.     _int64 nLimit = m_pOptions->GetOptionVal(OPTION_LOGLIMITSIZE);
  116.     
  117.     if (nLogType)
  118.     {
  119.         //Different logfiles for each day
  120.         //Find all log files, delete old ones
  121.         //Also delete newer ones if total size exceeds limit
  122.  
  123.         //Get current date
  124.  
  125.         SYSTEMTIME time;
  126.         FILETIME curFileTime;
  127.         GetSystemTime(&time);
  128.         SystemTimeToFileTime(&time, &curFileTime);
  129.         _int64 nTime = curFileTime.dwLowDateTime + ((_int64)curFileTime.dwHighDateTime<<32);
  130.         
  131.         TCHAR buffer[MAX_PATH + 1000]; //Make it large enough
  132.         _tcscpy(buffer, path);
  133.         _tcscat(buffer, _T("fzs-*.log"));
  134.  
  135.         WIN32_FIND_DATA FindFileData;
  136.         WIN32_FIND_DATA NextFindFileData;
  137.         HANDLE hFind;
  138.         hFind = FindFirstFile(buffer, &NextFindFileData);
  139.         
  140.         _int64 nDeleteTime = (_int64)m_pOptions->GetOptionVal(OPTION_LOGDELETETIME);
  141.         if (nDeleteTime)
  142.             nDeleteTime = (nDeleteTime+1) * 60 * 60 * 24 * 10000000;
  143.  
  144.         //Count total size of all logs, delete the oldest log if exceeding limit
  145.         _int64 totalsize = 0;
  146.         CStdString oldestname;
  147.         _int64 oldestDate = 0;
  148.         
  149.         while (hFind != INVALID_HANDLE_VALUE)
  150.         {
  151.             FindFileData=NextFindFileData;
  152.             if (!FindNextFile(hFind, &NextFindFileData))
  153.             {
  154.                 FindClose(hFind);
  155.                 hFind = INVALID_HANDLE_VALUE;
  156.             }
  157.  
  158.             if (!_tcscmp(FindFileData.cFileName, _T(".")) || !_tcscmp(FindFileData.cFileName, _T("..")))
  159.                 continue;
  160.  
  161.             _int64 size = ((_int64)FindFileData.nFileSizeHigh<<32) + FindFileData.nFileSizeLow;
  162.             if (!_tcscmp(FindFileData.cFileName, m_pFileName))
  163.             {
  164.                 totalsize += size;
  165.                 continue;
  166.             }
  167.  
  168.             _int64 curtime=FindFileData.ftLastWriteTime.dwLowDateTime + ((_int64)FindFileData.ftLastWriteTime.dwHighDateTime<<32);
  169.             _int64 span = nTime - curtime;
  170.             TCHAR filename[MAX_PATH + 1000];
  171.             _tcscpy(filename, path);
  172.             _tcscat(filename, FindFileData.cFileName);
  173.             if (nDeleteTime && span > nDeleteTime)
  174.                 DeleteFile(filename); //File is too old, delete it
  175.             else
  176.             {
  177.                 totalsize += size;
  178.                 if (curtime < oldestDate || !oldestDate)
  179.                 {
  180.                     oldestDate = curtime;
  181.                     oldestname = filename;
  182.                 }
  183.             }
  184.         }
  185.         
  186.         if (_tcscmp(oldestname, "") && nLimit && totalsize > nLimit*1024)
  187.         {
  188.             DeleteFile(oldestname);
  189.             return TRUE;
  190.         }
  191.     }
  192.  
  193.     //Single logfile, check size...
  194.     if (nLimit)
  195.     {
  196.         _int64 size = GetPosition64(m_hLogFile);
  197.         size /= 1024;
  198.         if (size > nLimit) //Log file too large, shrink it...
  199.         {
  200.             int curReadPos = (int)(size * 1024 - (nLimit * 1024) * 0.9); //New log size is 10% smaller than the set limit
  201.             int curWritePos =0;
  202.             const int bufsize = 16384; // 16KB
  203.             char buffer[bufsize];
  204.             DWORD numread;
  205.             DWORD numwritten;
  206.             BOOL bFirst = TRUE;;
  207.             do {
  208.                 SetFilePointer(m_hLogFile, curReadPos, 0, FILE_BEGIN);
  209.                 if (!ReadFile(m_hLogFile, buffer, bufsize, &numread, 0))
  210.                     break;
  211.                 curReadPos += numread;
  212.                 
  213.                 SetFilePointer(m_hLogFile, curWritePos, 0, FILE_BEGIN);
  214.                 if (bFirst) //Assure log starts with complete line
  215.                 {
  216.                     unsigned int i;
  217.                     for (i=0; i<numread; i++)
  218.                     {
  219.                         if (buffer[i] == '\n')
  220.                             break;
  221.                     }
  222.                     if (i >= (numread-1))
  223.                         continue;
  224.                     bFirst = FALSE;
  225.                     if (!WriteFile(m_hLogFile, buffer + i + 1, numread - i - 1, &numwritten, 0))
  226.                         break;
  227.                 }
  228.                 else
  229.                     if (!WriteFile(m_hLogFile, buffer, numread, &numwritten, 0))
  230.                         break;
  231.                     curWritePos += numwritten;
  232.                     
  233.             } while (numread == bufsize);
  234.             
  235.             SetFilePointer(m_hLogFile, curWritePos, 0, FILE_BEGIN);
  236.             SetEndOfFile(m_hLogFile);
  237.         }
  238.     }
  239.     return TRUE;
  240. }