Quantcast
Channel: First steps - JuliaLang
Viewing all articles
Browse latest Browse all 2795

Print debugging output only if some condition holds

$
0
0

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.

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?

2 posts - 2 participants

Read full topic


Viewing all articles
Browse latest Browse all 2795

Latest Images

Trending Articles



Latest Images