Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,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 @@ -429,6 +440,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 @@ -1229,6 +1229,54 @@ end
end
end

@static if !isdefined(Base, Symbol("@info"))
macro info(msg, args...)
return :(info($(esc(msg)), prefix = "Info: "))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you want 0.6 equivalent formatting, perhaps skip the prefix for @info and @warn?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did it like this to be consistent with 0.7, and also to differentiate logging errors from actual errors, which prints ERROR: in all caps red.

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))))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Debug messages are invisible by default in 0.7, so I think having them visible here by default might end up being hopelessly verbose.

Maybe a simplistic global flag which disables debug by default might be a good idea?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea, we can have const DEBUG = Ref(false) in Compat, such that people can turn it on.

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 @@ -1115,6 +1115,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