On Tuesday, 22 July 2014 at 23:43:59 UTC, Robert burner Schadek wrote:
On Tuesday, 22 July 2014 at 21:52:09 UTC, linkrope wrote:
Controversial conditional logging
----------------------------
The only advantage of
   tracec(condition, "passed");
over
   if (condition) trace("passed");
would be, that a costly evaluation of the condition is omitted when there is no trace logger.
That's why the std.log proposal had 'when(lazy bool now)'.

First, I was puzzled about your argument that LOG_FIRST_N or LOG_EVERY_N would be no problem with the '...c' functions. But a look at the implementation confirmed that the std.logger has no lazy evaluation of the condition; discarding the only advantage.

passing a bool as a delegate that is the first thing that gets evaluated does not sound right.

Indeed: that's why the lazy condition should be evaluated last!

Your "will log" condition is very simple and efficient: only >= and != But you cannot know, how long the evaluation of the user-provided condition will take.

BTW: with 'globalLogLevel', 'defaultLogger.logLevel'and 'LogLevel.off' the usual 'willLog' predicate will come in handy to avoid code duplication.

While the lazy evaluation of the condition would be the only advantage over

    if (condtion) log(...);

I haven't encountered a single opportunity for conditional logging in the code of our company.

'if'/'log' is always followed by 'return', 'break', 'continue', ...

    if (condition)
    {
        log("will do something else because condition passed");
        return;
    }

Reply via email to