Am Donnerstag, 23. Dezember 2004 03:14 schrieb Curt Arnold:
> On Dec 22, 2004, at 4:33 PM, Christopher Sm ith wrote:
...
> If I was doing:
>
> filestream << "We have: " << std::scientific << 3.1415926 << std::endl;
>
> At the end of the statement, I would expect that either an exception
> had been thrown (disk full, etc) or the filestream would have be left
> with the file stream using scientific notation.
>
> If I had used an explicit if, then obviously it would be silly for me
> to make an assertion about the state of the filestream after the if
> block. Since the explicit statement doesn't look conditional, the
> state of the stream after the statement should be the same as if I was
> writing to a filestream.
>
Good point. If someone writes multithreaded programs, he have to deal with
syncronisation. And if someone changes a attribute of a object, the
programmer should know, what he does.
I personally feel, that the whole logstream-concept is broken by design. I use
streams heavily, but don't use logstream. I have a macro, which encapsulates
this whole formatting-stuff. I instantiate a ostringstream, when loging is
enabled. Here is my macro-definition:
#define CXXTOOLS_LOG(level, expr) \
do { \
if (getLogger()->isEnabledFor(::log4cxx::Level::level)) \
{ \
std::ostringstream msg; \
msg << expr; \
getLogger()->log( \
::log4cxx::Level::level, \
msg.str(), \
LOG4CXX_LOCATION); \
} \
} while (false)
#define log_fatal(expr) CXXTOOLS_LOG(FATAL, expr)
#define log_error(expr) CXXTOOLS_LOG(ERROR, expr)
#define log_warn(expr) CXXTOOLS_LOG(WARN, expr)
#define log_info(expr) CXXTOOLS_LOG(INFO, expr)
#define log_debug(expr) CXXTOOLS_LOG(DEBUG, expr)
In my program I use:
log_debug("the value is " << value << " hex=" << std::hex << value);
I have the guarantee to get a fresh stream every time and when logging is
disabled there is no stream-overhead at all.
For log4cxx the best way to go is to offer a real ostream for users, who wants
to use it. Everything else has its limitations. The standard-library is
responsable for the overhead and not log4cxx.
Tommi