logging-log4cxx-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Curt Arnold <carn...@apache.org>
Subject Re: LogString conversion
Date Fri, 09 May 2008 14:25:52 GMT

On May 9, 2008, at 7:46 AM, Mehta, Jenica wrote:

> Hi All,
>
> Please let me know if log4cxx 0.10.0 provides any API for converting  
> std::string to log4cxx::LogString and vice versa.
>
> Thanks & Regards,
>
> Jenica Mehta
>
>

LogString is the internal string representation within log4cxx which  
is either an std::basic_string<char> where the characters are UTF-8  
codes (default build on non-Windows platforms) or  
std::basic_string<wchar_t> (default on Windows).  Most of the commonly  
used methods are written accept many flavors of strings (std::string  
in current code page, wstrings, CFString, etc), but less frequently  
used methods require that the user explicitly between their desired  
string type and LogString.

The simplest way to convert, is the use the LOGCXX_ENCODE_CHAR and  
LOG4CXX_DECODE_CHAR macros defined in log4cxx/helpers/transcoder.h.   
ENCODE creates a std::string from a LogString and DECODE creates a  
LogString from a char.  If log4cxx is build with --with-charset=utf-8  
and --with-logchar=utf-8, the macros will simply be a reference  
definition since there is no need for conversion.  Here is an example  
(don't actually run it for obvious reasons):

void bar(LogString& param) {
    LOG4CXX_ENCODE_CHAR(string, param);
    foo(string);
}

void foo(std::string& param) {
     LOG4CXX_DECODE_CHAR(logstring, param);
     bar(logstring);
}

Mime
View raw message