Created
June 4, 2020 14:56
-
-
Save waj/f1ca6253ac27705c85709babcd140758 to your computer and use it in GitHub Desktop.
IO wrapper to log all data passing through to a file
This file contains hidden or 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
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