Skip to content

Commit

Permalink
add Compat.at-error, Compat.at-warn, Compat.at-info and Compat.at-deb…
Browse files Browse the repository at this point in the history
…ug (#458)
  • Loading branch information
fredrikekre authored Jan 18, 2018
1 parent 37a1150 commit 0b398b8
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 0 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down
48 changes: 48 additions & 0 deletions src/Compat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down
26 changes: 26 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 0b398b8

Please sign in to comment.