opencog / cogutil

Very low-level C++ programming utilities used by several components

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Add laziness in Logger

ngeiswei opened this issue · comments

I did test

http://www.boost.org/doc/libs/1_59_0/libs/log/doc/html/log/tutorial/trivial_filtering.html

and indeed it doesn't evaluate the message if the log is filtered out!!! I've tried to decipher the code to steal the idea and implement it in our Logger (cause I think replacing opencog's Logger by boost's would be too much work), but I can't understand what's going on, it looks like magic.

Anyway, I'm confident we can add lazy evaluation one way or another in our Logger class. I've already 2 ideas:

  1. Overload log(), fine(), debug(), etc to take a lambda
logger().fine([&](){h->toString()});  // string version
logger().fine() << [&](){h->toString()}; // stream version

instead of

logger().fine(h->toString());  // string version
logger().fine() << h->toString(); // stream version

It's a bit more verbose but it's lazy.
2. Write marcos LAZY_LOGGER_FINE, LAZY_LOGGER_DEBUG, etc

#define LAZY_LOGGER_FINE if(logger().isFineEnabled()) logger().fine()
...

which would be used that way

LAZY_LOGGER_FINE << h->toString();

It's more limited but perhaps clearer.

Ultimately it wouldn't be hard to implement both (I've already started).

What do you think?

Hmm, I made a mistake, C++ isn't Rust, the lambda should be

logger().fine([&](){return h->toString()});  // string version
logger().fine() << [&](){return h->toString()}; // stream version

This is really getting too verbose...

Maybe I'll only implement the macro version.

Oh, turns out lambdas without arguments don't need brackets

logger().fine([&]{return h->toString();});  // string version
logger().fine() << [&]{return h->toString();}; // stream version

Still too verbose though...

Yeah, I don't use any kind of syntax I can't remember, or anything that I can't easily cut-n-paste from somewhere else. Having to memorize some complex, tricky invocation for a log message would not be good.

If you can't figure out the magic, #define LAZY_LOGGER_FINE is fine .. maybe #define LOG_FINE is even better.