Debug checks without output

hello, is it correct way to use a large number of checks in the code without output, and then turn them off at once?

const USE_DEBUG_CODE_BLOCKS = true

macro debug_code_block(expr)
    USE_DEBUG_CODE_BLOCKS || return nothing
    return expr
end

@debug_code_block begin
    1 != 1 && throw("ooops!")
end

or how to disable the output for @debug but have the checks run?

If you change the macro to:

macro debug_code_block(expr)
    USE_DEBUG_CODE_BLOCKS || return nothing
    return :($(esc(expr)))
end

it works:

julia> const USE_DEBUG_CODE_BLOCKS = true;

julia> macro debug_code_block(expr)
           USE_DEBUG_CODE_BLOCKS || return nothing
           return :($(esc(expr)))
       end;

julia> @debug_code_block begin
           1 != 1 || throw("ooops!")
       end
ERROR: "ooops!"
Stacktrace:
 [1] top-level scope
   @ REPL[3]:2
julia> const USE_DEBUG_CODE_BLOCKS = false;

julia> macro debug_code_block(expr)
           USE_DEBUG_CODE_BLOCKS || return nothing
           return :($(esc(expr)))
       end;

julia> @debug_code_block begin
           1 != 1 || throw("ooops!")
       end

julia>

Note that you need to restart Julia when toggling like this.

3 Likes

but it is a correct way ? or use @debug or something else is better ?

I just don’t want to destroy the console with millions of debug messages

It is a way, and I don’t see anything wrong with it. I use it in some packages, e.g. in Ferrite.jl.

Note that @debug is for log messages, and @assert is currently not toggleable (see JuliaLang/julia#37874 for some work towards making it so).

1 Like

thank you