Skip to content

Commit 521ae4c

Browse files
committed
add Compat.at-error, Compat.at-warn, Compat.at-info and Compat.at-debug
1 parent 2ccee51 commit 521ae4c

File tree

3 files changed

+86
-0
lines changed

3 files changed

+86
-0
lines changed

README.md

+12
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,17 @@ Currently, the `@compat` macro supports the following syntaxes:
301301

302302
* `@nospecialize` has been added ([#22666]).
303303

304+
* The logging macros `@error`, `@warn`, `@info` and `@debug` can be used as
305+
`Compat.@error`, `Compat.@warn`, `Compat.@info` and `Compat.@debug` on Julia 0.6 ([#24490]).
306+
Note that the behavior do not mirror the logging macros in Julia 0.7, instead on Julia 0.6:
307+
- Messages are printed to `STDERR` (like `info` and `warn` on Julia 0.6) and not to a
308+
dedicated logging stream.
309+
- The loglevel can not be controlled, but `Compat.@debug` messages can be turned on/off
310+
by calling `Compat.enable_debug(true/false)`.
311+
- Extra metadata sent to the macros are ignored.
312+
313+
As an alternative, see the MicroLogging.jl package for a logging interface similar to the one in Julia 0.7.
314+
304315
## Other changes
305316

306317
* On versions of Julia that do not contain a Base.Threads module, Compat defines a Threads module containing a no-op `@threads` macro.
@@ -429,6 +440,7 @@ includes this fix. Find the minimum version from there.
429440
[#24361]: https://github.com/JuliaLang/julia/issues/24361
430441
[#24372]: https://github.com/JuliaLang/julia/issues/24372
431442
[#24459]: https://github.com/JuliaLang/julia/issues/24459
443+
[#24490]: https://github.com/JuliaLang/julia/issues/24490
432444
[#24605]: https://github.com/JuliaLang/julia/issues/24605
433445
[#24647]: https://github.com/JuliaLang/julia/issues/24647
434446
[#24648]: https://github.com/JuliaLang/julia/issues/24648

src/Compat.jl

+48
Original file line numberDiff line numberDiff line change
@@ -1229,6 +1229,54 @@ end
12291229
end
12301230
end
12311231

1232+
@static if !isdefined(Base, Symbol("@info"))
1233+
macro info(msg, args...)
1234+
return :(info($(esc(msg)), prefix = "Info: "))
1235+
end
1236+
else
1237+
@eval const $(Symbol("@info")) = Base.$(Symbol("@info"))
1238+
end
1239+
@static if !isdefined(Base, Symbol("@warn"))
1240+
macro warn(msg, args...)
1241+
return :(warn($(esc(msg)), prefix = "Warning: "))
1242+
end
1243+
else
1244+
@eval const $(Symbol("@warn")) = Base.$(Symbol("@warn"))
1245+
end
1246+
1247+
const DEBUG = Ref(false) # debug printing off by default, as on 0.7
1248+
enable_debug(x::Bool) = DEBUG[] = x
1249+
@static if !isdefined(Base, Symbol("@debug"))
1250+
function debug(msg)
1251+
DEBUG[] || return
1252+
buf = IOBuffer()
1253+
iob = Base.redirect(IOContext(buf, STDERR), Base.log_info_to, :debug)
1254+
print_with_color(:blue, iob, "Debug: "; bold = true)
1255+
Base.println_with_color(:blue, iob, chomp(string(msg)))
1256+
print(STDERR, String(take!(buf)))
1257+
return
1258+
end
1259+
macro debug(msg, args...)
1260+
return :(debug($(esc(msg))))
1261+
end
1262+
else
1263+
@eval const $(Symbol("@debug")) = Base.$(Symbol("@debug"))
1264+
end
1265+
@static if !isdefined(Base, Symbol("@error"))
1266+
function _error(msg)
1267+
buf = IOBuffer()
1268+
iob = Base.redirect(IOContext(buf, STDERR), Base.log_error_to, :error)
1269+
print_with_color(Base.error_color(), iob, "Error: "; bold = true)
1270+
Base.println_with_color(Base.error_color(), iob, chomp(string(msg)))
1271+
print(STDERR, String(take!(buf)))
1272+
return
1273+
end
1274+
macro error(msg, args...)
1275+
return :(_error($(esc(msg))))
1276+
end
1277+
else
1278+
@eval const $(Symbol("@error")) = Base.$(Symbol("@error"))
1279+
end
12321280

12331281
include("deprecated.jl")
12341282

test/runtests.jl

+26
Original file line numberDiff line numberDiff line change
@@ -1115,6 +1115,32 @@ let c = CartesianIndices(1:3, 1:2), l = LinearIndices(1:3, 1:2)
11151115
@test l[vec(c)] == collect(1:6)
11161116
end
11171117

1118+
1119+
if !isdefined(Base, Symbol("@info"))
1120+
let fname = tempname()
1121+
try
1122+
open(fname, "w") do fout
1123+
redirect_stderr(fout) do
1124+
Compat.@info "A"
1125+
Compat.@warn "B"
1126+
oldstate = Compat.DEBUG[]
1127+
Compat.enable_debug(false)
1128+
Compat.@debug "C"
1129+
Compat.enable_debug(true)
1130+
Compat.@debug "D"
1131+
Compat.enable_debug(oldstate)
1132+
Compat.@error "E"
1133+
end
1134+
end
1135+
@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" :
1136+
"Info: A\nWarning: B\nDebug: D\nError: E\n")
1137+
finally
1138+
rm(fname, force=true)
1139+
end
1140+
end
1141+
end
1142+
1143+
11181144
if VERSION < v"0.6.0"
11191145
include("deprecated.jl")
11201146
end

0 commit comments

Comments
 (0)