Skip to content

Commit ba7ad9d

Browse files
committed
added squelch() to mute warn()
1 parent fdc4a85 commit ba7ad9d

File tree

5 files changed

+68
-17
lines changed

5 files changed

+68
-17
lines changed

NEWS.md

+2
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ Language changes
4848
* The built-in `NTuple` type has been removed; `NTuple{N,T}` is now
4949
implemented internally as `Tuple{Vararg{T,N}}` ([#11242]).
5050

51+
* `squelch` can be used to mute `warn` on a per-module/function basis ([#16213]).
52+
5153
Command-line option changes
5254
---------------------------
5355

base/docs/helpdb/Base.jl

-14
Original file line numberDiff line numberDiff line change
@@ -3607,13 +3607,6 @@ behavior, including program corruption or segfaults, at any later time.
36073607
"""
36083608
unsafe_convert
36093609

3610-
"""
3611-
warn(msg)
3612-
3613-
Display a warning. Argument `msg` is a string describing the warning to be displayed.
3614-
"""
3615-
warn
3616-
36173610
"""
36183611
erfinv(x)
36193612
@@ -4669,13 +4662,6 @@ multiple of four, this is equivalent to a `copy`.
46694662
"""
46704663
rotl90(A, k)
46714664

4672-
"""
4673-
info(msg)
4674-
4675-
Display an informational message. Argument `msg` is a string describing the information to be displayed.
4676-
"""
4677-
info
4678-
46794665
"""
46804666
eigmin(A)
46814667

base/util.jl

+36
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,11 @@ println_with_color(color::Symbol, msg::AbstractString...) =
332332

333333
## warnings and messages ##
334334

335+
"""
336+
info(msg...)
337+
338+
Display an informational message. Argument `msg` is a string describing the information to be displayed.
339+
"""
335340
function info(io::IO, msg...; prefix="INFO: ")
336341
println_with_color(info_color(), io, prefix, chomp(string(msg...)))
337342
end
@@ -340,10 +345,32 @@ info(msg...; prefix="INFO: ") = info(STDERR, msg..., prefix=prefix)
340345
# print a warning only once
341346

342347
const have_warned = Set()
348+
const do_not_warn = Set()
349+
350+
"""
351+
squelch(m [, f])
352+
353+
Add `m` to the set of modules (and specifically function `f` within `m`) for
354+
which `warn()` will be muted. See `Base.do_not_warn` for the current set.
355+
Call `squelch` with no arguments to display all warnings.
356+
"""
357+
squelch(m::Module, f::Union{Symbol,Void}=nothing) = push!(do_not_warn,(m,f))
358+
squelch() = empty!(do_not_warn)
359+
360+
export squelch
343361

344362
warn_once(io::IO, msg...) = warn(io, msg..., once=true)
345363
warn_once(msg...) = warn(STDERR, msg..., once=true)
346364

365+
"""
366+
warn(msg..., [prefix="WARNING: ", once=false, key=nothing, bt=nothing, filename=nothing, lineno::Int=0])
367+
368+
Display a warning. Argument `msg` is a string describing the warning to be displayed. Set `once` to
369+
true and specify a `key` to only display `msg` the first time `warn` is called. If `bt` is not nothing
370+
a backtrace is displayed. If `filename` is not nothing both it and `lineno` are displayed.
371+
372+
See also `squelch`.
373+
"""
347374
function warn(io::IO, msg...;
348375
prefix="WARNING: ", once=false, key=nothing, bt=nothing,
349376
filename=nothing, lineno::Int=0)
@@ -355,6 +382,15 @@ function warn(io::IO, msg...;
355382
(key in have_warned) && return
356383
push!(have_warned, key)
357384
end
385+
st = StackTraces.remove_frames!(stacktrace(), :warn)[1]
386+
mo = st.outer_linfo.value.def.module
387+
fu = st.func
388+
str *= " ($mo.$fu)"
389+
if !isempty(do_not_warn)
390+
for sq in do_not_warn
391+
(sq[1]==mo && (sq[2]==nothing || sq[2]==fu)) && return
392+
end
393+
end
358394
print_with_color(warn_color(), io, prefix, str)
359395
if bt !== nothing
360396
show_backtrace(io, bt)

doc/stdlib/io-network.rst

+11-3
Original file line numberDiff line numberDiff line change
@@ -464,17 +464,25 @@ Text I/O
464464

465465
``color`` may take any of the values ``:normal``\ , ``:bold``\ , ``:black``\ , ``:blue``\ , ``:cyan``\ , ``:green``\ , ``:magenta``\ , ``:red``\ , ``:white``\ , or ``:yellow``\ .
466466

467-
.. function:: info(msg)
467+
.. function:: info(msg...)
468468

469469
.. Docstring generated from Julia source
470470
471471
Display an informational message. Argument ``msg`` is a string describing the information to be displayed.
472472

473-
.. function:: warn(msg)
473+
.. function:: warn(msg..., [prefix="WARNING: ", once=false, key=nothing, bt=nothing, filename=nothing, lineno::Int=0])
474474

475475
.. Docstring generated from Julia source
476476
477-
Display a warning. Argument ``msg`` is a string describing the warning to be displayed.
477+
Display a warning. Argument ``msg`` is a string describing the warning to be displayed. Set ``once`` to true and specify a ``key`` to only display ``msg`` the first time ``warn`` is called. If ``bt`` is not nothing a backtrace is displayed. If ``filename`` is not nothing both it and ``lineno`` are displayed.
478+
479+
See also ``squelch``\ .
480+
481+
.. function:: squelch(m [, f])
482+
483+
.. Docstring generated from Julia source
484+
485+
Add ``m`` to the set of modules (and specifically function ``f`` within ``m``\ ) for which ``warn()`` will be muted. See ``Base.do_not_warn`` for the current set. Call ``squelch`` with no arguments to display all warnings.
478486

479487
.. function:: @printf([io::IOStream], "%Fmt", args...)
480488

test/misc.jl

+19
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,25 @@ let bt = backtrace()
2727
@test contains(l, b)
2828
end
2929
end
30+
module Foo
31+
bar(io) = warn(io,"bar")
32+
pooh(io) = warn(io,"pooh")
33+
end
34+
@test contains(sprint(Foo.bar), "WARNING: bar (Foo.bar)")
35+
@test contains(sprint(Foo.pooh), "WARNING: pooh (Foo.pooh)")
36+
squelch(Foo,:bar)
37+
@test sprint(Foo.bar) == ""
38+
@test contains(sprint(Foo.pooh), "WARNING: pooh (Foo.pooh)")
39+
squelch(Foo)
40+
@test sprint(Foo.bar) == ""
41+
@test sprint(Foo.pooh) == ""
42+
tmp=copy(Base.do_not_warn)
43+
squelch()
44+
@test contains(sprint(Foo.bar), "WARNING: bar (Foo.bar)")
45+
@test contains(sprint(Foo.pooh), "WARNING: pooh (Foo.pooh)")
46+
map(x->squelch(x...), setdiff(tmp,[(Foo,nothing)]))
47+
@test sprint(Foo.bar) == ""
48+
@test contains(sprint(Foo.pooh), "WARNING: pooh (Foo.pooh)")
3049

3150
# test assert() method
3251
@test_throws AssertionError assert(false)

0 commit comments

Comments
 (0)