0
当我做$stdout.puts "foo"
时,我希望它转到stdout和文件。将标准从流程复制到标准输出和文件的最佳方式是什么?
我想出了:
def log_init
$stdout.sync = true
old_out = $stdout.dup
old_out.sync = true
r, w = IO.pipe
$stdout.reopen(w)
fork do
f = File.open('/tmp/test', 'a') do |f|
f.sync = true
while (sel = IO.select([r], [], [], 1000))
readfds, *rest = sel
data = readfds.first.readpartial(1024)
old_out.write(data)
f.write(data)
end
end
end
end
你能做到这一点,而不需要第二个过程?