From 0b398b8e49a09315af55a1d7d19ec29911c7bd89 Mon Sep 17 00:00:00 2001 From: Fredrik Ekre Date: Thu, 18 Jan 2018 15:23:43 +0100 Subject: [PATCH] add Compat.at-error, Compat.at-warn, Compat.at-info and Compat.at-debug (#458) --- README.md | 12 ++++++++++++ src/Compat.jl | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ test/runtests.jl | 26 ++++++++++++++++++++++++++ 3 files changed, 86 insertions(+) diff --git a/README.md b/README.md index 348d5cc7e..e0c0a520f 100644 --- a/README.md +++ b/README.md @@ -304,6 +304,17 @@ Currently, the `@compat` macro supports the following syntaxes: * `@nospecialize` has been added ([#22666]). +* The logging macros `@error`, `@warn`, `@info` and `@debug` can be used as + `Compat.@error`, `Compat.@warn`, `Compat.@info` and `Compat.@debug` on Julia 0.6 ([#24490]). + Note that the behavior do not mirror the logging macros in Julia 0.7, instead on Julia 0.6: + - Messages are printed to `STDERR` (like `info` and `warn` on Julia 0.6) and not to a + dedicated logging stream. + - The loglevel can not be controlled, but `Compat.@debug` messages can be turned on/off + by calling `Compat.enable_debug(true/false)`. + - Extra metadata sent to the macros are ignored. + + As an alternative, see the MicroLogging.jl package for a logging interface similar to the one in Julia 0.7. + ## Other changes * On versions of Julia that do not contain a Base.Threads module, Compat defines a Threads module containing a no-op `@threads` macro. @@ -432,6 +443,7 @@ includes this fix. Find the minimum version from there. [#24361]: https://github.com/JuliaLang/julia/issues/24361 [#24372]: https://github.com/JuliaLang/julia/issues/24372 [#24459]: https://github.com/JuliaLang/julia/issues/24459 +[#24490]: https://github.com/JuliaLang/julia/issues/24490 [#24605]: https://github.com/JuliaLang/julia/issues/24605 [#24647]: https://github.com/JuliaLang/julia/issues/24647 [#24648]: https://github.com/JuliaLang/julia/issues/24648 diff --git a/src/Compat.jl b/src/Compat.jl index b83d3b641..af1172741 100644 --- a/src/Compat.jl +++ b/src/Compat.jl @@ -1235,6 +1235,54 @@ end end end +@static if !isdefined(Base, Symbol("@info")) + macro info(msg, args...) + return :(info($(esc(msg)), prefix = "Info: ")) + end +else + @eval const $(Symbol("@info")) = Base.$(Symbol("@info")) +end +@static if !isdefined(Base, Symbol("@warn")) + macro warn(msg, args...) + return :(warn($(esc(msg)), prefix = "Warning: ")) + end +else + @eval const $(Symbol("@warn")) = Base.$(Symbol("@warn")) +end + +const DEBUG = Ref(false) # debug printing off by default, as on 0.7 +enable_debug(x::Bool) = DEBUG[] = x +@static if !isdefined(Base, Symbol("@debug")) + function debug(msg) + DEBUG[] || return + buf = IOBuffer() + iob = Base.redirect(IOContext(buf, STDERR), Base.log_info_to, :debug) + print_with_color(:blue, iob, "Debug: "; bold = true) + Base.println_with_color(:blue, iob, chomp(string(msg))) + print(STDERR, String(take!(buf))) + return + end + macro debug(msg, args...) + return :(debug($(esc(msg)))) + end +else + @eval const $(Symbol("@debug")) = Base.$(Symbol("@debug")) +end +@static if !isdefined(Base, Symbol("@error")) + function _error(msg) + buf = IOBuffer() + iob = Base.redirect(IOContext(buf, STDERR), Base.log_error_to, :error) + print_with_color(Base.error_color(), iob, "Error: "; bold = true) + Base.println_with_color(Base.error_color(), iob, chomp(string(msg))) + print(STDERR, String(take!(buf))) + return + end + macro error(msg, args...) + return :(_error($(esc(msg)))) + end +else + @eval const $(Symbol("@error")) = Base.$(Symbol("@error")) +end include("deprecated.jl") diff --git a/test/runtests.jl b/test/runtests.jl index fcffacefc..047c7e654 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -1119,6 +1119,32 @@ let c = CartesianIndices(1:3, 1:2), l = LinearIndices(1:3, 1:2) @test l[vec(c)] == collect(1:6) end + +if !isdefined(Base, Symbol("@info")) + let fname = tempname() + try + open(fname, "w") do fout + redirect_stderr(fout) do + Compat.@info "A" + Compat.@warn "B" + oldstate = Compat.DEBUG[] + Compat.enable_debug(false) + Compat.@debug "C" + Compat.enable_debug(true) + Compat.@debug "D" + Compat.enable_debug(oldstate) + Compat.@error "E" + end + end + @test read(fname, String) == (Base.have_color ? "\e[1m\e[36mInfo: \e[39m\e[22m\e[36mA\n\e[39m\e[1m\e[33mWarning: \e[39m\e[22m\e[33mB\e[39m\n\e[1m\e[34mDebug: \e[39m\e[22m\e[34mD\n\e[39m\e[1m\e[91mError: \e[39m\e[22m\e[91mE\n\e[39m" : + "Info: A\nWarning: B\nDebug: D\nError: E\n") + finally + rm(fname, force=true) + end + end +end + + if VERSION < v"0.6.0" include("deprecated.jl") end