How to compile print debugs only when a certain environment variable is set?

I have a bunch of debug prints all over my code. In mass, they take a ton of resources to run.

In the module (console) I do this:

(define-module (console)
  #:export (debug log warn)
  #:use-module (hoot ffi))

(define *debug* #t)

(define-foreign %debug "console" "debug" (ref string) -> none)
(define-foreign %log "console" "log" (ref string) -> none)
(define-foreign %warn "console" "warn" (ref string) -> none)

(define debug (if *debug*
                  (lambda* (s #:rest rest) (%debug (apply format #f s rest)))
                  (lambda (. _) '())))
(define* (log s #:rest rest) (%log (apply format #f s rest)))
(define* (warn s #:rest rest) (%warn (apply format #f s rest)))

Changing *debug*'s value manually is a bit of a PITA.

Is there a way of defining *debug* in such a way that by default its value would be #f, but #t when some specific environment variable is set?

There’s no built-in solution for this particular thing. On the web, there is no equivalent of UNIX environment variables, so you have to do something else. One thing you could do is (define-foreign debug-mode? "debug" "isDebugMode" -> i32) and write a host function that does… something. Maybe it checks for a <meta> tag value or maybe it does something else. Hope this helps a bit.

I was thinking of something that conditionally compiles that procedure, not changes how it runs at runtime.

Like some way of providing the guild compiler system with variables. In C compilers there are CFLAGS.



Now that I have looked into guild compile-wasm --help, maybe I could just define two alternative (debug-flag) modules.

One in ./debug-flag/debugflag.scm:

(define-module (debug-flag) #:export (*debug*))
(define *debug* #t)

The other in ./release-flag/debug-flag.scm:

(define-module (debug-flag) #:export (*debug*))
(define *debug* #f)

Then I would run either guild compile-wasm -L debug-flag … or guild compile-wasm -L release-flag ….

The (console) module which #:use-module (debug-flag) would then get the right value.

A bit of work, but it’s something!