2015-11-15 23 views
0

我想要編譯log4cpp在Visual Studio 2008,但我得到了一些錯誤,像這樣的:如何解決log4cpp中的以下問題?

Error 26 error C2664: 'ReportEventW' : cannot convert parameter 8 from 'const char *[1]' to 'LPCWSTR *' ...\testlog4cppdll\log4cpp\src\nteventlogappender.cpp 64 
Error 19 error C2676: binary '+' : 'std::basic_string<_Elem,_Traits,_Ax>' does not define this operator or a conversion to a type acceptable to the predefined operator ...\testlog4cppdll\log4cpp\src\dailyrollingfileappender.cpp 127 
Error 11 error C2782: 'std::basic_string<_Elem,_Traits,_Alloc> std::operator +(const std::basic_string<_Elem,_Traits,_Alloc> &,const _Elem *)' : template parameter '_Elem' is ambiguous ...\testlog4cppdll\log4cpp\src\dailyrollingfileappender.cpp 127 
Error 10 error C2782: 'std::basic_string<_Elem,_Traits,_Alloc> std::operator +(const std::basic_string<_Elem,_Traits,_Alloc> &,const _Elem)' : template parameter '_Elem' is ambiguous ...\testlog4cppdll\log4cpp\src\dailyrollingfileappender.cpp 127 
Error 18 error C2784: 'std::_Revranit<_RanIt,_Base> std::operator +(_Diff,const std::_Revranit<_RanIt,_Base> &)' : could not deduce template argument for 'const std::_Revranit<_RanIt,_Base> &' from 'WCHAR [260]' ...\testlog4cppdll\log4cpp\src\dailyrollingfileappender.cpp 127 
Error 16 error C2784: 'std::_String_const_iterator<_Elem,_Traits,_Alloc> std::operator +(_String_const_iterator<_Elem,_Traits,_Alloc>::difference_type,std::_String_const_iterator<_Elem,_Traits,_Alloc>)' : could not deduce template argument for 'std::_String_const_iterator<_Elem,_Traits,_Alloc>' from 'WCHAR [260]' ...\testlog4cppdll\log4cpp\src\dailyrollingfileappender.cpp 127 
Error 15 error C2784: 'std::_String_iterator<_Elem,_Traits,_Alloc> std::operator +(_String_iterator<_Elem,_Traits,_Alloc>::difference_type,std::_String_iterator<_Elem,_Traits,_Alloc>)' : could not deduce template argument for 'std::_String_iterator<_Elem,_Traits,_Alloc>' from 'WCHAR [260]' ...\testlog4cppdll\log4cpp\src\dailyrollingfileappender.cpp 127 
Error 7 error C2784: 'std::_Vb_const_iterator<_Sizet,_Difft,_MycontTy> std::operator +(_Difft,std::_Vb_const_iterator<_Sizet,_Difft,_MycontTy>)' : could not deduce template argument for 'std::_Vb_const_iterator<_Sizet,_Difft,_MycontTy>' from 'WCHAR [260]' ...\testlog4cppdll\log4cpp\src\dailyrollingfileappender.cpp 127 
Error 6 error C2784: 'std::_Vb_iterator<_Sizet,_Difft,_MycontTy> std::operator +(_Difft,std::_Vb_iterator<_Sizet,_Difft,_MycontTy>)' : could not deduce template argument for 'std::_Vb_iterator<_Sizet,_Difft,_MycontTy>' from 'WCHAR [260]' ...\testlog4cppdll\log4cpp\src\dailyrollingfileappender.cpp 127 
Error 9 error C2784: 'std::_Vector_const_iterator<_Ty,_Alloc> std::operator +(_Vector_const_iterator<_Ty,_Alloc>::difference_type,std::_Vector_const_iterator<_Ty,_Alloc>)' : could not deduce template argument for 'std::_Vector_const_iterator<_Ty,_Alloc>' from 'WCHAR [260]' ...\testlog4cppdll\log4cpp\src\dailyrollingfileappender.cpp 127 
Error 8 error C2784: 'std::_Vector_iterator<_Ty,_Alloc> std::operator +(_Vector_iterator<_Ty,_Alloc>::difference_type,std::_Vector_iterator<_Ty,_Alloc>)' : could not deduce template argument for 'std::_Vector_iterator<_Ty,_Alloc>' from 'WCHAR [260]' ...\testlog4cppdll\log4cpp\src\dailyrollingfileappender.cpp 127 
Error 13 error C2784: 'std::basic_string<_Elem,_Traits,_Alloc> std::operator +(const _Elem *,const std::basic_string<_Elem,_Traits,_Alloc> &)' : could not deduce template argument for 'const _Elem *' from 'std::basic_string<_Elem,_Traits,_Ax>' ...\testlog4cppdll\log4cpp\src\dailyrollingfileappender.cpp 127 
Error 12 error C2784: 'std::basic_string<_Elem,_Traits,_Alloc> std::operator +(const _Elem,const std::basic_string<_Elem,_Traits,_Alloc> &)' : could not deduce template argument for 'const std::basic_string<_Elem,_Traits,_Alloc> &' from 'WCHAR [260]' ...\testlog4cppdll\log4cpp\src\dailyrollingfileappender.cpp 127 
Error 14 error C2784: 'std::basic_string<_Elem,_Traits,_Alloc> std::operator +(const std::basic_string<_Elem,_Traits,_Alloc> &,const std::basic_string<_Elem,_Traits,_Alloc> &)' : could not deduce template argument for 'const std::basic_string<_Elem,_Traits,_Alloc> &' from 'WCHAR [260]' ...\testlog4cppdll\log4cpp\src\dailyrollingfileappender.cpp 127 
Error 17 error C2784: 'std::reverse_iterator<_RanIt> std::operator +(_Diff,const std::reverse_iterator<_RanIt> &)' : could not deduce template argument for 'const std::reverse_iterator<_RanIt> &' from 'WCHAR [260]' ...\testlog4cppdll\log4cpp\src\dailyrollingfileappender.cpp 127 

