Sending stdout to multiple handles


D

Daniel Berger

Hi all,

How can I make it so that when I print to stdout, it actually prints to
both stdout and a file at the same time? Without redefining puts or
print or something similar, that is.

I'm trying to get the benchmark package to print to both stdout and a
file at the same time. I've been messing around with various IO
options, but I'm not hitting it, and io-tee seems to be dead.

Any ideas?

Regards,

Dan
 
Ad

Advertisements

N

nobu.nokada

Hi,

At Sun, 24 Jul 2005 13:05:58 +0900,
Daniel Berger wrote in [ruby-talk:149323]:
How can I make it so that when I print to stdout, it actually prints to
both stdout and a file at the same time? Without redefining puts or
print or something similar, that is.

Without redefining anything?

STDOUT.reopen("|tee output")
I'm trying to get the benchmark package to print to both stdout and a
file at the same time. I've been messing around with various IO
options, but I'm not hitting it, and io-tee seems to be dead.

I know nothing about io-tee, though what about this?

class IO
class Tee
attr_reader :ios
def initialize(*ios)
@ios = ios
end
def write
@ios.dup.each do |f|
f.write(str) rescue @ios.delete(f)
end
end
end
end

$stdout = IO::Tee.new(STDOUT)
class Object
remove_const:)STDOUT)
end
STDOUT = $stdout
 

Top