Tommi Maekitalo wrote:

Am Dienstag, 5. Oktober 2004 01:44 schrieb Christopher Smith:
...


LoggingStream foo(logger, WARN);
foo << "We got: " << anInt << " for a total of: " << anotherInt;
foo << " and an average of: " << aFloat << endl;

The advantage being that you don't need to an explicit "if
(logger.isEnabled(WARN)) { ... }" wrapper around the formatting code. If
the priority is disabled, then the LoggingStream just turns all
operator<<()'s into noops, while otherwise it writes to a buffer, which
it then flushes and sends on an endl or equivalent io manipulator. You
get the efficiency of using the if() to check things, but the logging
code doesn't have to take up as much screen realestate now.



This has a major performance-drawback. When processing operator<< your compiler/system/whoever can't know, you will throw away the results. It has to format all your output even if logging is disabled.


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

Reply via email to