Hi log4cxx --

I have a feature written, I wonder if log4cxx wants it.

there are the handy LOG4CXX_DEBUG(log, message) macros. The thing that gets me is that it won't do formatstring/args like printf.

One option:

#define LOG4CXX_DEBUG(logger, format, ...) { \
if (logger->isDebugEnabled()) { \
snprintf(logbuf_,logbuf_size_, format, ##__VA_ARGS__); \
logger->forcedLog(::log4cxx::Level::DEBUG, logbuf_, __FILE__, __LINE__);}}


But this is problematic because it depends on the gnu preprocessor to properly handle cases where __VA_ARGS__ is empty (at least the gcc manual says this is a gnu extension to C. Don't know if MS has adopted it or not. Anyway, it's not ANSI.) and it assumes that you have some logbuf_ and logbuf_size_ somewhere, not thread-safe.

So I put together a binding to boost::format that allows you to do this:

LOG4CXX_DEBUG(logger, "%d %d %d, it puts the lotion in the %s") % 1 % 2 % 3 % "basket";

which also gets the __FILE__ and __LINE__ info through to the logger and involves some pretty slick glue code to format the string and pass it to the logger without any static buffers.
It also supports UDT's, since it's boost::format. I notice that the library already uses (or can use) boost::regex, so I wonder how big of an issue that would be. If you don't know what boost::format is, you have to check it out, it's very cool. A typesafe printf for C++ that supports UDT's e.g.:


format("%s") % myclass;

will work if myclass has an associated insertion operator. Also, the boost::format approach does not depend on anything non-ansi like the printf thing above, should work fine on windows (as far as I know).

The funamental motivation here is idea that that the calls to the logger should be as easy to use as possible, so that people write lots of debug level stuff, which makes great documentation, often better than comments. There are a lot of people who just debug with printf out there, and if the logging statements are too hard to use, they'll just stick with printf, and then you have a mess (like the project I'm on on now), a mixture of printf, cout, cerr, and logging statements, egh.

If there is any interest I'll send on the full implementation (it's not very big)...

-t



Reply via email to