Skip to content

Commit 5d77d1b

Browse files
committed
Deprecate old logging info(), warn(), logging()
To capture messages from Base, users should install a logger instead, for example, using `with_logger()`.
1 parent 7f434c0 commit 5d77d1b

File tree

5 files changed

+187
-207
lines changed

5 files changed

+187
-207
lines changed

base/deprecated.jl

Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2994,6 +2994,193 @@ end
29942994

29952995
@deprecate merge!(repo::LibGit2.GitRepo, args...; kwargs...) LibGit2.merge!(repo, args...; kwargs...)
29962996

2997+
# 24490 - warnings and messages
2998+
const log_info_to = Dict{Tuple{Union{Module,Void},Union{Symbol,Void}},IO}()
2999+
const log_warn_to = Dict{Tuple{Union{Module,Void},Union{Symbol,Void}},IO}()
3000+
const log_error_to = Dict{Tuple{Union{Module,Void},Union{Symbol,Void}},IO}()
3001+
3002+
function _redirect(io::IO, log_to::Dict, sf::StackTraces.StackFrame)
3003+
(sf.linfo isa Core.MethodInstance) || return io
3004+
mod = sf.linfo.def
3005+
isa(mod, Method) && (mod = mod.module)
3006+
fun = sf.func
3007+
if haskey(log_to, (mod,fun))
3008+
return log_to[(mod,fun)]
3009+
elseif haskey(log_to, (mod,nothing))
3010+
return log_to[(mod,nothing)]
3011+
elseif haskey(log_to, (nothing,nothing))
3012+
return log_to[(nothing,nothing)]
3013+
else
3014+
return io
3015+
end
3016+
end
3017+
3018+
function _redirect(io::IO, log_to::Dict, fun::Symbol)
3019+
clos = string("#",fun,"#")
3020+
kw = string("kw##",fun)
3021+
local sf
3022+
break_next_frame = false
3023+
for trace in backtrace()
3024+
stack::Vector{StackFrame} = StackTraces.lookup(trace)
3025+
filter!(frame -> !frame.from_c, stack)
3026+
for frame in stack
3027+
(frame.linfo isa Core.MethodInstance) || continue
3028+
sf = frame
3029+
break_next_frame && (@goto skip)
3030+
mod = frame.linfo.def
3031+
isa(mod, Method) && (mod = mod.module)
3032+
mod === Base || continue
3033+
sff = string(frame.func)
3034+
if frame.func == fun || startswith(sff, clos) || startswith(sff, kw)
3035+
break_next_frame = true
3036+
end
3037+
end
3038+
end
3039+
@label skip
3040+
_redirect(io, log_to, sf)
3041+
end
3042+
3043+
@inline function redirect(io::IO, log_to::Dict, arg::Union{Symbol,StackTraces.StackFrame})
3044+
if isempty(log_to)
3045+
return io
3046+
else
3047+
if length(log_to)==1 && haskey(log_to,(nothing,nothing))
3048+
return log_to[(nothing,nothing)]
3049+
else
3050+
return _redirect(io, log_to, arg)
3051+
end
3052+
end
3053+
end
3054+
3055+
"""
3056+
logging(io [, m [, f]][; kind=:all])
3057+
logging([; kind=:all])
3058+
3059+
Stream output of informational, warning, and/or error messages to `io`,
3060+
overriding what was otherwise specified. Optionally, divert stream only for
3061+
module `m`, or specifically function `f` within `m`. `kind` can be `:all` (the
3062+
default), `:info`, `:warn`, or `:error`. See `Base.log_{info,warn,error}_to`
3063+
for the current set of redirections. Call `logging` with no arguments (or just
3064+
the `kind`) to reset everything.
3065+
"""
3066+
function logging(io::IO, m::Union{Module,Void}=nothing, f::Union{Symbol,Void}=nothing;
3067+
kind::Symbol=:all)
3068+
depwarn("""`logging()` is deprecated, use `with_logger` instead to capture
3069+
messages from `Base`""", :logging)
3070+
(kind==:all || kind==:info) && (log_info_to[(m,f)] = io)
3071+
(kind==:all || kind==:warn) && (log_warn_to[(m,f)] = io)
3072+
(kind==:all || kind==:error) && (log_error_to[(m,f)] = io)
3073+
nothing
3074+
end
3075+
3076+
function logging(; kind::Symbol=:all)
3077+
depwarn("""`logging()` is deprecated, use `with_logger` instead to capture
3078+
messages from `Base`""", :logging)
3079+
(kind==:all || kind==:info) && empty!(log_info_to)
3080+
(kind==:all || kind==:warn) && empty!(log_warn_to)
3081+
(kind==:all || kind==:error) && empty!(log_error_to)
3082+
nothing
3083+
end
3084+
3085+
"""
3086+
info([io, ] msg..., [prefix="INFO: "])
3087+
3088+
Display an informational message.
3089+
Argument `msg` is a string describing the information to be displayed.
3090+
The `prefix` keyword argument can be used to override the default
3091+
prepending of `msg`.
3092+
3093+
# Examples
3094+
```jldoctest
3095+
julia> info("hello world")
3096+
INFO: hello world
3097+
3098+
julia> info("hello world"; prefix="MY INFO: ")
3099+
MY INFO: hello world
3100+
```
3101+
3102+
See also [`logging`](@ref).
3103+
"""
3104+
function info(io::IO, msg...; prefix="INFO: ")
3105+
depwarn("`info()` is deprecated, use `@info` instead.", :info)
3106+
buf = IOBuffer()
3107+
iob = redirect(IOContext(buf, io), log_info_to, :info)
3108+
print_with_color(info_color(), iob, prefix; bold = true)
3109+
println_with_color(info_color(), iob, chomp(string(msg...)))
3110+
print(io, String(take!(buf)))
3111+
return
3112+
end
3113+
info(msg...; prefix="INFO: ") = info(STDERR, msg..., prefix=prefix)
3114+
3115+
# print a warning only once
3116+
3117+
const have_warned = Set()
3118+
3119+
warn_once(io::IO, msg...) = warn(io, msg..., once=true)
3120+
warn_once(msg...) = warn(STDERR, msg..., once=true)
3121+
3122+
"""
3123+
warn([io, ] msg..., [prefix="WARNING: ", once=false, key=nothing, bt=nothing, filename=nothing, lineno::Int=0])
3124+
3125+
Display a warning. Argument `msg` is a string describing the warning to be
3126+
displayed. Set `once` to true and specify a `key` to only display `msg` the
3127+
first time `warn` is called. If `bt` is not `nothing` a backtrace is displayed.
3128+
If `filename` is not `nothing` both it and `lineno` are displayed.
3129+
3130+
See also [`logging`](@ref).
3131+
"""
3132+
function warn(io::IO, msg...;
3133+
prefix="WARNING: ", once=false, key=nothing, bt=nothing,
3134+
filename=nothing, lineno::Int=0)
3135+
depwarn("`warn()` is deprecated, use `@warn` instead.", :warn)
3136+
str = chomp(string(msg...))
3137+
if once
3138+
if key === nothing
3139+
key = str
3140+
end
3141+
(key in have_warned) && return
3142+
push!(have_warned, key)
3143+
end
3144+
buf = IOBuffer()
3145+
iob = redirect(IOContext(buf, io), log_warn_to, :warn)
3146+
print_with_color(warn_color(), iob, prefix; bold = true)
3147+
print_with_color(warn_color(), iob, str)
3148+
if bt !== nothing
3149+
show_backtrace(iob, bt)
3150+
end
3151+
if filename !== nothing
3152+
print(iob, "\nin expression starting at $filename:$lineno")
3153+
end
3154+
println(iob)
3155+
print(io, String(take!(buf)))
3156+
return
3157+
end
3158+
3159+
"""
3160+
warn(msg)
3161+
3162+
Display a warning. Argument `msg` is a string describing the warning to be displayed.
3163+
3164+
# Examples
3165+
```jldoctest
3166+
julia> warn("Beep Beep")
3167+
WARNING: Beep Beep
3168+
```
3169+
"""
3170+
warn(msg...; kw...) = warn(STDERR, msg...; kw...)
3171+
3172+
warn(io::IO, err::Exception; prefix="ERROR: ", kw...) =
3173+
warn(io, sprint(showerror, err), prefix=prefix; kw...)
3174+
3175+
warn(err::Exception; prefix="ERROR: ", kw...) =
3176+
warn(STDERR, err, prefix=prefix; kw...)
3177+
3178+
info(io::IO, err::Exception; prefix="ERROR: ", kw...) =
3179+
info(io, sprint(showerror, err), prefix=prefix; kw...)
3180+
3181+
info(err::Exception; prefix="ERROR: ", kw...) =
3182+
info(STDERR, err, prefix=prefix; kw...)
3183+
29973184
# issue #24019
29983185
@deprecate similar(a::AbstractDict) empty(a)
29993186
@deprecate similar(a::AbstractDict, ::Type{Pair{K,V}}) where {K, V} empty(a, K, V)

