On one project, we use variadic macros to format log messages.  This gives the 
functionality of printf.  The formatting is performed only if the specific log 
level is enabled. The formatted message is passed as a char* to log4cxx.

// Size of the local message format buffer
#define __MyAPP_DEBUG_MSG_LEN__ 512

// Log a message
#define MyAPP_LOG(_logger,_level, ...) {\
    char __myapp_buffer__[__MyAPP_DEBUG_MSG_LEN__]; \
    sprintf_s(__myapp_buffer__, __MyAPP_DEBUG_MSG_LEN__, __VA_ARGS__); \
    LOG4CXX_LOG(_logger,_level,__adc_buffer__);\
    }

// Gated Macros
#define MyAPP _TRACE(_logger, ...) if (logger->isTraceEnabled()) 
MyAPP_LOG(_logger,::log4cxx::Level::getTrace(), __VA_ARGS__)
#define MyAPP _DEBUG(_logger, ...) if (logger->isDebugEnabled()) 
MyAPP_LOG(_logger,::log4cxx::Level::getDebug(), __VA_ARGS__)

Etc...

Then, in code, we can use formatted log messages:

MyAPP_TRACE(logger,"message <%d, %11.5f>", someInteger, dblValue)

Printf does not handle STL strings.  So, if you want to log the contents of a 
string, you'll need to use the c_str() method to get the char*.

MyAPP_TRACE(logger,"message <%d, \"%s\">", someInteger, strValue.c_str() )


From: Matthew Bingham [mailto:texbing...@gmail.com]
Sent: Friday, January 07, 2011 7:03 AM
To: log4cxx-user@logging.apache.org
Subject: Re: double precision

Sorry for not being more specific.  If I use the following code with the 
log4cxx.xml set to use a FileAppender:

DOMConfigurator::configure("log4cxx.xml");
logger_ = Logger::getLogger("TestApp");

double tmp = 100.12345;
LOG4CXX_DEBUG(logger_,"value is "<<tmp);

The string in the log file is
"DEBUG TestApp- value is 100.123"

The value of the variable was 100.12345 but only 100.123 was logged.  How can I 
log the extra decimal places?

I know one way is to use std::stringstream, set its precision, build the string 
myself, then pass it into log4cxx.  However, I am concerned about performance 
and would hate to build the string only for it not to be logged because it is 
only a DEBUG log and need atleast INFO to log.

On Mon, Jan 3, 2011 at 10:02 PM, Matthew Bingham 
<texbing...@gmail.com<mailto:texbing...@gmail.com>> wrote:
I cannot seem to find an answer to this anywhere.  How can I increase decimal 
precision when logging?   Something like std::stringstream::precision().  Is 
there a way to do it from the xml configuration file?

Thanks

Reply via email to