Skip to content

Commit

Permalink
Fix stdio redirections for the Julia 1.7 API breakage (#7)
Browse files Browse the repository at this point in the history
Base.redirect_stdout etc are no longer generic functions, so it's not
possible to overload these anymore. Instead it's necessary to overload
the function object Base.RedirectStdStream.
  • Loading branch information
c42f authored Feb 4, 2022
1 parent 7c42167 commit 094eb66
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 25 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ jobs:
matrix:
version:
- '1.4'
- '1.5'
- '1.6'
- '1'
- 'nightly'
os:
- ubuntu-latest
Expand Down
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "Logging2"
uuid = "18cd0bd4-3461-488c-9dae-1a490b172243"
authors = ["Chris Foster <[email protected]> and contributors"]
version = "0.1.0"
version = "0.1.1"

[deps]
Logging = "56ddb016-857b-54e1-b83d-db4d58db5568"
Expand Down
73 changes: 50 additions & 23 deletions src/Logging2.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,37 +8,64 @@ include("LoggingStream.jl")
#-------------------------------------------------------------------------------
# Utilities


function _redirect_to_logger(f::Function, logger::AbstractLogger,
level_for_logs, redirect_func, prev_stream, stream_name)
result = nothing

output = LineBufferedIO(LoggingStream(logger; id=stream_name, level=level_for_logs))
rd,rw = redirect_func()
try
@sync begin
try
Threads.@spawn write(output, rd) # loops while !eof(rd)
result = f()
finally
# To close the read side of the pipe, we must close *all*
# writers. This includes `rw`, but *also* the dup'd fd
# created behind the scenes by redirect_func(). (To close
# that, must call redirect_func() here with the prev stream.)
close(rw)
redirect_func(prev_stream)
end
end
finally
close(rd)
close(output)
end
return result
end

# Incompatibility due to
# https://github.com/JuliaLang/julia/pull/39132
@static if VERSION < v"1.7"

for (redirect_func, stream_name) in [
(:redirect_stdout, :stdout),
(:redirect_stderr, :stderr)
]
@eval function Base.$redirect_func(f::Function, logger::AbstractLogger; level=Logging.Info)
result = nothing
prev_stream = $stream_name
output = LineBufferedIO(LoggingStream(logger, id=$(QuoteNode(stream_name)); level=level))
rd,rw = $redirect_func()
try
@sync begin
try
Threads.@spawn write(output, rd) # loops until !eof(rd)
result = f()
finally
# To close the read side of the pipe, we must close *all*
# writers. This includes `rw`, but *also* the dup'd fd
# created behind the scenes by redirect_func(). (To close
# that, must call redirect_func() here with the prev stream.)
close(rw)
$redirect_func(prev_stream)
end
end
finally
close(rd)
close(output)
end
return result
_redirect_to_logger(f, logger, level, $redirect_func,
$stream_name, $(QuoteNode(stream_name)))
end
end

else

function (redirect_func::Base.RedirectStdStream)(f::Function, logger::AbstractLogger; level=Logging.Info)
# See https://github.com/JuliaLang/julia/blob/294b0dfcd308b3c3f829b2040ca1e3275595e058/base/stream.jl#L1417
prev_stream, stream_name =
redirect_func.unix_fd == 0 ? (stdin, :stdin) :
redirect_func.unix_fd == 1 ? (stdout, :stdout) :
redirect_func.unix_fd == 2 ? (stderr, :stderr) :
throw(ArgumentError("Not implemented to get old handle of fd except for stdio"))

_redirect_to_logger(f, logger, level, redirect_func, prev_stream, stream_name)
end

end


@doc """
redirect_stdout(f::Function, logger::AbstractLogger)
Expand Down

2 comments on commit 094eb66

@c42f
Copy link
Member Author

@c42f c42f commented on 094eb66 Feb 4, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/53851

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.1.1 -m "<description of version>" 094eb6619aeaa8815585dbe7f33b4972f9a4ce6b
git push origin v0.1.1

Please sign in to comment.