Skip to content

Commit

Permalink
added squelch() to mute warn()
Browse files Browse the repository at this point in the history
  • Loading branch information
bjarthur committed May 6, 2016
1 parent fdc4a85 commit ba7ad9d
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 17 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ Language changes
* The built-in `NTuple` type has been removed; `NTuple{N,T}` is now
implemented internally as `Tuple{Vararg{T,N}}` ([#11242]).

* `squelch` can be used to mute `warn` on a per-module/function basis ([#16213]).

Command-line option changes
---------------------------

Expand Down
14 changes: 0 additions & 14 deletions base/docs/helpdb/Base.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3607,13 +3607,6 @@ behavior, including program corruption or segfaults, at any later time.
"""
unsafe_convert

"""
warn(msg)
Display a warning. Argument `msg` is a string describing the warning to be displayed.
"""
warn

"""
erfinv(x)
Expand Down Expand Up @@ -4669,13 +4662,6 @@ multiple of four, this is equivalent to a `copy`.
"""
rotl90(A, k)

"""
info(msg)
Display an informational message. Argument `msg` is a string describing the information to be displayed.
"""
info

"""
eigmin(A)
Expand Down
36 changes: 36 additions & 0 deletions base/util.jl
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,11 @@ println_with_color(color::Symbol, msg::AbstractString...) =

## warnings and messages ##

"""
info(msg...)
Display an informational message. Argument `msg` is a string describing the information to be displayed.
"""
function info(io::IO, msg...; prefix="INFO: ")
println_with_color(info_color(), io, prefix, chomp(string(msg...)))
end
Expand All @@ -340,10 +345,32 @@ info(msg...; prefix="INFO: ") = info(STDERR, msg..., prefix=prefix)
# print a warning only once

const have_warned = Set()
const do_not_warn = Set()

"""
squelch(m [, f])
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.
"""
squelch(m::Module, f::Union{Symbol,Void}=nothing) = push!(do_not_warn,(m,f))
squelch() = empty!(do_not_warn)

export squelch

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

"""
warn(msg..., [prefix="WARNING: ", once=false, key=nothing, bt=nothing, filename=nothing, lineno::Int=0])
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.
See also `squelch`.
"""
function warn(io::IO, msg...;
prefix="WARNING: ", once=false, key=nothing, bt=nothing,
filename=nothing, lineno::Int=0)
Expand All @@ -355,6 +382,15 @@ function warn(io::IO, msg...;
(key in have_warned) && return
push!(have_warned, key)
end
st = StackTraces.remove_frames!(stacktrace(), :warn)[1]
mo = st.outer_linfo.value.def.module
fu = st.func
str *= " ($mo.$fu)"
if !isempty(do_not_warn)
for sq in do_not_warn
(sq[1]==mo && (sq[2]==nothing || sq[2]==fu)) && return
end
end
print_with_color(warn_color(), io, prefix, str)
if bt !== nothing
show_backtrace(io, bt)
Expand Down
14 changes: 11 additions & 3 deletions doc/stdlib/io-network.rst
Original file line number Diff line number Diff line change
Expand Up @@ -464,17 +464,25 @@ Text I/O

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

.. function:: info(msg)
.. function:: info(msg...)

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

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

.. Docstring generated from Julia source
Display a warning. Argument ``msg`` is a string describing the warning to be displayed.
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.

See also ``squelch``\ .

.. function:: squelch(m [, f])

.. Docstring generated from Julia source
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.

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

Expand Down
19 changes: 19 additions & 0 deletions test/misc.jl
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,25 @@ let bt = backtrace()
@test contains(l, b)
end
end
module Foo
bar(io) = warn(io,"bar")
pooh(io) = warn(io,"pooh")
end
@test contains(sprint(Foo.bar), "WARNING: bar (Foo.bar)")
@test contains(sprint(Foo.pooh), "WARNING: pooh (Foo.pooh)")
squelch(Foo,:bar)
@test sprint(Foo.bar) == ""
@test contains(sprint(Foo.pooh), "WARNING: pooh (Foo.pooh)")
squelch(Foo)
@test sprint(Foo.bar) == ""
@test sprint(Foo.pooh) == ""
tmp=copy(Base.do_not_warn)
squelch()
@test contains(sprint(Foo.bar), "WARNING: bar (Foo.bar)")
@test contains(sprint(Foo.pooh), "WARNING: pooh (Foo.pooh)")
map(x->squelch(x...), setdiff(tmp,[(Foo,nothing)]))
@test sprint(Foo.bar) == ""
@test contains(sprint(Foo.pooh), "WARNING: pooh (Foo.pooh)")

# test assert() method
@test_throws AssertionError assert(false)
Expand Down

0 comments on commit ba7ad9d

Please sign in to comment.