Skip to content

Commit edf2aa2

Browse files
authored
Avoid closure in @safe_ macros (#686)
1 parent e0a95d9 commit edf2aa2

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

src/utils.jl

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,23 @@ for level in [:debug, :info, :warn, :error]
6969
# they may expect Logging.shouldlog() getting called, so we use
7070
# the global_logger()'s min level which is more likely to be usable.
7171
min_level = _invoked_min_enabled_level(global_logger())
72-
with_logger(Logging.ConsoleLogger(io, min_level)) do
73-
$(esc(macrocall))
72+
safe_logger = Logging.ConsoleLogger(io, min_level)
73+
# using with_logger would create a closure, which is incompatible with
74+
# generated functions, so instead we reproduce its implementation here
75+
safe_logstate = Base.CoreLogging.LogState(safe_logger)
76+
@static if VERSION < v"1.11-"
77+
t = current_task()
78+
old_logstate = t.logstate
79+
try
80+
t.logstate = safe_logstate
81+
$(esc(macrocall))
82+
finally
83+
t.logstate = old_logstate
84+
end
85+
else
86+
Base.ScopedValues.@with(
87+
Base.CoreLogging.CURRENT_LOGSTATE => safe_logstate, $(esc(macrocall))
88+
)
7489
end
7590
end
7691
end

0 commit comments

Comments
 (0)