Lazy evaluation with ostream C++ operators

Posted by SavinG on Stack Overflow See other posts from Stack Overflow or by SavinG
Published on 2011-02-17T23:07:48Z Indexed on 2011/02/17 23:25 UTC
Read the original article Hit count: 233

Filed under:
|
|
|

I am looking for a portable way to implement lazy evaluation in C++ for logging class. Let's say that I have a simple logging function like

void syslog(int priority, const char *format, ...);

then in syslog() function we can do:

if (priority < current_priority)
  return;

so we never actually call the formatting function (sprintf). On the other hand, if we use logging stream like

log << LOG_NOTICE << "test " << 123;

all the formating is always executed, which may take a lot of time. Is there any possibility to actually use all the goodies of ostream (like custom << operator for classes, type safety, elegant syntax...) in a way that the formating is executed AFTER the logging level is checked ?

© Stack Overflow or respective owner

Related posts about c++

Related posts about logging