On Jun 21, 2005, at 3:39 PM, Moul, Russell G wrote:
I noticed that the LOG4CXX_DEBUG and related macros no longer support
stream concatination. Are there inherent problems with the stream
concatination in the macros or were they removed for other reasons?
Thanks
Russ
The log4cxx 0.9.7 macros had as an undocumented (without looking at
the macro source) semantic of accepting any right hand side of an
std::ostream operator<<. That is you could do:
LOG4CXX_DEBUG(logger, "Iteration " << i);
which would expand to something like:
if (logger->isDebugEnabled()) }
std::ostringstream os;
os << "Iteration " << i;
logger->debug(os.str());
}
There are a couple of issues with this behavior. First, it was very
difficult to generalize to support multiple character types, so if
you did:
LOG4CXX_DEBUG(logger, L"Iteration" << i);
You would get a message about an undeclared operator<<(const
wchar_t*). In addition, it added an unnecessary std::ostringstream
construction and destruction in the common case that the message was
simply a text literal or string.
It seemed better to split the stream insertion operators into a
separate API (the logstream) and simply the LOG4CXX_ macros to have
the equivalent semantics of
#define LOG4CXX_DEBUG(logger, msg) \
loggger->debug(msg)
But with hidden optimizations.
The logstream work stalled before really coming to a satisfactory
conclusion and there has been interest in restarting it.
log4j 1.3 has now added additional parameterized logging methods like:
logger.debug("Iteration {}",new Integer(i));
or
logger.debug("Iteration {} of {}", new Integer(i), new Integer(limit));
It would be nice to bring these over to log4cxx, but no work has been
done on that yet. I do not believe that you can support variable
number of parameters in a macro, so you'd need to pick a fixed number
of arguments and do something like:
#define LOG4CXX_PARAM_DEBUG(logger, msg, param1, param2) \
log4cxx::parameterizedDebug(logger, msg, param1, param2,
LOG4CXX_LOCATION)
with some template definition that could handle the various
permutations.