From ba7ad9d345687acb467f9a6aa8aa10000e4de789 Mon Sep 17 00:00:00 2001 From: Ben Arthur Date: Thu, 5 May 2016 10:50:46 -0400 Subject: [PATCH] added squelch() to mute warn() --- NEWS.md | 2 ++ base/docs/helpdb/Base.jl | 14 -------------- base/util.jl | 36 ++++++++++++++++++++++++++++++++++++ doc/stdlib/io-network.rst | 14 +++++++++++--- test/misc.jl | 19 +++++++++++++++++++ 5 files changed, 68 insertions(+), 17 deletions(-) diff --git a/NEWS.md b/NEWS.md index d05757004ca62..60da5bd21bead 100644 --- a/NEWS.md +++ b/NEWS.md @@ -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 --------------------------- diff --git a/base/docs/helpdb/Base.jl b/base/docs/helpdb/Base.jl index 1e50a3993b4c7..8c5ee9c0fffad 100644 --- a/base/docs/helpdb/Base.jl +++ b/base/docs/helpdb/Base.jl @@ -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) @@ -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) diff --git a/base/util.jl b/base/util.jl index 82a4396c581bc..2e5d31bc99749 100644 --- a/base/util.jl +++ b/base/util.jl @@ -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 @@ -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) @@ -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) diff --git a/doc/stdlib/io-network.rst b/doc/stdlib/io-network.rst index 1e7f5d857ea65..2887585aed108 100644 --- a/doc/stdlib/io-network.rst +++ b/doc/stdlib/io-network.rst @@ -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...) diff --git a/test/misc.jl b/test/misc.jl index 7b6cc4d8e0697..3b7b927b21917 100644 --- a/test/misc.jl +++ b/test/misc.jl @@ -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)