[racket] avoiding unnecessary work when using `log-debug` et al

From: Matthew Butterick (mb at mbtype.com)
Date: Fri Dec 12 16:32:41 EST 2014

Is there a reason not to redefine `log-debug` et al as macros that evaluate their arguments lazily?

Sort of like this;

(if ((current-logging-level) . is-equal-or-above? . 'debug)
  (log-debug arg ...)
  (void))

Because I find that my use of these functions often looks like this:

(log-debug "Value = ~a" (expensive-operation-for-logging-purposes))

The problem is that when I change to a lower logging level, the (expensive-operation) is still evaluated. 

I asked a similar question about hash-ref! not long ago. The answer was that you can get lazy evaluation by wrapping the default argument in a lambda, like so:

(hash-ref! hashtable key (λ () (expression-that-produces-value)))

But it seems there's no equivalent idiom for `log-debug` et al.

Posted on the users mailing list.