You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4cxx-user@logging.apache.org by "Jara, Mike SEA" <Mi...@siemens.com> on 2008/04/24 03:12:55 UTC

Problem logging unicode MFC/ATL CString with LOG4CXX_* macros

I'm having trouble getting (Windows) MFC or ATL CStrings to log
properly, when using the LOG4CXX_* macros.  For example, this will log
the pointer's hex value, rather than the string contents:

CString sMyString(_T("Test CString"));
LOG4CXX_DEBUG(MyLogger, sMyString);

CString does implement a cast operator to LPCTSTR (wchar_t* in my case).
If I explicitly call this, the string logs properly:

CString sMyString(_T("Test CString"));
LOG4CXX_DEBUG(MyLogger, (LPCTSTR)sMyString);

I would expect this cast operator to be implicitly called inside
MessageBuffer, to make the argument comply with the function signature.
I think this is the function that should be called:

WideMessageBuffer& MessageBuffer::operator<<(const wchar_t* msg);

But when I step through in the debugger, I see that there is a global
cast operator called instead.  I assume it's a better fit, as far as the
compiler's concerned:

template<class V>
std::ostream& operator<<(MessageBuffer& os, const V& val) {
   return ((std::ostream&) os) << val;

I tried commenting this cast operator out, and then I no longer have to
cast my CString!  But I'm afraid this was probably put in for a reason,
and I hate to just hack something when I don't completely understand it.
Does anyone know if I can safetly remove this?  Looking at the change
list, I see it was added by "carnold" on 11/6/07, revision number
592542.

I'm using the last snapshot before the 0.10 release, by the way.

Any help would be appreciated.

Thanks!
Mike

Problem logging _bstr_t with LOG4CXX_* macros

Posted by "Jara, Mike SEA" <Mi...@siemens.com>.
On the topic of string logging handlers, I thought this might be of
interest, possibly for future improvements to Log4cxx.

_bstr_t is a COM string class, that will maintain a NULL string
representation if created with one.  (I don't know how the STL string
behaves, but the MFC CString keeps an empty string, "", instead of
NULL.)  Logging this will crash my program:

_bstr_t sMyStr(NULL);
LOG4CXX_DEBUG(MyLogger, sMyStr);

Log4cxx delegates the logging of "sMyStr" to std::ostream, which invokes
the "char*" cast operator, and then tries to do a "strlen" on the value.
Because the value is NULL, I get a crash.

My solution to avoiding the crash was to implement "<<" operators for
_bstr_t, with "MessageBuffer", "WideMessageBuffer", "CharMessageBuffer",
and also "std::basic_ostream<...>" as lvalues.  In my implementations
that involve delegating to the ostream, I check for NULL (after the
cast) before forwarding to the ostream.

But since I can't implement an operator for everything someone might
log, I redefined the LOG4CXX_* macros to wrap all logging calls in a
try/catch(...) block.  (If a logging system is going to fail, it should
above all, not bring the program down.)  In benchmarks, I didn't see any
performance difference with and without the try/catch, using MS VC++ 7.
If any log4cxx developers are listening, I think it might be nice to
incorporate a try/catch blocks into these macros in a future version.

If anyone has alternate solutions, I'd be interested to hear them.

Mike

RE: Problem logging unicode MFC/ATL CString with LOG4CXX_* macros

Posted by "Jara, Mike SEA" <Mi...@siemens.com>.
For those interested, here is my final solution (until I find a new
"final" solution, I guess.)

First, I had to use templated functions, because the CString in MFC is
slightly different from the CString in ATL.  Parameterizing the "Traits"
parameter of the template base class (CStringT) was the first solution I
found that worked with both.

Second, I added another function to convert wide strings to fit in a
CharMessageBuffer.  This handles the case where you start logging an
Ansi string, and then try to stream in a CStringW.
LOG4CXX_DEBUG(MyLogger, "This is a CString: " << CString(_T("Hello")))

Third, to keep with the spirit of simultaneous support of Ansi and
Unicode strings in log4cxx, I specified the char type.

One nice thing about this solution is that it can be put in an optional
header file.  It is not compiled with log4cxx, and it is up to the
including project to choose MFC or ATL CStrings.  It could therefore be
added to log4cxx, without breaking the multi-platform compatibility.


namespace log4cxx {
namespace helpers {

// -- Wide String Operators --

template<class Traits>
WideMessageBuffer& operator<<(MessageBuffer& buf, const
CStringT<wchar_t, Traits>& str)
{
	return buf << (const wchar_t*) str;
}

template<class Traits>
WideMessageBuffer& operator<<(WideMessageBuffer& buf, const
CStringT<wchar_t, Traits>& str)
{
	return buf << (const wchar_t*) str;
}

template<class Traits>
CharMessageBuffer& operator<<(CharMessageBuffer& buf, const
CStringT<wchar_t, Traits>& str)
{
	USES_CONVERSION;
	return buf << W2CA((const wchar_t*)str);
}

// -- ANSI String Operators --

template<class Traits>
CharMessageBuffer& operator<<(MessageBuffer& buf, const CStringT<char,
Traits>& str)
{
	return buf << (const char*) str;
}

template<class Traits>
CharMessageBuffer& operator<<(CharMessageBuffer& buf, const
CStringT<char, Traits>& str)
{
	return buf << (const char*) str;
}

} // namespace helpers
} // namespace log4cxx


-----Original Message-----
From: Dale King [mailto:dalewking@gmail.com] 
Sent: Thursday, April 24, 2008 4:30 AM
To: Log4CXX User
Subject: Re: Problem logging unicode MFC/ATL CString with LOG4CXX_*
macros

Which is something that should probably be added to log4cxx.hw.

On Wed, 2008-04-23 at 23:30 -0500, Curt Arnold wrote:
> On Apr 23, 2008, at 8:12 PM, Jara, Mike SEA wrote:
> 
> > I'm having trouble getting (Windows) MFC or ATL CStrings to log
> > properly, when using the LOG4CXX_* macros.  For example, this will
log
> > the pointer's hex value, rather than the string contents:
> >
> > CString sMyString(_T("Test CString"));
> > LOG4CXX_DEBUG(MyLogger, sMyString);
> >
> > CString does implement a cast operator to LPCTSTR (wchar_t* in my  
> > case).
> > If I explicitly call this, the string logs properly:
> >
> > CString sMyString(_T("Test CString"));
> > LOG4CXX_DEBUG(MyLogger, (LPCTSTR)sMyString);
> >
> > I would expect this cast operator to be implicitly called inside
> > MessageBuffer, to make the argument comply with the function  
> > signature.
> > I think this is the function that should be called:
> >
> > WideMessageBuffer& MessageBuffer::operator<<(const wchar_t* msg);
> >
> > But when I step through in the debugger, I see that there is a
global
> > cast operator called instead.  I assume it's a better fit, as far as

> > the
> > compiler's concerned:
> >
> > template<class V>
> > std::ostream& operator<<(MessageBuffer& os, const V& val) {
> >   return ((std::ostream&) os) << val;
> >
> > I tried commenting this cast operator out, and then I no longer have

> > to
> > cast my CString!  But I'm afraid this was probably put in for a  
> > reason,
> > and I hate to just hack something when I don't completely understand

> > it.
> > Does anyone know if I can safetly remove this?  Looking at the
change
> > list, I see it was added by "carnold" on 11/6/07, revision number
> > 592542.
> >
> > I'm using the last snapshot before the 0.10 release, by the way.
> >
> > Any help would be appreciated.
> >
> > Thanks!
> > Mike
> 
> 
> The template that you mentioned basically says that when attempting to

> insert, you do not recognize the right hand side as a recognized  
> string type (char*, wchar_t*, std::string, std::wstring), then use any

> available insertion operator for a std::ostream and force the rest of

> the expression to be char* based.
> 
> What you'd like to have is something like the following in scope:
> 
> #if defined(_UNICODE)
> WideMessageBuffer& operator<<(MessageBuffer& buf, const CString& str)
{
>      return buf << (const wchar_t*) str;
> }
> WideMessageBuffer& operator<<(WideMessageBuffer& buf, const CString&  
> str) {
>      return buf << (const wchar_t*) str;
> }
> #else
> CharMessageBuffer& operator<<(MessageBuffer& buf, const CString& str)
{
>      return buf << (const char*) str;
> }
> CharMessageBuffer& operator<<(CharMessageBuffer& buf, const CString&  
> str) {
>      return buf << (const char*) str;
> }
> #endif
> 
> which should take precedence over the fallback insertion operation.
> 
-- 
Dale King <da...@gmail.com>


Re: Problem logging unicode MFC/ATL CString with LOG4CXX_* macros

Posted by Dale King <da...@gmail.com>.
Which is something that should probably be added to log4cxx.hw.

On Wed, 2008-04-23 at 23:30 -0500, Curt Arnold wrote:
> On Apr 23, 2008, at 8:12 PM, Jara, Mike SEA wrote:
> 
> > I'm having trouble getting (Windows) MFC or ATL CStrings to log
> > properly, when using the LOG4CXX_* macros.  For example, this will log
> > the pointer's hex value, rather than the string contents:
> >
> > CString sMyString(_T("Test CString"));
> > LOG4CXX_DEBUG(MyLogger, sMyString);
> >
> > CString does implement a cast operator to LPCTSTR (wchar_t* in my  
> > case).
> > If I explicitly call this, the string logs properly:
> >
> > CString sMyString(_T("Test CString"));
> > LOG4CXX_DEBUG(MyLogger, (LPCTSTR)sMyString);
> >
> > I would expect this cast operator to be implicitly called inside
> > MessageBuffer, to make the argument comply with the function  
> > signature.
> > I think this is the function that should be called:
> >
> > WideMessageBuffer& MessageBuffer::operator<<(const wchar_t* msg);
> >
> > But when I step through in the debugger, I see that there is a global
> > cast operator called instead.  I assume it's a better fit, as far as  
> > the
> > compiler's concerned:
> >
> > template<class V>
> > std::ostream& operator<<(MessageBuffer& os, const V& val) {
> >   return ((std::ostream&) os) << val;
> >
> > I tried commenting this cast operator out, and then I no longer have  
> > to
> > cast my CString!  But I'm afraid this was probably put in for a  
> > reason,
> > and I hate to just hack something when I don't completely understand  
> > it.
> > Does anyone know if I can safetly remove this?  Looking at the change
> > list, I see it was added by "carnold" on 11/6/07, revision number
> > 592542.
> >
> > I'm using the last snapshot before the 0.10 release, by the way.
> >
> > Any help would be appreciated.
> >
> > Thanks!
> > Mike
> 
> 
> The template that you mentioned basically says that when attempting to  
> insert, you do not recognize the right hand side as a recognized  
> string type (char*, wchar_t*, std::string, std::wstring), then use any  
> available insertion operator for a std::ostream and force the rest of  
> the expression to be char* based.
> 
> What you'd like to have is something like the following in scope:
> 
> #if defined(_UNICODE)
> WideMessageBuffer& operator<<(MessageBuffer& buf, const CString& str) {
>      return buf << (const wchar_t*) str;
> }
> WideMessageBuffer& operator<<(WideMessageBuffer& buf, const CString&  
> str) {
>      return buf << (const wchar_t*) str;
> }
> #else
> CharMessageBuffer& operator<<(MessageBuffer& buf, const CString& str) {
>      return buf << (const char*) str;
> }
> CharMessageBuffer& operator<<(CharMessageBuffer& buf, const CString&  
> str) {
>      return buf << (const char*) str;
> }
> #endif
> 
> which should take precedence over the fallback insertion operation.
> 
-- 
Dale King <da...@gmail.com>


RE: Problem logging unicode MFC/ATL CString with LOG4CXX_* macros

Posted by "Jara, Mike SEA" <Mi...@siemens.com>.
Thanks for the tip, Curt!

Mike 

-----Original Message-----
From: Curt Arnold [mailto:carnold@apache.org] 
Sent: Wednesday, April 23, 2008 9:30 PM
To: Log4CXX User
Subject: Re: Problem logging unicode MFC/ATL CString with LOG4CXX_*
macros


On Apr 23, 2008, at 8:12 PM, Jara, Mike SEA wrote:

> I'm having trouble getting (Windows) MFC or ATL CStrings to log
> properly, when using the LOG4CXX_* macros.  For example, this will log
> the pointer's hex value, rather than the string contents:
>
> CString sMyString(_T("Test CString"));
> LOG4CXX_DEBUG(MyLogger, sMyString);
>
> CString does implement a cast operator to LPCTSTR (wchar_t* in my  
> case).
> If I explicitly call this, the string logs properly:
>
> CString sMyString(_T("Test CString"));
> LOG4CXX_DEBUG(MyLogger, (LPCTSTR)sMyString);
>
> I would expect this cast operator to be implicitly called inside
> MessageBuffer, to make the argument comply with the function  
> signature.
> I think this is the function that should be called:
>
> WideMessageBuffer& MessageBuffer::operator<<(const wchar_t* msg);
>
> But when I step through in the debugger, I see that there is a global
> cast operator called instead.  I assume it's a better fit, as far as  
> the
> compiler's concerned:
>
> template<class V>
> std::ostream& operator<<(MessageBuffer& os, const V& val) {
>   return ((std::ostream&) os) << val;
>
> I tried commenting this cast operator out, and then I no longer have  
> to
> cast my CString!  But I'm afraid this was probably put in for a  
> reason,
> and I hate to just hack something when I don't completely understand  
> it.
> Does anyone know if I can safetly remove this?  Looking at the change
> list, I see it was added by "carnold" on 11/6/07, revision number
> 592542.
>
> I'm using the last snapshot before the 0.10 release, by the way.
>
> Any help would be appreciated.
>
> Thanks!
> Mike


The template that you mentioned basically says that when attempting to  
insert, you do not recognize the right hand side as a recognized  
string type (char*, wchar_t*, std::string, std::wstring), then use any  
available insertion operator for a std::ostream and force the rest of  
the expression to be char* based.

What you'd like to have is something like the following in scope:

#if defined(_UNICODE)
WideMessageBuffer& operator<<(MessageBuffer& buf, const CString& str) {
     return buf << (const wchar_t*) str;
}
WideMessageBuffer& operator<<(WideMessageBuffer& buf, const CString&  
str) {
     return buf << (const wchar_t*) str;
}
#else
CharMessageBuffer& operator<<(MessageBuffer& buf, const CString& str) {
     return buf << (const char*) str;
}
CharMessageBuffer& operator<<(CharMessageBuffer& buf, const CString&  
str) {
     return buf << (const char*) str;
}
#endif

which should take precedence over the fallback insertion operation.


Re: Problem logging unicode MFC/ATL CString with LOG4CXX_* macros

Posted by Curt Arnold <ca...@apache.org>.
On Apr 23, 2008, at 8:12 PM, Jara, Mike SEA wrote:

> I'm having trouble getting (Windows) MFC or ATL CStrings to log
> properly, when using the LOG4CXX_* macros.  For example, this will log
> the pointer's hex value, rather than the string contents:
>
> CString sMyString(_T("Test CString"));
> LOG4CXX_DEBUG(MyLogger, sMyString);
>
> CString does implement a cast operator to LPCTSTR (wchar_t* in my  
> case).
> If I explicitly call this, the string logs properly:
>
> CString sMyString(_T("Test CString"));
> LOG4CXX_DEBUG(MyLogger, (LPCTSTR)sMyString);
>
> I would expect this cast operator to be implicitly called inside
> MessageBuffer, to make the argument comply with the function  
> signature.
> I think this is the function that should be called:
>
> WideMessageBuffer& MessageBuffer::operator<<(const wchar_t* msg);
>
> But when I step through in the debugger, I see that there is a global
> cast operator called instead.  I assume it's a better fit, as far as  
> the
> compiler's concerned:
>
> template<class V>
> std::ostream& operator<<(MessageBuffer& os, const V& val) {
>   return ((std::ostream&) os) << val;
>
> I tried commenting this cast operator out, and then I no longer have  
> to
> cast my CString!  But I'm afraid this was probably put in for a  
> reason,
> and I hate to just hack something when I don't completely understand  
> it.
> Does anyone know if I can safetly remove this?  Looking at the change
> list, I see it was added by "carnold" on 11/6/07, revision number
> 592542.
>
> I'm using the last snapshot before the 0.10 release, by the way.
>
> Any help would be appreciated.
>
> Thanks!
> Mike


The template that you mentioned basically says that when attempting to  
insert, you do not recognize the right hand side as a recognized  
string type (char*, wchar_t*, std::string, std::wstring), then use any  
available insertion operator for a std::ostream and force the rest of  
the expression to be char* based.

What you'd like to have is something like the following in scope:

#if defined(_UNICODE)
WideMessageBuffer& operator<<(MessageBuffer& buf, const CString& str) {
     return buf << (const wchar_t*) str;
}
WideMessageBuffer& operator<<(WideMessageBuffer& buf, const CString&  
str) {
     return buf << (const wchar_t*) str;
}
#else
CharMessageBuffer& operator<<(MessageBuffer& buf, const CString& str) {
     return buf << (const char*) str;
}
CharMessageBuffer& operator<<(CharMessageBuffer& buf, const CString&  
str) {
     return buf << (const char*) str;
}
#endif

which should take precedence over the fallback insertion operation.