Skip to content

Commit db9780b

Browse files
danielwemaleadt
andauthored
Replace current, not global logger in safe loggers (#622)
Co-authored-by: Tim Besard <[email protected]>
1 parent b490b6f commit db9780b

File tree

3 files changed

+62
-6
lines changed

3 files changed

+62
-6
lines changed

src/utils.jl

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,16 @@ for level in [:debug, :info, :warn, :error]
6060
# NOTE: `@placeholder` in order to avoid hard-coding @__LINE__ etc
6161
macrocall.args[1] = Symbol($"@$level")
6262
quote
63-
old_logger = global_logger()
6463
io = IOContext(Core.stderr, :color=>STDERR_HAS_COLOR[])
65-
min_level = _invoked_min_enabled_level(old_logger)
66-
global_logger(Logging.ConsoleLogger(io, min_level))
67-
ret = $(esc(macrocall))
68-
global_logger(old_logger)
69-
ret
64+
# ideally we call Logging.shouldlog() here, but that is likely to yield,
65+
# so instead we rely on the min_enabled_level of the logger.
66+
# in the case of custom loggers that may be an issue, because,
67+
# they may expect Logging.shouldlog() getting called, so we use
68+
# the global_logger()'s min level which is more likely to be usable.
69+
min_level = _invoked_min_enabled_level(global_logger())
70+
with_logger(Logging.ConsoleLogger(io, min_level)) do
71+
$(esc(macrocall))
72+
end
7073
end
7174
end
7275
end

test/Project.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
Aqua = "4c88cf16-eb10-579e-8560-4a9242c79595"
33
InteractiveUtils = "b77e0a4c-d291-57a0-90e8-8db25a27a240"
44
LLVM = "929cbde3-209d-540e-8aea-75f648917ca0"
5+
Logging = "56ddb016-857b-54e1-b83d-db4d58db5568"
56
PrecompileTools = "aea7be01-6a6a-4083-8856-8a6e6704d82a"
67
REPL = "3fa0cd96-eef1-5676-8a61-b3b8758bbffb"
78
ReTestItems = "817f1d60-ba6b-4fd5-9520-3cf149f6a823"

test/util_tests.jl

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,56 @@ end
1919
@test GPUCompiler.mangle_sig(Tuple{typeof(sin), XX{Int64(-10)}}) == "_Z3sin2XXILln10EE" # "sin(XX<-10l>)"
2020
end
2121

22+
@testset "safe loggers" begin
23+
using Logging: Logging
24+
25+
struct YieldingLogger <: Logging.AbstractLogger
26+
logger::Logging.AbstractLogger
27+
YieldingLogger() = new(Logging.current_logger())
28+
end
29+
30+
function Logging.handle_message(logger::YieldingLogger, args...)
31+
yield()
32+
return Logging.handle_message(logger.logger, args...)
33+
end
34+
35+
Logging.shouldlog(::YieldingLogger, ::Any...) = true
36+
Logging.min_enabled_level(::YieldingLogger) = Logging.Debug
37+
38+
GPUCompiler.@locked function f()
39+
GPUCompiler.@safe_debug "safe_debug"
40+
GPUCompiler.@safe_info "safe_info"
41+
GPUCompiler.@safe_warn "safe_warn"
42+
GPUCompiler.@safe_error "safe_error"
43+
GPUCompiler.@safe_show "safe_show"
44+
end
45+
46+
@test begin
47+
@sync begin
48+
Threads.@spawn begin
49+
sleep(0.1)
50+
@debug "debug"
51+
sleep(0.1)
52+
@info "info"
53+
sleep(0.1)
54+
@warn "warn"
55+
sleep(0.1)
56+
@error "error"
57+
sleep(0.1)
58+
@show "show"
59+
sleep(0.1)
60+
end
61+
pipe = Pipe()
62+
Base.link_pipe!(pipe; reader_supports_async=true, writer_supports_async=true)
63+
Threads.@spawn print(stdout, read(pipe, String))
64+
Threads.@spawn Logging.with_logger(YieldingLogger()) do
65+
sleep(0.1)
66+
redirect_stdout(f, pipe)
67+
close(pipe)
68+
end
69+
end
70+
true
71+
end
72+
end
73+
2274
end

0 commit comments

Comments
 (0)