# Print debugging output only if some condition holds

**URL:** <https://discourse.julialang.org/t/print-debugging-output-only-if-some-condition-holds/51595>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [December 10, 2020, 8:38am UTC](https://discourse.julialang.org/t/print-debugging-output-only-if-some-condition-holds/51595 "2020-12-10T08:38:09Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jae-Mo\_Lihm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jae-mo_lihm/32/20145_2.png) [@Jae-Mo\_Lihm](https://discourse.julialang.org/u/Jae-Mo_Lihm)\
**Post date:** [December 10, 2020, 8:38am UTC](https://discourse.julialang.org/t/print-debugging-output-only-if-some-condition-holds/51595/1 "2020-12-10T08:38:09Z")

</div>

I want to introduce some checks in my function. In my real code, the check is time-consuming.  
So, I want that part to run only when some global environment variable is set (e.g. `ENV["JULIA_DEBUG"] = "all"`).  
When the global variable is not set (e.g. `ENV["JULIA_DEBUG"] = ""`), I will assume that the condition is met and skip the check.

An example is the following.

```julia
function my_func(a::Int)
    @debug begin
        # Perform a time-consuming check here.
        # This is a simplified example.
        check = a + 2 < 0
        check ? "" : "a + 2 cannot be negative"
    end
    # Perform real calculation here.
    return a
end

```

Calling `my_func(-5)` writes `Debug: a cannot be negative` output, which is what I want.

But `my_func(5)` also writes `Debug: ` to the log, which is what I do not want.  
Can I make the code be completely silent in this case?

---

<div class="post-metadata">

**Author:** ![ffevotte](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ffevotte/32/6587_2.png) [@ffevotte](https://discourse.julialang.org/u/ffevotte)\
**Post date:** [December 10, 2020, 9:20am UTC](https://discourse.julialang.org/t/print-debugging-output-only-if-some-condition-holds/51595/2 "2020-12-10T09:20:46Z")

</div>

`@debug` (and other logging macros like `@info` & co) are meant to log messages, so I’m not sure that’s what you want here.

The standard `@assert` is closer to what you’d want, but AFAIK it is not so easy to control whether checks are actually performed at run-time or not. So in your case, I’d probably use something like [ToggleableAsserts](https://github.com/MasonProtter/ToggleableAsserts.jl) – see the relevant discussion about this here on discourse:

> [@@assert alternatives?](https://discourse.julialang.org/t/assert-alternatives/24775/14):
>
> You can also look at taking advantage of constant propogation to get the behavior you want. asserting() = false #making this a function results in code being invalidated and recompiled when this gets changed macro mayassert(test) esc(:(if $(@\_\_MODULE\_\_).asserting() @assert($test) end)) end f(x) = @mayassert x \< 2 If we use that in a function we can see it gets compile out julia\> @code\_llvm f(2) ; @ REPL[3]:1 within `f' define void @julia\_f\_12238(i64) { top: ret void } we can …

You example could be written something like (untested):

```julia
using ToggleableAsserts

function my_func(a::Int)
    @toggled_assert (a + 2 < 0) "a + 2 cannot be negative"
    # Perform real calculation here.
    return a
end

```

And you can call `toggle(true)` or `toggle(false)` to decide whether you want checks to actually be performed (debug mode) or be elided (performance mode)
