-
Notifications
You must be signed in to change notification settings - Fork 181
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add DebugLayer / IODebug <: IO, activated by verbose=3
- Loading branch information
1 parent
8dc959e
commit 20c4547
Showing
3 changed files
with
107 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
module DebugRequest | ||
|
||
import ..Layer, ..request | ||
using ..IOExtras | ||
import ..ConnectionPool: ByteView, byteview | ||
|
||
|
||
include("IODebug.jl") | ||
|
||
|
||
""" | ||
request(DebugLayer, ::IO, ::Request, body) -> HTTP.Response | ||
Wrap the `IO` stream in an `IODebug` stream and print Message data. | ||
""" | ||
abstract type DebugLayer{Next <:Layer} <: Layer end | ||
export DebugLayer | ||
|
||
|
||
function request(::Type{DebugLayer{Next}}, io::IO, req, body; kw...) where Next | ||
|
||
iod = IODebug(io) | ||
|
||
try | ||
return request(Next, iod, req, body; kw...) | ||
finally | ||
print(STDOUT, iod) | ||
end | ||
end | ||
|
||
|
||
end # module DebugRequest |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
struct IODebug{T <: IO} <: IO | ||
io::T | ||
log::Vector{Tuple{String,String}} | ||
end | ||
|
||
IODebug(io::T) where T <: IO = IODebug{T}(io, []) | ||
|
||
logwrite(iod::IODebug, x) = push!(iod.log, ("➡️ ", x)) | ||
logread(iod::IODebug, x) = push!(iod.log, ("⬅️ ", x)) | ||
logunread(iod::IODebug, x) = push!(iod.log, ("♻️ ", x)) | ||
|
||
Base.write(iod::IODebug, a...) = (logwrite(iod, join(a)); write(iod.io, a...)) | ||
|
||
Base.write(iod::IODebug, x::String) = (logwrite(iod, x); write(iod.io, x)) | ||
|
||
Base.unsafe_write(iod::IODebug, x::Ptr{UInt8}, n::UInt) = | ||
(logwrite(iod, unsafe_string(x,n)); | ||
unsafe_write(iod.io, x, n)) | ||
|
||
Base.read(iod::IODebug, n) = | ||
(r = read(iod.io, n); | ||
logread(iod, String(r)); r) | ||
|
||
Base.readavailable(iod::IODebug) = | ||
(r = readavailable(iod.io); | ||
logread(iod, String(r)); r) | ||
|
||
IOExtras.unread!(iod::IODebug, bytes) = | ||
(logunread(iod, String(bytes)); | ||
unread!(iod.io, bytes)) | ||
|
||
Base.eof(iod::IODebug) = eof(iod.io) | ||
Base.close(iod::IODebug) = close(iod.io) | ||
Base.isopen(iod::IODebug) = isopen(iod.io) | ||
Base.iswritable(iod::IODebug) = iswritable(iod.io) | ||
Base.isreadable(iod::IODebug) = isreadable(iod.io) | ||
IOExtras.startread(iod::IODebug) = startread(iod.io) | ||
IOExtras.startwrite(iod::IODebug) = startwrite(iod.io) | ||
IOExtras.closeread(iod::IODebug) = closeread(iod.io) | ||
IOExtras.closewrite(iod::IODebug) = closewrite(iod.io) | ||
|
||
@static if isdefined(Base, :bytesavailable) | ||
Base.bytesavailable(iod::IODebug) = bytesavailable(iod.io) | ||
else | ||
Base.nb_available(iod::IODebug) = nb_available(iod.io) | ||
end | ||
|
||
function Base.show(io::IO, iod::IODebug) | ||
lock(io) | ||
println(io, "$(typeof(iod)):\nio: $(iod.io)") | ||
prevop = "" | ||
for (operation, bytes) in iod.log | ||
if prevop != "" && prevop != operation | ||
println(io) | ||
end | ||
prefix = rpad(operation, 5) | ||
i = j = 1 | ||
while i < length(bytes) | ||
j = findnext(bytes, '\n', i) | ||
if j == nothing || j == 0 | ||
j = length(bytes) | ||
end | ||
println(io, prefix, "\"", escape_string(bytes[i:j]), "\"") | ||
prefix = " " | ||
i = nextind(bytes, j) | ||
end | ||
prevop = operation | ||
end | ||
println(io) | ||
unlock(io) | ||
end |