Am Dienstag, 5. Oktober 2004 16:55 schrieb Christopher Smith:
> No, this is the whole idea. The LoggingStream operator<< checks against
> a constant bool called "enabled". If it's false, then it just returns
> *this (this is what is meant when I say "noops", which isn't quite
> accurate, but a fair reflection of what will happen once the optimizer
> is done with the code). If it's true, *then* it does formatting.
>
> --Chris
Do you mean you don't use the normal operator<<(ostream&, whatever)?

Then you have to declare a template-operator<<, so that every datatype with a 
proper output-operator can be logged.

template <typename T>
::log4cxx::LoggingStream& operator<<(::log4cxx::LoggingStream& out, const T& 
data)
{
  if (out.isEnabled())
  {
    out.getOStream() << data;
  }
  return out;
}

Yes I think it could work. Still out.isEnabled() (which should be inline) is 
called for each token to output, but this might be optimized away by the 
compiler.

I'm not sure, what the c++-standard tell about the type of std::endl, but if 
there is a specific type for it, the template can be specialized like this:

::log4cxx::LoggingStream& operator<<(::log4cxx::LoggingStream& out, const 
type_of_std__endl& data)
{
  out.doLogging();
}

To change loglevel you might define:
::log4cxx::LoggingStream& operator<<(::log4cxx::LoggingStream& 
out, ::log4cxx::LevelPtr& level)
{
  out.setLogLevel(level);
}

Tommi

Reply via email to