hi rubyists,
is it possible to trap everything that was written to the $stdout by
system ‘ls’ for instance?
i d like to somehow read what was written to the output stream for
graphical output.
how would you do this?
Thanks,
- Meinrad
hi rubyists,
is it possible to trap everything that was written to the $stdout by
system ‘ls’ for instance?
i d like to somehow read what was written to the output stream for
graphical output.
how would you do this?
Thanks,
simple = ls
is one way… or:
another = nil
IO.popen(‘ls’,‘r’) do |f|
another = f.read
end
Is that what you were looking for? In both cases you’re capturing the
command’s standard output (but it’s not actually going to the Ruby object
$stdout)
Regards,
Brian.
On Sat, Apr 05, 2003 at 07:21:50AM +0900, meinradrecheis wrote:
hi rubyists,
is it possible to trap everything that was written to the $stdout by
system ‘ls’ for instance?
thanks brian, that sounds good. but is very complicated for capturing
the whole output of a large program.
i would like to connect a pipe to the $stdout (i have no gess how to do
it, but hope that you understand my intention) and in one location of my
main loop (in the display procedure)
i would like to read whatever is on the stream and print it to my
graphical output.
sorry for not being clear in formulation of my question.
Ah, in that case probably what you want is:
$defout = File.open(“log.txt”,“w”)
puts “hello” # goes to log file
However, any sub-processes which you spawn which write to stdout will write
their output directly to the operating system’s stdout, bypassing Ruby
altogether.
The only ways round that that I know of are:
(1) explicitly capture the stdout of the child process, using `` or IO.popen
as described before, and write it to $defout yourself
(2) when spawning the child process, redirect its stdout to a file, e.g.
system(“ls >log2.txt”)
(3) redirect stdout before you start the ruby interpreter itself, e.g.
ruby myprog.rb >log.txt
Regards,
Brian.
On Sat, Apr 05, 2003 at 06:43:04PM +0900, Meinrad Recheis wrote:
thanks brian, that sounds good. but is very complicated for capturing
the whole output of a large program.i would like to connect a pipe to the $stdout (i have no gess how to do
it, but hope that you understand my intention) and in one location of my
main loop (in the display procedure)
i would like to read whatever is on the stream and print it to my
graphical output.
$defout = File.open("log.txt","w")
puts "hello" # goes to log file
$stdout = File.open("log.txt","w")
$stdout.sync = true
$defout = $stderr = $stdout
puts "hello"
system("ls")
use #dup if you want to restore it after, you have examples in ruby-talk
Guy Decoux
ts wrote:
$stdout = File.open("log.txt","w") $stdout.sync = true $defout = $stderr = $stdout puts "hello" system("ls")
so it seems to me, that it is necessary to go the long and slowly way
over a log file.
i have annother idea:
what do you think about aliasing and overwriting all outputting methods
in IO like puts, write, print and so on? would that affect really all
output?
thanks for your help, brian and guy!
what do you think about aliasing and overwriting all outputting methods
in IO like puts, write, print and so on? would that affect really all
output?
I don't understand, sorry : you use system() this mean that the command is
executed by the OS not by ruby; OS use fprintf(), etc not the ruby methods
Guy Decoux
Hi Meinrad,
so it seems to me, that it is necessary to go the long and slowly way
over a log file.i have annother idea:
what do you think about aliasing and overwriting all outputting methods
in IO like puts, write, print and so on? would that affect really all
output?
I’m still not quite sure I’m clear on what you’re trying to do
and why, say, popen() wouldn’t do the job for you… But here’s
another popen variation that sounds possibly similar to what
you’re describing… (Did you say what Operating System you’re
on? The following won’t work on Windows, I believe, due to lack
of availibilty of fork. (Probably works under cygwin though (?))
From: “Meinrad Recheis” meinrad.recheis@gmx.dot.at
#!/usr/local/bin/ruby -w
# parent, here, can read everything child sends to stdout
def do_parent_stuff(io)
io.each {|line| puts "child emitted: #{line}" }
end
# child gets to do both ruby print statements and run
# system commands, all of whose stdout are piped to the parent
def do_child_stuff
puts "Hello!!! I'm child #{Process.pid}."
system("ls -l /usr/lib/ruby/1.6/net")
puts "Bye now!!!"
end
IO.popen("-") do |io|
io ? do_parent_stuff(io) : do_child_stuff
end
on my system the output is:
child emitted: Hello!!! I’m child 2743.
child emitted: total 172
child emitted: -rw-r–r-- 1 root root 13968 Jul 24 2002 ftp.rb
child emitted: -rw-r–r-- 1 root root 32781 Jul 24 2002 http.rb
child emitted: -rw-r–r-- 1 root root 52629 Jul 24 2002 imap.rb
child emitted: -rw-r–r-- 1 root root 14189 Jul 24 2002 pop.rb
child emitted: -rw-r–r-- 1 root root 14652 Jul 24 2002 protocol.rb
child emitted: -rw-r–r-- 1 root root 11220 Jul 24 2002 smtp.rb
child emitted: -rw-r–r-- 1 root root 19823 Jul 24 2002 telnet.rb
child emitted: Bye now!!!
Hope this helps,
Bill