Skip to content

Instantly share code, notes, and snippets.

@waj
Created June 4, 2020 14:56
Show Gist options
  • Save waj/f1ca6253ac27705c85709babcd140758 to your computer and use it in GitHub Desktop.
Save waj/f1ca6253ac27705c85709babcd140758 to your computer and use it in GitHub Desktop.
IO wrapper to log all data passing through to a file
class IOLogger < IO
@@log_index = 0
def initialize(@io : IO)
@@log_index += 1
@log = File.new("log#{@@log_index}.txt", "w")
@mode = :waiting
end
def read(slice : Bytes)
bytes = @io.read(slice)
if bytes > 0
if @mode != :reading
@mode = :reading
@log.puts
@log.puts "<<<<"
end
@log.write slice[0, bytes]
@log.flush
end
bytes
end
def write(slice : Bytes)
if @mode != :writing
@mode = :writing
@log.puts
@log.puts ">>>>"
end
@log.write slice
@log.flush
@io.write(slice)
end
def close
@io.close
end
def flush
@io.flush
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment