Skip to content

Commit 072e166

Browse files
KristofferCararslan
authored andcommitted
print output from info and warnings in one shot, to prevent interleaving when used async (#24530)
(cherry picked from commit 1cd62b3)
1 parent 3259ae2 commit 072e166

File tree

1 file changed

+13
-9
lines changed

1 file changed

+13
-9
lines changed

base/util.jl

+13-9
Original file line numberDiff line numberDiff line change
@@ -524,9 +524,11 @@ MY INFO: hello world
524524
See also [`logging`](@ref).
525525
"""
526526
function info(io::IO, msg...; prefix="INFO: ")
527-
io = redirect(io, log_info_to, :info)
528-
print_with_color(info_color(), io, prefix; bold = true)
529-
println_with_color(info_color(), io, chomp(string(msg...)))
527+
buf = IOBuffer()
528+
iob = redirect(IOContext(buf, io), log_info_to, :info)
529+
print_with_color(info_color(), iob, prefix; bold = true)
530+
println_with_color(info_color(), iob, chomp(string(msg...)))
531+
print(io, String(take!(buf)))
530532
return
531533
end
532534
info(msg...; prefix="INFO: ") = info(STDERR, msg..., prefix=prefix)
@@ -559,16 +561,18 @@ function warn(io::IO, msg...;
559561
(key in have_warned) && return
560562
push!(have_warned, key)
561563
end
562-
io = redirect(io, log_warn_to, :warn)
563-
print_with_color(warn_color(), io, prefix; bold = true)
564-
print_with_color(warn_color(), io, str)
564+
buf = IOBuffer()
565+
iob = redirect(IOContext(buf, io), log_warn_to, :warn)
566+
print_with_color(warn_color(), iob, prefix; bold = true)
567+
print_with_color(warn_color(), iob, str)
565568
if bt !== nothing
566-
show_backtrace(io, bt)
569+
show_backtrace(iob, bt)
567570
end
568571
if filename !== nothing
569-
print(io, "\nwhile loading $filename, in expression starting on line $lineno")
572+
print(iob, "\nwhile loading $filename, in expression starting on line $lineno")
570573
end
571-
println(io)
574+
println(iob)
575+
print(io, String(take!(buf)))
572576
return
573577
end
574578

0 commit comments

Comments
 (0)