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

updates for 0.7 and more systematic test output #26

Merged
merged 2 commits into from
Jul 6, 2018
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

[![Travis](https://travis-ci.org/JuliaIO/Suppressor.jl.svg?branch=master)](https://travis-ci.org/JuliaIO/Suppressor.jl) [![Build status](https://ci.appveyor.com/api/projects/status/e3uuqon84kt97402/branch/master?svg=true)](https://ci.appveyor.com/project/SalchiPapa/suppressor-jl/branch/master) [![CoverAlls](https://coveralls.io/repos/github/JuliaIO/Suppressor.jl/badge.svg?branch=master)](https://coveralls.io/github/JuliaIO/Suppressor.jl?branch=master) [![CodeCov](https://codecov.io/gh/JuliaIO/Suppressor.jl/branch/master/graph/badge.svg)](https://codecov.io/gh/JuliaIO/Suppressor.jl)

Julia macros for suppressing and/or capturing output (STDOUT), warnings (STDERR) or both streams at the same time.
Julia macros for suppressing and/or capturing output (`stdout`), warnings (`stderr`) or both streams at the same time.

## Installation

Expand Down
106 changes: 78 additions & 28 deletions src/Suppressor.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,50 @@ export @suppress, @suppress_out, @suppress_err
export @capture_out, @capture_err
export @color_output

haslogging() = isdefined(Base, :CoreLogging)

"""
@suppress expr

Suppress the STDOUT and STDERR streams for the given expression.
Suppress the `stdout` and `stderr` streams for the given expression.
"""
macro suppress(block)
quote
if ccall(:jl_generating_output, Cint, ()) == 0
ORIGINAL_STDOUT = STDOUT
original_stdout = stdout
out_rd, out_wr = redirect_stdout()
out_reader = @schedule read(out_rd, String)
out_reader = @async read(out_rd, String)

ORIGINAL_STDERR = STDERR
original_stderr = stderr
err_rd, err_wr = redirect_stderr()
err_reader = @schedule read(err_rd, String)
err_reader = @async read(err_rd, String)

# approach adapted from https://github.com/JuliaLang/IJulia.jl/pull/667/files
if haslogging()
logstate = Base.CoreLogging._global_logstate
logger = logstate.logger
if logger.stream == original_stderr
new_logstate = Base.CoreLogging.LogState(typeof(logger)(err_wr, logger.min_level))
Core.eval(Base.CoreLogging, Expr(:(=), :(_global_logstate), new_logstate))
end
end
end

try
$(esc(block))
finally
if ccall(:jl_generating_output, Cint, ()) == 0
redirect_stdout(ORIGINAL_STDOUT)
redirect_stdout(original_stdout)
close(out_wr)

redirect_stderr(ORIGINAL_STDERR)
redirect_stderr(original_stderr)
close(err_wr)

if haslogging()
if logger.stream == stderr
Core.eval(Base.CoreLogging, Expr(:(=), :(_global_logstate), logstate))
end
end
end
end
end
Expand All @@ -41,21 +59,21 @@ end
"""
@suppress_out expr

Suppress the STDOUT stream for the given expression.
Suppress the `stdout` stream for the given expression.
"""
macro suppress_out(block)
quote
if ccall(:jl_generating_output, Cint, ()) == 0
ORIGINAL_STDOUT = STDOUT
original_stdout = stdout
out_rd, out_wr = redirect_stdout()
out_reader = @schedule read(out_rd, String)
out_reader = @async read(out_rd, String)
end

try
$(esc(block))
finally
if ccall(:jl_generating_output, Cint, ()) == 0
redirect_stdout(ORIGINAL_STDOUT)
redirect_stdout(original_stdout)
close(out_wr)
end
end
Expand All @@ -65,22 +83,38 @@ end
"""
@suppress_err expr

Suppress the STDERR stream for the given expression.
Suppress the `stderr` stream for the given expression.
"""
macro suppress_err(block)
quote
if ccall(:jl_generating_output, Cint, ()) == 0
ORIGINAL_STDERR = STDERR
original_stderr = stderr
err_rd, err_wr = redirect_stderr()
err_reader = @schedule read(err_rd, String)
err_reader = @async read(err_rd, String)

# approach adapted from https://github.com/JuliaLang/IJulia.jl/pull/667/files
if haslogging()
logstate = Base.CoreLogging._global_logstate
logger = logstate.logger
if logger.stream == original_stderr
new_logstate = Base.CoreLogging.LogState(typeof(logger)(err_wr, logger.min_level))
Core.eval(Base.CoreLogging, Expr(:(=), :(_global_logstate), new_logstate))
end
end
end

try
$(esc(block))
finally
if ccall(:jl_generating_output, Cint, ()) == 0
redirect_stderr(ORIGINAL_STDERR)
redirect_stderr(original_stderr)
close(err_wr)

if haslogging()
if logger.stream == stderr
Core.eval(Base.CoreLogging, Expr(:(=), :(_global_logstate), logstate))
end
end
end
end
end
Expand All @@ -90,27 +124,27 @@ end
"""
@capture_out expr

Capture the STDOUT stream for the given expression.
Capture the `stdout` stream for the given expression.
"""
macro capture_out(block)
quote
if ccall(:jl_generating_output, Cint, ()) == 0
ORIGINAL_STDOUT = STDOUT
original_stdout = stdout
out_rd, out_wr = redirect_stdout()
out_reader = @schedule read(out_rd, String)
out_reader = @async read(out_rd, String)
end

try
$(esc(block))
finally
if ccall(:jl_generating_output, Cint, ()) == 0
redirect_stdout(ORIGINAL_STDOUT)
redirect_stdout(original_stdout)
close(out_wr)
end
end

if ccall(:jl_generating_output, Cint, ()) == 0
wait(out_reader)
fetch(out_reader)
else
""
end
Expand All @@ -120,27 +154,43 @@ end
"""
@capture_err expr

Capture the STDERR stream for the given expression.
Capture the `stderr` stream for the given expression.
"""
macro capture_err(block)
quote
if ccall(:jl_generating_output, Cint, ()) == 0
ORIGINAL_STDERR = STDERR
original_stderr = stderr
err_rd, err_wr = redirect_stderr()
err_reader = @schedule read(err_rd, String)
err_reader = @async read(err_rd, String)

if haslogging()
# approach adapted from https://github.com/JuliaLang/IJulia.jl/pull/667/files
logstate = Base.CoreLogging._global_logstate
logger = logstate.logger
if logger.stream == original_stderr
new_logstate = Base.CoreLogging.LogState(typeof(logger)(err_wr, logger.min_level))
Core.eval(Base.CoreLogging, Expr(:(=), :(_global_logstate), new_logstate))
end
end
end

try
$(esc(block))
finally
if ccall(:jl_generating_output, Cint, ()) == 0
redirect_stderr(ORIGINAL_STDERR)
redirect_stderr(original_stderr)
close(err_wr)

if haslogging()
if logger.stream == stderr
Core.eval(Base.CoreLogging, Expr(:(=), :(_global_logstate), logstate))
end
end
end
end

if ccall(:jl_generating_output, Cint, ()) == 0
wait(err_reader)
fetch(err_reader)
else
""
end
Expand All @@ -157,17 +207,17 @@ combination with the `@capture_*` macros:

@color_output false begin
output = @capture_err begin
warn("should get captured, not printed")
@warn "should get captured, not printed"
end
end
@test output == "WARNING: should get captured, not printed\n"
"""
macro color_output(enabled::Bool, block)
quote
prev_color = Base.have_color
eval(Base, :(have_color = $$enabled))
Core.eval(Base, :(have_color = $$enabled))
retval = $(esc(block))
eval(Base, Expr(:(=), :have_color, prev_color))
Core.eval(Base, Expr(:(=), :have_color, prev_color))

retval
end
Expand Down
Loading