[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

How do I implement the Unix 'tee' function for $stdout?

Larry Fast

3/26/2007 9:45:00 PM

I already discovered that I can redirect $stdout just by pointing it at
a new file handle. But what I really want is to have the output
continue to STDOUT but ALSO go into my logging file along with a pile of
trace data. This is very similar to the Unix program 'tee'

Any suggestions?

--
Posted via http://www.ruby-....

13 Answers

James Gray

3/26/2007 11:39:00 PM

0

On Mar 26, 2007, at 4:44 PM, Larry Fast wrote:

> I already discovered that I can redirect $stdout just by pointing
> it at
> a new file handle. But what I really want is to have the output
> continue to STDOUT but ALSO go into my logging file along with a
> pile of
> trace data. This is very similar to the Unix program 'tee'
>
> Any suggestions?

Sure. I showed how to replace $stdout in a post earlier today:

http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-t...

Just make a TeeIO object and stick it in that variable.

Lets us know if you are still stuck and we will give more help.

James Edward Gray II


Nick Sieger

3/27/2007 1:25:00 AM

0

On 3/26/07, Larry Fast <lfast@mdsi.ca> wrote:
> I already discovered that I can redirect $stdout just by pointing it at
> a new file handle. But what I really want is to have the output
> continue to STDOUT but ALSO go into my logging file along with a pile of
> trace data. This is very similar to the Unix program 'tee'
>
> Any suggestions?

Funny, I was just writing this code last night for the latest release
of ci_reporter. Have a look at the CI::Reporter::OutputCapture class
at line 11, and its usage on line 50:

http://tinyurl....

Cheers,
/Nick

Ara.T.Howard

3/27/2007 1:54:00 AM

0

JD Harrington

3/27/2007 3:24:00 AM

0

Hi Larry,

I realize this is *not* what you are asking, but in the interest of
self-promotion (*gag*), here's a Ruby app I wrote that functions as a
replacement for 'tee'

http://www.awk-fu.com/sof...

Getting back on topic now,
JD

On Mar 26, 2007, at 5:44 PM, Larry Fast wrote:

> I already discovered that I can redirect $stdout just by pointing
> it at
> a new file handle. But what I really want is to have the output
> continue to STDOUT but ALSO go into my logging file along with a
> pile of
> trace data. This is very similar to the Unix program 'tee'
>
> Any suggestions?
>
> --
> Posted via http://www.ruby-....
>


Larry Fast

3/27/2007 4:08:00 AM

0

Nick Sieger wrote:
> Have a look at the CI::Reporter::OutputCapture class
> at line 11, and its usage on line 50:
> http://tinyurl....

Thanks Nick,
This is *almost* what I want. I want to MERGE $stdout, $stderr and my
debugging output. If I change @captured_io to @@captured_io and move it
outside Initialize, does everything else stay the same?

The only changed section is then:
class OutputCapture < DelegateClass(IO)
def initialize(io, &assign)
super
@delegate_io = io
@assign_block = assign
@assign_block.call self
end

@@captured_io = StringIO.new

Also, in your invocation on line 50, is {|io| $stdio = io} only used for
resetting $stdout back to normal. Nothing else?

--
Posted via http://www.ruby-....

Nick Sieger

3/27/2007 4:18:00 AM

0

On 3/26/07, Larry Fast <lfast@mdsi.ca> wrote:
> Nick Sieger wrote:
> > Have a look at the CI::Reporter::OutputCapture class
> > at line 11, and its usage on line 50:
> > http://tinyurl....
>
> Thanks Nick,
> This is *almost* what I want. I want to MERGE $stdout, $stderr and my
> debugging output. If I change @captured_io to @@captured_io and move it
> outside Initialize, does everything else stay the same?
>
> The only changed section is then:
> class OutputCapture < DelegateClass(IO)
> def initialize(io, &assign)
> super
> @delegate_io = io
> @assign_block = assign
> @assign_block.call self
> end
>
> @@captured_io = StringIO.new
>
> Also, in your invocation on line 50, is {|io| $stdio = io} only used for
> resetting $stdout back to normal. Nothing else?

For the initial replacement as well as the resetting back to normal,
see the #initialize method where it is also used.

Seems like your mutation to use a @@class var should work fine.

/Nick

Larry Fast

3/27/2007 5:35:00 AM

0

Nick Sieger wrote:
> For the initial replacement as well as the resetting back to normal,
> see the #initialize method where it is also used.
> /Nick

OK, now I see it. I'm still quite green at Ruby.

... and in finish(), @captured_io.string is returning the captured text?

22 def finish
23 @assign_block.call @delegate_io
24 @captured_io.string
25 end

--
Posted via http://www.ruby-....

Robert Klemme

3/27/2007 7:55:00 AM

0

On 27.03.2007 03:54, ara.t.howard@noaa.gov wrote:
> On Tue, 27 Mar 2007, Nick Sieger wrote:
>
>> On 3/26/07, Larry Fast <lfast@mdsi.ca> wrote:
>>> I already discovered that I can redirect $stdout just by pointing it at
>>> a new file handle. But what I really want is to have the output
>>> continue to STDOUT but ALSO go into my logging file along with a pile of
>>> trace data. This is very similar to the Unix program 'tee'
>>>
>>> Any suggestions?
>>
>> Funny, I was just writing this code last night for the latest release
>> of ci_reporter. Have a look at the CI::Reporter::OutputCapture class
>> at line 11, and its usage on line 50:
>>
>> http://tinyurl....
>>
>> Cheers,
>> /Nick
>>
>
>
> class TeeIO
> attr 'tee'
>
> def initialize tee, io
> @tee = tee
> @io = io
> end
>
> def << buf
> @tee << buf
> @io << " "
> @io << buf
> end
> end

I rarely drink tea. Can we please also have a CoffeaIO? Thank you!

SCNR

robert

Nick Sieger

3/27/2007 1:46:00 PM

0

On 3/27/07, Larry Fast <lfast@mdsi.ca> wrote:
> Nick Sieger wrote:
> > For the initial replacement as well as the resetting back to normal,
> > see the #initialize method where it is also used.
> > /Nick
>
> OK, now I see it. I'm still quite green at Ruby.
>
> ... and in finish(), @captured_io.string is returning the captured text?

Yes, see the rdocs for StringIO: http://tinyurl....

/Nick

Erik Veenstra

3/27/2007 4:40:00 PM

0

> I already discovered that I can redirect $stdout just by
> pointing it at a new file handle. But what I really want is
> to have the output continue to STDOUT but ALSO go into my
> logging file along with a pile of trace data. This is very
> similar to the Unix program 'tee'

Here's the code of my own log library that does exactly what
you want.

gegroet,
Erik V. - http://www.erikve...

----------------------------------------------------------------

["$stdout", "$stderr"].each do |std|
io = eval(std)
old_write = io.method(:write)

class << io
self
end.module_eval do
define_method(:write) do |text|
unless text =~ /^[\r\n]+$/ # Because puts calls twice.
File.open("logfile.log", "a") do |f|
f.puts [std[1..-1].upcase, caller[2], text].join(" ")
end
end

old_write.call(text)
end
end
end

$stdout.puts "text on stdout"
$stderr.puts "text on stderr"

----------------------------------------------------------------