Using utf-8 characters in log4cxx

I need to be able to use utf-8 encoded strings with log4cxx. I can just print the lines with std::cout (characters are displayed correctly). Using log4cxx, i.e. Placing strings in the macro LOG4CXX_DEBUG() using ConsoleAppender will produce "??" instead of a special character. I found one solution:

 LOG4CXX_DECODE_CHAR(logstring, str); LOG4CXX_DEBUG(logstring); 

where str is my input line, but this does not work. Does anyone have an idea how this might work? I did a little work, but did not find anything useful.

+7
source share
4 answers

you can use

 setlocale(LC_CTYPE, "UTF-8"); 

to set only character encoding without changing any other locale information.

+3
source

One solution is to use

 setlocale(LC_ALL, "en_US.UTF-8"); 

in my main function. This is fine for me, but if you want more localized applications, it will probably become difficult to track / use.

+1
source

The first answer did not work for me, the second - more than I want. So I combined the two answers:

 setlocale(LC_CTYPE, "xx_XX.UTF-8"); // or "xx_XX.utf8", it means the same 

where xx_XX is some language tag. I tried to write strings in many languages ​​with different alphabets (in LINUX, including Chinese, language from left to right and rigth-to-left); so i tried:

 setlocale(LC_CTYPE, "it_IT.UTF-8"); 

and he worked with any tested language. I can’t understand why the simple β€œUTF-8” without specifying the language xx_XX does not work, since I use UTF8 as a language-independent and should not specify it. (If someone knows the reason for this, it would be an interesting improvement in the answer). Perhaps this also depends on the Operatin system.

Finally, on Linux, you can get a list of encodings by typing on the shell:

 # locale -a | grep utf 
+1
source

I met the same problem and searched and searched. I found this post, it may work, but I don't like the setlocaleish solution. so I did more research, finally, the solution came out.

I reconfigured log4cxx and built it, the problem was solved!

add two more configuration options to log4cxx :

 ./configure --prefx=blabla --with-apr=blabla --with-apr-util=blabla --with-charset=utf-8 --with-logchar=utf-8 

Hope this helps anyone who needs it.

+1
source

All Articles