Skip to content

Instantly share code, notes, and snippets.

@eregon
Created September 30, 2021 17:03
Show Gist options
  • Save eregon/797eb71fd82c8abe47da0f0d97ec89ca to your computer and use it in GitHub Desktop.
Save eregon/797eb71fd82c8abe47da0f0d97ec89ca to your computer and use it in GitHub Desktop.
command = ARGV
start = Process.clock_gettime Process::CLOCK_MONOTONIC, :millisecond
IO.popen(command, err: [:child, :out]) do |io|
while line = io.gets
now = Process.clock_gettime Process::CLOCK_MONOTONIC, :millisecond
puts "[#{"%5d" % (now - start)}] #{line}"
end
end
@eregon
Copy link
Author

eregon commented Sep 30, 2021

Example:

$ ruby time_output.rb ruby -e 'STDOUT.sync = true; sleep 0.1; STDOUT.puts "out"; sleep 0.1; STDERR.puts "err"'
[  134] out
[  234] err

Note the needed STDOUT.sync = true (or STDOUT.flush), otherwise STDOUT might be block-buffered since it's not a TTY (but a pipe).
This is currently not needed on TruffleRuby as there is no write buffering (only read buffering).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment