Menu

#308 failure to creat log file having unicode characters in its filepath

v1.2.x
open
2
2016-08-06
2014-07-15
Saood
No
  • created a folder path for creating the log file as follows
    /Users/<someName>/私は食べに行くよ
  • I built log4cplus 1.2.2 using the following command on OSX Mavericks
    ./configure CXX="clang++ -arch i386 -stdlib=libstdc++" --with-working-locale
  • The following code is expected to create a log file in the specified path as shown in (1), here fileName value recieved is = /Users/<someName>/私は食べに行くよ/log.txt
void LogImpl::setFile(const std::wstring &fileName)
{
    log4cplus::tstring ts;
    ts.assign(fileName.begin(), fileName.end());
    log4cplus::RollingFileAppender *rfa = new log4cplus::RollingFileAppender(ts,                  
               DMLOG_MAXSIZE_PERFILE, DMLOG_MAXFILES_FORLOG);
   /* ... */
}
  • Unfortunately, the log file does not get created.
  • When i try the same thing with the non-unicode path, it works fine.

Can you please help me understand what mistake is being done in the above steps.

Discussion

  • Saood

    Saood - 2014-07-15

    What I noticed is log4cplus::tstring ts - this variable is failing to recognize unicode characters and replaces all unicode characters with alphanumerics as follows

    log4cplus::tstring "/Users/<someName>/\xc1o\xdfykLO\x88/test.log"

     
    • Václav Haisman

      Václav Haisman - 2014-07-18

      What is your source file encoding? It seems to me that your problem happens during compilation. Assuming your locale is UTF-8 based, make sure that your source is UTF-8 encoded and that your compiler know about it. Second, make sure that your application's C++ global locale is set to your UTF-8 locale.

       
      • Saood

        Saood - 2014-07-18

        I compiled log4Cplus using the following command ./configure CXX="clang++ -arch i386 -stdlib=libstdc++ -DUNICODE"

        The code below works for windows but not for Mac. What we observed was the fstream function which uses some sort of converter, this seems to be missing in Mac

        int main()
        {
            log4cplus::initialize ();
            helpers::LogLog::getLogLog()->setInternalDebugging(true);
        
            std::locale::global(std::locale(""));
            std::wstring fileName = L"/Users/<someName>/รายการถาวร";
            log4cplus::tstring ts;
            ts.assign(fileName.begin(), fileName.end());
            SharedFileAppenderPtr append_1(new RollingFileAppender(ts, 5*1024, 5,false, true));
        
        }
        

        Another simple example is the following, taken from Log4CPlus src itself, this does not work on Mac, but works on windows

        #include <iostream>
        #include <fstream>
        #define UNICODE
        
        #define LOG4CPLUS_TSTRING_TO_STRING(STRING) STRING
        #if defined (LOG4CPLUS_FSTREAM_ACCEPTS_WCHAR_T) && defined (UNICODE)
        #  define LOG4CPLUS_FSTREAM_PREFERED_FILE_NAME(X) (X)
        #else
        #  define LOG4CPLUS_FSTREAM_PREFERED_FILE_NAME(X) (LOG4CPLUS_TSTRING_TO_STRING(X))
        #endif
        
        typedef wchar_t tchar;
        typedef std::basic_ofstream<tchar> tofstream;
        typedef std::basic_ifstream<tchar> tifstream;
        typedef std::basic_string<tchar> tstring;
        
        int main(int argc, const char * argv[])
        {
            std::wstring name = L"/Users/<someName>/私は食べに行くよ/Test.log";
            tstring ts2(name.begin(), name.end());
            tofstream outStream;
        
            std::ios_base::iostate exceptionMask = outStream.exceptions() | std::ios::failbit;
            outStream.exceptions(exceptionMask);
        
            try {
                outStream.open((char*)(LOG4CPLUS_FSTREAM_PREFERED_FILE_NAME(ts2).c_str()));
            }
            catch (std::ios_base::failure& e) {
                std::cerr << e.what() << '\n';
            }
        }
        
         

        Last edit: Václav Haisman 2016-08-06
  • Václav Haisman

    Václav Haisman - 2014-07-17
    • labels: unicode --> unicode, OSX
    • Description has changed:

    Diff:

    --- old
    +++ new
    @@ -1,4 +1,3 @@
    -
     * created a folder path for creating the log file as follows 
     /Users/<someName>/私は食べに行くよ
     * I built log4cplus 1.2.2 using the following command on OSX Mavericks
    
    • assigned_to: Václav Zeman
     
  • Saood

    Saood - 2014-07-17

    Thanks Václav Zeman appreciate your response on this bug. Would it be possible for you to provide an answer asap ?

     
  • Václav Haisman

    Václav Haisman - 2016-08-06
    • Description has changed:

    Diff:

    --- old
    +++ new
    @@ -4,16 +4,16 @@
     ./configure CXX="clang++ -arch i386 -stdlib=libstdc++" --with-working-locale
     * The following code is expected to create a log file in the specified path as shown in (1), here fileName value recieved is =  /Users/<someName>/私は食べに行くよ/log.txt
    
    +```cpp
     void LogImpl::setFile(const std::wstring &fileName)
     {
         log4cplus::tstring ts;
         ts.assign(fileName.begin(), fileName.end());
         log4cplus::RollingFileAppender *rfa = new log4cplus::RollingFileAppender(ts,                  
                    DMLOG_MAXSIZE_PERFILE, DMLOG_MAXFILES_FORLOG);
    -    ...
    -    ...
    -    ...
    +   /* ... */
     }
    +```
    
     * Unfortunately, the log file does not get created.
     * When i try the same thing with the non-unicode path, it works fine.
    
     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.