# 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:** 1\
**Showing post:** 2

<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)

---

_[View the full topic](https://discourse.julialang.org/t/print-debugging-output-only-if-some-condition-holds/51595)._
