Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use with_logger to handle non-default loggers in suppress as well #53

Merged
merged 1 commit into from
Jun 14, 2023
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
18 changes: 14 additions & 4 deletions src/Suppressor.jl
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,18 @@ macro suppress(block)
logstate = Base.CoreLogging._global_logstate
logger = logstate.logger
if :stream in propertynames(logger) && logger.stream == original_stderr
new_logstate = Base.CoreLogging.LogState(typeof(logger)(err_wr, logger.min_level))
_logger = typeof(logger)(err_wr, logger.min_level)
new_logstate = Base.CoreLogging.LogState(_logger)
Core.eval(Base.CoreLogging, Expr(:(=), :(_global_logstate), new_logstate))
else
_logger = logger
end
end

try
$(esc(block))
Logging.with_logger(_logger) do
$(esc(block))
end
finally
if ccall(:jl_generating_output, Cint, ()) == 0
redirect_stdout(original_stdout)
Expand Down Expand Up @@ -90,13 +95,18 @@ macro suppress_err(block)
logstate = Base.CoreLogging._global_logstate
logger = logstate.logger
if :stream in propertynames(logger) && logger.stream == original_stderr
new_logstate = Base.CoreLogging.LogState(typeof(logger)(err_wr, logger.min_level))
_logger = typeof(logger)(err_wr, logger.min_level)
new_logstate = Base.CoreLogging.LogState(_logger)
Core.eval(Base.CoreLogging, Expr(:(=), :(_global_logstate), new_logstate))
else
_logger = logger
end
end

try
$(esc(block))
Logging.with_logger(_logger) do
$(esc(block))
end
finally
if ccall(:jl_generating_output, Cint, ()) == 0
redirect_stderr(original_stderr)
Expand Down
28 changes: 28 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -168,4 +168,32 @@ end
@test startswith(out, "┌ Error: @error")
end

@testset "suppress_err within with_logger" begin
mktemp() do path, io
redirect_stderr(io) do
with_logger(ConsoleLogger(stderr)) do
@suppress_err begin
@error "@error"
end
end
end
flush(io)
@test read(path, String) == ""
end
end

@testset "suppress within with_logger" begin
mktemp() do path, io
redirect_stderr(io) do
with_logger(ConsoleLogger(stderr)) do
@suppress begin
@error "@error"
end
end
end
flush(io)
@test read(path, String) == ""
end
end

end # @testset "Suppressor"