Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Event-based stdout content capturing

Reply
Thread Tools

Event-based stdout content capturing

 
 
Daniel Vartanov
Guest
Posts: n/a
 
      02-03-2009
Some legacy code produces output to STDOUT during execution. I need to
capture it on even-based model, something like the following:

capture the_legacy_code_lambda do |content|
# do something with content each time something appears in STDOUT
end

how can I manage this? Are there any idioms?
 
Reply With Quote
 
 
 
 
Sean O'Halpin
Guest
Posts: n/a
 
      02-04-2009
On Tue, Feb 3, 2009 at 11:19 AM, Daniel Vartanov
<> wrote:
> Some legacy code produces output to STDOUT during execution. I need to
> capture it on even-based model, something like the following:
>
> capture the_legacy_code_lambda do |content|
> # do something with content each time something appears in STDOUT
> end
>
> how can I manage this? Are there any idioms?
>
>


You can reassign to the global $stdout object any object which
provides the method #write. You could use that to capture the output
something like this:

class Capture
def initialize(*args, &block)
@block = block
end
def write(txt)
@block.call(txt)
end
end

def capture(code, &block)
old_stdout = $stdout
begin
$stdout = Capture.new(&block)
code.call
ensure
$stdout = old_stdout
end
end

def legacy_code
puts "hello world"
puts "me again"
raise Exception, "Oh no!"
end

puts "start"
begin
capture(proc { legacy_code }) do |txt|
STDOUT.print "captured ["
STDOUT.print txt
STDOUT.puts "]"
STDOUT.flush
end
rescue Object => e
puts "Got exception"
raise
end
puts "end"

# $ ruby capture-stdout.rb
# start
# captured [hello world]
# captured [
# ]
# captured [me again]
# captured [
# ]
# Got exception
# capture-stdout.rb:23:in `legacy_code': Oh no! (Exception)
# etc.

Regards,
Sean

 
Reply With Quote
 
 
 
 
Daniel Vartanov
Guest
Posts: n/a
 
      02-04-2009
Thank you very much, great idea! I missed in my mind, that puts just
uses global variable $stdout.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Capturing stdout from a class method Falcolas Python 1 06-27-2007 07:48 PM
Capturing stderr and stdout of a subprocess as a single stream Fuzzyman Python 3 01-07-2007 08:44 PM
Capturing stdout without waiting for the process end Luigi Python 5 04-03-2006 07:24 PM
capturing stdout from lynx.. sergio@village-buzz.com Python 2 03-13-2006 03:07 PM
Capturing stdout incrementally Moosebumps Python 5 04-07-2004 03:38 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57