Stream: helpdesk (published)

Topic: Adding to @info output in loop?


view this post on Zulip Nils (Feb 05 2024 at 13:57):

The answer is probably no, but can I somehow add to an info "box" in a loop? I.e. I have:

julia> function test()
           @info "Starting loop"
           for i  1:5
               println("\tRandom number $i is",rand())
           end
       end
test (generic function with 2 methods)

julia> test()
[ Info: Starting loop
        Random number 1 is0.9431049058632653
        Random number 2 is0.6857053427685
        Random number 3 is0.5635097298087287
        Random number 4 is0.6772063413820648
        Random number 5 is0.40036800767161795

But I want it to be printed as

julia> @info "This is \n\ta multi line\n\tinfobox"
 Info: This is
       a multi line
       infobox

Note how the "bracket" on the left hand side goes over multiple lines, grouping them visually together.

view this post on Zulip Mosè Giordano (Feb 05 2024 at 17:34):

julia> @eval @info $(join(["Starting loop"; ("Random number $(i) is $(rand())" for i in 1:5)...], "\n\t"))
 Info: Starting loop
       Random number 1 is 0.08727815644082126
       Random number 2 is 0.5143229494820801
       Random number 3 is 0.2279420608548931
       Random number 4 is 0.06105790644739839
       Random number 5 is 0.4146310646135424

view this post on Zulip Mosè Giordano (Feb 05 2024 at 17:35):

actually, don't even need @eval

julia> @info join(["Starting loop"; ("Random number $(i) is $(rand())" for i in 1:5)...], "\n\t")
 Info: Starting loop
       Random number 1 is 0.5010904789610295
       Random number 2 is 0.6292650737151477
       Random number 3 is 0.11161364590871192
       Random number 4 is 0.8046665101173005
       Random number 5 is 0.04649665030116268

view this post on Zulip Mosè Giordano (Feb 05 2024 at 17:36):

this isn't exactly doing what you ask (add to an existing @info), but perhaps dynamically building the info string is a possible strategy for your case?

view this post on Zulip Mason Protter (Feb 05 2024 at 17:37):

He wants the user to be notified at the beginning of the loop iteration though

view this post on Zulip Mason Protter (Feb 05 2024 at 17:37):

whereas that won't notify them till the end

view this post on Zulip Mason Protter (Feb 05 2024 at 17:38):

I think you could probably do something though where you println("Starting loop"), and then delete that output after the loop finishes and then do the @info

view this post on Zulip Timothy (Feb 05 2024 at 17:40):

Hmm, one thing I'm not a fan of with that style in general is the inability to visually differentiate between a single multi-line @info and multiple independent @info lines.

view this post on Zulip Fredrik Ekre (Feb 05 2024 at 18:32):

You can (probably) do this already with ProgressLogging.jl?


Last updated: Dec 28 2024 at 04:38 UTC