下面是代碼:

hFind = FindFirstFile(LPTSTR(pattern.c_str()), &ffd); //Ahrost 
    if (hFind != INVALID_HANDLE_VALUE) { 
     do { 
      struct stat statBuf; 
      const std::string fullfilename = dirname + PATHDELIMITER + ffd.cFileName; 
      int res = ::stat(fullfilename.c_str(), &statBuf); 
      if (res != -1 && statBuf.st_mtime < oldest && !(ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) { 
       std::cout << "Deleting " << fullfilename << "\n"; 
       ::unlink(fullfilename.c_str()); 
      } 
     } while (FindNextFile(hFind, &ffd) != 0); 

     if (GetLastError() != ERROR_NO_MORE_FILES) { 
      // [XXX] some kind of error happened 
     } 
     FindClose(hFind); 
     hFind = INVALID_HANDLE_VALUE; 
    } 

文件的全部源代碼是:

#include "PortabilityImpl.hh" 
#ifdef LOG4CPP_HAVE_IO_H 
# include <io.h> 
#endif 
#ifdef LOG4CPP_HAVE_UNISTD_H 
# include <unistd.h> 
#endif 

#include <sys/types.h> 
#include <sys/stat.h> 
#include <fcntl.h> 
#include <log4cpp/DailyRollingFileAppender.hh> 
#include <log4cpp/Category.hh> 
#include <log4cpp/FactoryParams.hh> 
#include <memory> 
#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 
#include <iostream> 

#include <time.h> //Ahrost 

#ifdef LOG4CPP_HAVE_SSTREAM 
#include <sstream> 
#include <iomanip> 
#endif 

#ifndef WIN32 // only available on Win32 
#include <dirent.h> 
#endif 

namespace log4cpp { 

    unsigned int DailyRollingFileAppender::maxDaysToKeepDefault = 30; 

    DailyRollingFileAppender::DailyRollingFileAppender(const std::string& name, 
              const std::string& fileName, 
              unsigned int maxDaysToKeep, 
              bool append, 
              mode_t mode) : 
     FileAppender(name, fileName, append, mode), 
     _maxDaysToKeep(maxDaysToKeep != 0 ? maxDaysToKeep : maxDaysToKeepDefault) { 
     struct stat statBuf; 
     int res; 
     time_t t; 

     // obtain last modification time 
     res = ::stat(fileName.c_str(), &statBuf); 
     if (res < 0) { 
      t = time(NULL); 
     } else { 
      t = statBuf.st_mtime; 
     } 
#ifndef WIN32 // only available on Win32 
     localtime_r(&t, &_logsTime); 
#else 
     localtime_s(&_logsTime, &t); 
#endif 
    } 

    void DailyRollingFileAppender::setMaxDaysToKeep(unsigned int maxDaysToKeep) { 
     _maxDaysToKeep = maxDaysToKeep; 
    } 

    unsigned int DailyRollingFileAppender::getMaxDaysToKeep() const { 
     return _maxDaysToKeep; 
    } 

    void DailyRollingFileAppender::rollOver() 
    { 
     std::ostringstream filename_s; 
     ::close(_fd); 
     filename_s << _fileName << "." << _logsTime.tm_year + 1900 << "-" 
         << std::setfill('0') << std::setw(2) << _logsTime.tm_mon + 1 << "-" 
         << std::setw(2) << _logsTime.tm_mday << std::ends; 
     const std::string lastFn = filename_s.str(); 
     ::rename(_fileName.c_str(), lastFn.c_str()); 

     _fd = ::open(_fileName.c_str(), _flags, _mode); 

     const time_t oldest = time(NULL) - _maxDaysToKeep * 60 * 60 * 24; 

#ifndef WIN32 
#define PATHDELIMITER "/" 
#else 
#define PATHDELIMITER "\\" 
#endif 
     // iterate over files around log file and delete older with same prefix 
     const std::string::size_type last_delimiter = _fileName.rfind(PATHDELIMITER); 
     const std::string dirname((last_delimiter == std::string::npos)? "." : _fileName.substr(0, last_delimiter)); 
     const std::string filname((last_delimiter == std::string::npos)? _fileName : _fileName.substr(last_delimiter+1, _fileName.size()-last_delimiter-1)); 
#ifndef WIN32 // only available on Win32 
     struct dirent **entries; 
     int nentries = scandir(dirname.c_str(), &entries, 0, alphasort); 
     if (nentries < 0) 
      return; 
     for (int i = 0; i < nentries; i++) { 
      struct stat statBuf; 
      int res = ::stat(entries[i]->d_name, &statBuf); 
      if ((res == -1) || (!S_ISREG(statBuf.st_mode))) { 
       free(entries[i]); 
       continue; 
      } 
      if (statBuf.st_mtime < oldest && strstr(entries[i]->d_name, filname.c_str())) { 
       const std::string fullfilename = dirname + PATHDELIMITER + entries[i]->d_name; 
       ::unlink(fullfilename.c_str()); 
       std::cout << " Deleting " << fullfilename.c_str() << std::endl; 
      } 
      free(entries[i]); 
     } 
     free(entries); 
#else 
    HANDLE hFind = INVALID_HANDLE_VALUE; 
    WIN32_FIND_DATA ffd; 
    const std::string pattern = _fileName + "*"; 

    hFind = FindFirstFile(LPTSTR(pattern.c_str()), &ffd); //Ahrost 
    if (hFind != INVALID_HANDLE_VALUE) { 
     do { 
      struct stat statBuf; 
      const std::string fullfilename = dirname + PATHDELIMITER + ffd.cFileName; 
      int res = ::stat(fullfilename.c_str(), &statBuf); 
      if (res != -1 && statBuf.st_mtime < oldest && !(ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) { 
       std::cout << "Deleting " << fullfilename << "\n"; 
       ::unlink(fullfilename.c_str()); 
      } 
     } while (FindNextFile(hFind, &ffd) != 0); 

     if (GetLastError() != ERROR_NO_MORE_FILES) { 
      // [XXX] some kind of error happened 
     } 
     FindClose(hFind); 
     hFind = INVALID_HANDLE_VALUE; 
    } 
#endif 
    } 

    void DailyRollingFileAppender::_append(const log4cpp::LoggingEvent &event) 
    { 
     struct tm now; 
     time_t t = time(NULL); 

#ifndef WIN32 // only available on Win32 
     bool timeok = localtime_r(&t, &now) != NULL; 
#else 
     bool timeok = localtime_s(&now, &t) == 0; 
#endif 
     if (timeok) { 
      if ((now.tm_mday != _logsTime.tm_mday) || 
       (now.tm_mon != _logsTime.tm_mon) || 
       (now.tm_year != _logsTime.tm_year)) { 
       rollOver(); 
       _logsTime = now; 
      } 
     } 
     log4cpp::FileAppender::_append(event); 
    } 

    std::auto_ptr<Appender> create_daily_roll_file_appender(const FactoryParams& params) 
    { 
     std::string name, filename; 
     bool append = true; 
     mode_t mode = 664; 
     unsigned int max_days_keep = 0; 
     params.get_for("daily roll file appender").required("name", name)("filename", filename)("max_days_keep", max_days_keep) 
              .optional("append", append)("mode", mode); 

     return std::auto_ptr<Appender>(new DailyRollingFileAppender(name, filename, max_days_keep, append, mode)); 
    } 
} 

上面的源代碼屬於DailyRollingFileAppender.cpp文件,它是log4cpp文件之一。

回答

0

你的問題是混淆窄字符串和寬字符串。

在名稱中使用帶字母T的字符類型(如LPTSTR)意味着代碼可以使用窄字符或寬字符,具體取決於_UNICODE設置。但是,你也可以使用std :: string類型,這些類型明確地縮小,並且使用沒有寬字符說明符L的文字。

如果你的代碼應該可用於狹義和廣義優勢,請確保使用TEXT()或_T()宏來形成適當的字符串。