I write physics simulations in Julia. Quite often when building the functions that run these, I effectively ‘debug with printf’.
These introspections are useful to keep around after the initial effort to get the code working, but I was wondering what the recommended way to make the code permanent would be. Obviously for production runs of a code, you would want these suppressed, ideally with no overhead cost.
In C, this is often by done by a
#define DEBUG 1, and then
#ifdefs wrapping the
printf functions. The equivalent in Julia would be to have a
const VERBOSE=true in the global scope, and then
if VERBOSE wrapping the print functions. I guess you could also use a @macro to compact away the if/end boilerplate.
Alternatively, you could add a
function foo(...;verbose::Bool=false) keyword argument to the function. This looks to me much nicer, except for the fact that the function signature has this bit tacked on. But are there any hidden performance gremlins? Is there a way to tell Julia in the function signature, that the variable is
const, so that the compiler can avoid even touching the verbose printing code unless it’s being used?
I guess this is heading in the direction where a full-blown logging package would be sensible (with differing levels of verbosity, warn/error/info levels), but it is also nice to know what to do on a smaller scale.