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 Matthew Bingham <te...@gmail.com> on 2011/01/04 05:02:28 UTC

double precision

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

Re: double precision

Posted by "Jacob L. Anawalt" <ja...@geckosoftware.com>.
On 1/3/2011 9:02 PM, Matthew Bingham 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?
>

Increase the precision of what? Your application supplied message 
(%m)? An example of a log source line and the current and wanted 
output would reduce guessing.

If it's your user supplied message, it's a string type by the time it 
hits the Logger so changing the precision would need to be done in the 
application code.

-- 
Jacob Anawalt
Gecko Software, Inc.
janawalt@geckosoftware.com
435-752-8026

Re: double precision

Posted by Thorsten Schöning <ts...@am-soft.de>.
Guten Tag Matthew Bingham,
am Freitag, 7. Januar 2011 um 14:02 schrieben Sie:

> 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.

Performance should not be an issue if you use a function, class or
something and use it directly in the macro. It only gets executed if
the level fits.

LOG4CXX_DEBUG(logger_, "value is " << buildHighPrecStringForLogging(tmp));

Depends if I understood the following right, of course. ;-)

#define LOG4CXX_DEBUG(logger, message) { \
        if (LOG4CXX_UNLIKELY(logger->isDebugEnabled())) {\
           ::log4cxx::helpers::MessageBuffer oss_; \
           logger->forcedLog(::log4cxx::Level::getDebug(), oss_.str(oss_ << message), LOG4CXX_LOCATION); }}
#else
#define LOG4CXX_DEBUG(logger, message)
#endif

Mit freundlichen Grüßen,

Thorsten Schöning

-- 
Thorsten Schöning
AM-SoFT IT-Systeme - Hameln | Potsdam | Leipzig
 
Telefon: Potsdam: 0331-743881-0
E-Mail:  tschoening@am-soft.de
Web:     http://www.am-soft.de

AM-SoFT GmbH IT-Systeme, Konsumhof 1-5, 14482 Potsdam
Amtsgericht Potsdam HRB 21278 P, Geschäftsführer: Andreas Muchow


RE: double precision

Posted by Ken Sheldon <KS...@slb.com>.
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:texbingham@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 <te...@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


Re: double precision

Posted by "Jacob L. Anawalt" <ja...@geckosoftware.com>.
On 1/7/2011 6:02 AM, Matthew Bingham wrote:
> double tmp = 100.12345;
> LOG4CXX_DEBUG(logger_,"value is "<<tmp);
>

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

LOG4CXX_DEBUG(logger_,"value is " << std::fixed << std::precision(5)
	<< tmp);

Or you could have your own macros that did those steps.

>     Is there a way to do it from the xml configuration file?
>

I still think the answer to this part of the question is no, not 
without coding your own layout handler or something that would then 
pick up settings from the config file.

-- 
Jacob Anawalt
Gecko Software, Inc.
janawalt@geckosoftware.com
435-752-8026

Re: double precision

Posted by Matthew Bingham <te...@gmail.com>.
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 <te...@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