[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Logging stdout/stderr/stdin of an spawn process (Open4::spawn

Edgardo Hames

4/29/2008 3:05:00 PM

Hi dear rubyists,

I apologize for reposting this message, but I'm not sure if the
previous one did get to the mailing list.

I need to log all the input (typed by the user) and the stdout/stderr
of a process. The #script method below should behave pretty much like
the Unix script application. This way, my application can issue
several commands, show their output to the user an let them whatever
is needed.

def script cmd, log
# prepare $stdin to save its content into log
# prepare $stdout to save its content into log
# prepare $stderr to save its content into log
status = Open4::spawn cmd, 'stdin' => $stdin, 'stdout' =>
$stdout, 'stderr' => $stderr
end

I've seen some implementations of a #tee function or Tee class, but
they don't seem to work with spawn. What would you suggest?

Thanks in advance,
Ed

6 Answers

5393 Dead, 526 since 1/20/09

9/28/2007 4:43:00 PM

0

On Fri, 28 Sep 2007 10:36:35 -0500, Jim Alder <jimalder@ssnet.com>
wrote:

>"Ron O'Neal" <roneal@wt.net> wrote in news:46fd10bc$0$3159$ae4e5890
>@news.nationwide.net:
>
>> "Harry Dope" <DumbassliberalShill@aol.com> wrote...
>>
>>> Clinton campaign kills negative story
>>>
>>>
>>>
>>> Early this summer, Sen. Hillary Rodham Clinton's campaign for
>>> president learned that the men's magazine GQ was working on a story the
>>> campaign was sure to hate: an account of infighting in Hillaryland.
>>>
>>> So Clinton's aides pulled a page from the book of Hollywood
>>> publicists
>>> and offered GQ a stark choice: Kill the piece, or lose access to planned
>>> celebrity coverboy Bill Clinton.
>>
>> <snipped>
>>
>>
>>
>> I don't like to be picayunish, but in your case I'll make exceptions. In
>> your Subject Line: It's "Herr", not "Heir".
>
> I thought perhaps he was implying that she hoped to inherit the presidency.
>
>> Trivial things like this make you look even more stupid than the content of
>> your posts.
>
> Since "Herr" means "Mister" and he is talking about Hillary you aren't
>doing all that well yourself. Maybe he'll clarify things for us both.

It's come to this: right wingers are heirheads.

Faggy Bush

9/28/2007 5:00:00 PM

0


"Jim Alder" <jimalder@ssnet.com> wrote in message
news:Xns99B97619B75A8jimaldersssnetcom@216.196.97.142...
> "Ron O'Neal" <roneal@wt.net> wrote in news:46fd10bc$0$3159$ae4e5890
> @news.nationwide.net:
>
>> "Harry Dope" <DumbassliberalShill@aol.com> wrote...
>>
>>> Clinton campaign kills negative story
>>>
>>>
>>>
>>> Early this summer, Sen. Hillary Rodham Clinton's campaign for
>>> president learned that the men's magazine GQ was working on a story the
>>> campaign was sure to hate: an account of infighting in Hillaryland.
>>>
>>> So Clinton's aides pulled a page from the book of Hollywood
>>> publicists
>>> and offered GQ a stark choice: Kill the piece, or lose access to planned
>>> celebrity coverboy Bill Clinton.
>>
>> <snipped>
>>
>>
>>
>> I don't like to be picayunish, but in your case I'll make exceptions. In
>> your Subject Line: It's "Herr", not "Heir".
>
> I thought perhaps he was implying that she hoped to inherit the
> presidency.
>
>> Trivial things like this make you look even more stupid than the content
>> of
>> your posts.
>
> Since "Herr" means "Mister" and he is talking about Hillary you aren't
> doing all that well yourself. Maybe he'll clarify things for us both.
>
> --
> President Bush was so buoyed by the warm reception he was given in Albania
> that he immediately gave all 3 million Albanians American citizenship,
> provided they learn Spanish. - Ann Coulter

Ann Coulter the dyke, haha.


Jim Alder

9/28/2007 11:07:00 PM

0

Trampdad <trampdad@aol.com> wrote in news:1191014108.400079.243110@
22g2000hsm.googlegroups.com:

> On Sep 28, 11:36?am, Jim Alder <jimal...@ssnet.com> wrote:
>> "Ron O'Neal" <ron...@wt.net> wrote in news:46fd10bc$0$3159$ae4e5890
>> @news.nationwide.net:
>>
>>
>>
>>
>>
>> > "Harry Dope" <DumbassliberalSh...@aol.com> wrote...
>>
>> >> Clinton campaign kills negative story
>>
>> >> Early this summer, Sen. Hillary Rodham Clinton's campaign for
>> >> president learned that the men's magazine GQ was working on a story the
>> >> campaign was sure to hate: an account of infighting in Hillaryland.
>>
>> >> So Clinton's aides pulled a page from the book of Hollywood
>> >> publicists
>> >> and offered GQ a stark choice: Kill the piece, or lose access to planned
>> >> celebrity coverboy Bill Clinton.
>>
>> > <snipped>
>>
>> > I don't like to be picayunish, but in your case I'll make exceptions. In
>> > your Subject Line: It's "Herr", not "Heir".
>>
>> I thought perhaps he was implying that she hoped to inherit the
presidency.
>>
>> > Trivial things like this make you look even more stupid than the content
of
>> > your posts.
>>
>> Since "Herr" means "Mister" and he is talking about Hillary you aren't
>> doing all that well yourself. Maybe he'll clarify things for us both.
>
> I'm thinking he means "heiress".
>
> But it is Harry Dope we're talking about, it's damn near impossible
> for him to look even more stupid.

Oh, he certainly could. He could emulate any one of several dozen
leftwingers on here.

--
President Bush was so buoyed by the warm reception he was given in Albania
that he immediately gave all 3 million Albanians American citizenship,
provided they learn Spanish. - Ann Coulter

roneal

9/29/2007 7:18:00 AM

0


"Jim Alder" <jimalder@ssnet.com> wrote in message
news:Xns99B97619B75A8jimaldersssnetcom@216.196.97.142...
> "Ron O'Neal" <roneal@wt.net> wrote in news:46fd10bc$0$3159$ae4e5890
> @news.nationwide.net:
>
> > "Harry Dope" <DumbassliberalShill@aol.com> wrote...
> >
> >> Clinton campaign kills negative story
> >>
> >>
> >>
> >> Early this summer, Sen. Hillary Rodham Clinton's campaign for
> >> president learned that the men's magazine GQ was working on a story the
> >> campaign was sure to hate: an account of infighting in Hillaryland.
> >>
> >> So Clinton's aides pulled a page from the book of Hollywood
> >> publicists
> >> and offered GQ a stark choice: Kill the piece, or lose access to
planned
> >> celebrity coverboy Bill Clinton.
> >
> > <snipped>
> >
> >
> >
> > I don't like to be picayunish, but in your case I'll make exceptions.
In
> > your Subject Line: It's "Herr", not "Heir".
>
> I thought perhaps he was implying that she hoped to inherit the
presidency.
>
> > Trivial things like this make you look even more stupid than the content
of
> > your posts.
>
> Since "Herr" means "Mister" and he is talking about Hillary you aren't
> doing all that well yourself. Maybe he'll clarify things for us both.
>
> --
> President Bush was so buoyed by the warm reception he was given in Albania
> that he immediately gave all 3 million Albanians American citizenship,
> provided they learn Spanish. - Ann Coulter

Re: I thought perhaps he was implying that she hoped to inherit the
presidency.

Naahhh....Dope isn't that subtle.

Re: Since "Herr" means "Mister" and he is talking about Hillary you aren't
doing all that well yourself.

I was a student of German for 2 years. I think Dope meant to imply that
Hillary is a "he". And besides, "Herr" sounds more sinister than "Frau".
In Dope's mind anyway.

RO



Jim Alder

9/29/2007 12:23:00 PM

0

"Ron O'Neal" <roneal@wt.net> wrote in news:46fdfbe9$0$3153$ae4e5890
@news.nationwide.net:

>
> "Jim Alder" <jimalder@ssnet.com> wrote in message
> news:Xns99B97619B75A8jimaldersssnetcom@216.196.97.142...
>> "Ron O'Neal" <roneal@wt.net> wrote in news:46fd10bc$0$3159$ae4e5890
>> @news.nationwide.net:
>>
>> > "Harry Dope" <DumbassliberalShill@aol.com> wrote...
>> >
>> >> Clinton campaign kills negative story
>> >>
>> >>
>> >>
>> >> Early this summer, Sen. Hillary Rodham Clinton's campaign for
>> >> president learned that the men's magazine GQ was working on a story the
>> >> campaign was sure to hate: an account of infighting in Hillaryland.
>> >>
>> >> So Clinton's aides pulled a page from the book of Hollywood
>> >> publicists and offered GQ a stark choice: Kill the piece, or lose
>> >> access to planned celebrity coverboy Bill Clinton.
>> >
>> > <snipped>
>> >
>> >
>> >
>> > I don't like to be picayunish, but in your case I'll make exceptions. In
>> > your Subject Line: It's "Herr", not "Heir".
>>
>> I thought perhaps he was implying that she hoped to inherit the
>> presidency.
>>
>> > Trivial things like this make you look even more stupid than the content
>> > of your posts.
>>
>> Since "Herr" means "Mister" and he is talking about Hillary you aren't
>> doing all that well yourself. Maybe he'll clarify things for us both.
>>
>> --
>> President Bush was so buoyed by the warm reception he was given in Albania
>> that he immediately gave all 3 million Albanians American citizenship,
>> provided they learn Spanish. - Ann Coulter
>
> Re: I thought perhaps he was implying that she hoped to inherit the
> presidency.
>
> Naahhh....Dope isn't that subtle.
>
> Re: Since "Herr" means "Mister" and he is talking about Hillary you aren't
> doing all that well yourself.
>
> I was a student of German for 2 years. I think Dope meant to imply that
> Hillary is a "he". And besides, "Herr" sounds more sinister than "Frau".
> In Dope's mind anyway.

I lack your ability to read minds, so like I said, I'll wait and see if he
wants to clear things up.

--
President Bush was so buoyed by the warm reception he was given in Albania
that he immediately gave all 3 million Albanians American citizenship,
provided they learn Spanish. - Ann Coulter

Edgardo Hames

5/6/2008 8:18:00 PM

0

On Apr 29, 12:05 pm, Edgardo Hames <eha...@gmail.com> wrote:
>
> I need to log all the input (typed by the user) and the stdout/stderr
> of a process. The #script method below should behave pretty much like
> the Unix script application. This way, my application can issue
> several commands, show their output to the user an let them whatever
> is needed.
>
> def script cmd, log
>         # prepare $stdin to save its content into log
>         # prepare $stdout to save its content into log
>         # prepare $stderr to save its content into log
>         status = Open4::spawn cmd, 'stdin' => $stdin, 'stdout' =>
> $stdout, 'stderr' => $stderr
> end

I finally managed to do it. Open4::spawn allows any object to be used
as stdin/stdout/stderr as long as they support 'each', 'read', or
'to_s' or '<<'. So I just created a wrapper class that encapsulates
two descriptors logging al I/O operations.

Best regards,
Edgardo


# BEGIN SCRIPT
require 'rubygems'
require 'open4'

class LoggingIO
def initialize io, log
@io = io
@log = log
end

def gets(sep_string=$/)
res = @io.gets(sep_string)
log res
res
end

def << obj
res = @io << obj
log obj
res
end

def log obj
str = obj ? obj.to_s : "(nil)\n"
@log << str
end
private :log
end

def execute cmd, logfile = nil
log = nil
stdin = $stdin
stdout = $stdout
stderr = $stderr

if (logfile)
log = File.open(logfile, 'a+')
stdin = LoggingIO.new($stdin, log)
stdout = LoggingIO.new($stdout, log)
stderr = LoggingIO.new($stderr, log)
end

status = Open4::spawn cmd, 'stdin' => stdin, 'stdout' =>
stdout, 'stderr' => stderr
log.close if log
status
end
# END SCRIPT