base/error.jl

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,6 @@ error(s::AbstractString) = throw(ErrorException(s))
3636
error(msg...)
3737
3838
Raise an `ErrorException` with the given message.
39-
40-
See also [`logging`](@ref).
4139
"""
4240
function error(s::Vararg{Any,N}) where {N}
4341
@_noinline_meta

base/util.jl

Lines changed: 0 additions & 182 deletions
Original file line numberDiff line numberDiff line change
@@ -347,188 +347,6 @@ println_with_color(color::Union{Int, Symbol}, io::IO, msg...; bold::Bool = false
347347
println_with_color(color::Union{Int, Symbol}, msg...; bold::Bool = false) =
348348
println_with_color(color, STDOUT, msg...; bold = bold)
349349

350-
## warnings and messages ##
351-
352-
const log_info_to = Dict{Tuple{Union{Module,Void},Union{Symbol,Void}},IO}()
353-
const log_warn_to = Dict{Tuple{Union{Module,Void},Union{Symbol,Void}},IO}()
354-
const log_error_to = Dict{Tuple{Union{Module,Void},Union{Symbol,Void}},IO}()
355-
356-
function _redirect(io::IO, log_to::Dict, sf::StackTraces.StackFrame)
357-
(sf.linfo isa Core.MethodInstance) || return io
358-
mod = sf.linfo.def
359-
isa(mod, Method) && (mod = mod.module)
360-
fun = sf.func
361-
if haskey(log_to, (mod,fun))
362-
return log_to[(mod,fun)]
363-
elseif haskey(log_to, (mod,nothing))
364-
return log_to[(mod,nothing)]
365-
elseif haskey(log_to, (nothing,nothing))
366-
return log_to[(nothing,nothing)]
367-
else
368-
return io
369-
end
370-
end
371-
372-
function _redirect(io::IO, log_to::Dict, fun::Symbol)
373-
clos = string("#",fun,"#")
374-
kw = string("kw##",fun)
375-
local sf
376-
break_next_frame = false
377-
for trace in backtrace()
378-
stack::Vector{StackFrame} = StackTraces.lookup(trace)
379-
filter!(frame -> !frame.from_c, stack)
380-
for frame in stack
381-
(frame.linfo isa Core.MethodInstance) || continue
382-
sf = frame
383-
break_next_frame && (@goto skip)
384-
mod = frame.linfo.def
385-
isa(mod, Method) && (mod = mod.module)
386-
mod === Base || continue
387-
sff = string(frame.func)
388-
if frame.func == fun || startswith(sff, clos) || startswith(sff, kw)
389-
break_next_frame = true
390-
end
391-
end
392-
end
393-
@label skip
394-
_redirect(io, log_to, sf)
395-
end
396-
397-
@inline function redirect(io::IO, log_to::Dict, arg::Union{Symbol,StackTraces.StackFrame})
398-
if isempty(log_to)
399-
return io
400-
else
401-
if length(log_to)==1 && haskey(log_to,(nothing,nothing))
402-
return log_to[(nothing,nothing)]
403-
else
404-
return _redirect(io, log_to, arg)
405-
end
406-
end
407-
end
408-
409-
"""
410-
logging(io [, m [, f]][; kind=:all])
411-
logging([; kind=:all])
412-
413-
Stream output of informational, warning, and/or error messages to `io`,
414-
overriding what was otherwise specified. Optionally, divert stream only for
415-
module `m`, or specifically function `f` within `m`. `kind` can be `:all` (the
416-
default), `:info`, `:warn`, or `:error`. See `Base.log_{info,warn,error}_to`
417-
for the current set of redirections. Call `logging` with no arguments (or just
418-
the `kind`) to reset everything.
419-
"""
420-
function logging(io::IO, m::Union{Module,Void}=nothing, f::Union{Symbol,Void}=nothing;
421-
kind::Symbol=:all)
422-
(kind==:all || kind==:info) && (log_info_to[(m,f)] = io)
423-
(kind==:all || kind==:warn) && (log_warn_to[(m,f)] = io)
424-
(kind==:all || kind==:error) && (log_error_to[(m,f)] = io)
425-
nothing
426-
end
427-
428-
function logging(; kind::Symbol=:all)
429-
(kind==:all || kind==:info) && empty!(log_info_to)
430-
(kind==:all || kind==:warn) && empty!(log_warn_to)
431-
(kind==:all || kind==:error) && empty!(log_error_to)
432-
nothing
433-
end
434-
435-
"""
436-
info([io, ] msg..., [prefix="INFO: "])
437-
438-
Display an informational message.
439-
Argument `msg` is a string describing the information to be displayed.
440-
The `prefix` keyword argument can be used to override the default
441-
prepending of `msg`.
442-
443-
# Examples
444-
```jldoctest
445-
julia> info("hello world")
446-
INFO: hello world
447-
448-
julia> info("hello world"; prefix="MY INFO: ")
449-
MY INFO: hello world
450-
```
451-
452-
See also [`logging`](@ref).
453-
"""
454-
function info(io::IO, msg...; prefix="INFO: ")
455-
buf = IOBuffer()
456-
iob = redirect(IOContext(buf, io), log_info_to, :info)
457-
print_with_color(info_color(), iob, prefix; bold = true)
458-
println_with_color(info_color(), iob, chomp(string(msg...)))
459-
print(io, String(take!(buf)))
460-
return
461-
end
462-
info(msg...; prefix="INFO: ") = info(STDERR, msg..., prefix=prefix)
463-
464-
# print a warning only once
465-
466-
const have_warned = Set()
467-
468-
warn_once(io::IO, msg...) = warn(io, msg..., once=true)
469-
warn_once(msg...) = warn(STDERR, msg..., once=true)
470-
471-
"""
472-
warn([io, ] msg..., [prefix="WARNING: ", once=false, key=nothing, bt=nothing, filename=nothing, lineno::Int=0])
473-
474-
Display a warning. Argument `msg` is a string describing the warning to be
475-
displayed. Set `once` to true and specify a `key` to only display `msg` the
476-
first time `warn` is called. If `bt` is not `nothing` a backtrace is displayed.
477-
If `filename` is not `nothing` both it and `lineno` are displayed.
478-
479-
See also [`logging`](@ref).
480-
"""
481-
function warn(io::IO, msg...;
482-
prefix="WARNING: ", once=false, key=nothing, bt=nothing,
483-
filename=nothing, lineno::Int=0)
484-
str = chomp(string(msg...))
485-
if once
486-
if key === nothing
487-
key = str
488-
end
489-
(key in have_warned) && return
490-
push!(have_warned, key)
491-
end
492-
buf = IOBuffer()
493-
iob = redirect(IOContext(buf, io), log_warn_to, :warn)
494-
print_with_color(warn_color(), iob, prefix; bold = true)
495-
print_with_color(warn_color(), iob, str)
496-
if bt !== nothing
497-
show_backtrace(iob, bt)
498-
end
499-
if filename !== nothing
500-
print(iob, "\nin expression starting at $filename:$lineno")
501-
end
502-
println(iob)
503-
print(io, String(take!(buf)))
504-
return
505-
end
506-
507-
"""
508-
warn(msg)
509-
510-
Display a warning. Argument `msg` is a string describing the warning to be displayed.
511-
512-
# Examples
513-
```jldoctest
514-
julia> warn("Beep Beep")
515-
WARNING: Beep Beep
516-
```
517-
"""
518-
warn(msg...; kw...) = warn(STDERR, msg...; kw...)
519-
520-
warn(io::IO, err::Exception; prefix="ERROR: ", kw...) =
521-
warn(io, sprint(showerror, err), prefix=prefix; kw...)
522-
523-
warn(err::Exception; prefix="ERROR: ", kw...) =
524-
warn(STDERR, err, prefix=prefix; kw...)
525-
526-
info(io::IO, err::Exception; prefix="ERROR: ", kw...) =
527-
info(io, sprint(showerror, err), prefix=prefix; kw...)
528-
529-
info(err::Exception; prefix="ERROR: ", kw...) =
530-
info(STDERR, err, prefix=prefix; kw...)
531-
532350
function julia_cmd(julia=joinpath(JULIA_HOME, julia_exename()))
533351
opts = JLOptions()
534352
cpu_target = unsafe_string(opts.cpu_target)

0 commit comments

Comments
 (0)