Event-based stdout content capturing

Discussion in 'Ruby' started by Daniel Vartanov, Feb 3, 2009.

  1. 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?
    Daniel Vartanov, Feb 3, 2009
    #1
    1. Advertising

  2. 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
    Sean O'Halpin, Feb 4, 2009
    #2
    1. Advertising

  3. Thank you very much, great idea! I missed in my mind, that puts just
    uses global variable $stdout.
    Daniel Vartanov, Feb 4, 2009
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Moosebumps

    Capturing stdout incrementally

    Moosebumps, Apr 3, 2004, in forum: Python
    Replies:
    5
    Views:
    502
    David Bolen
    Apr 7, 2004
  2. Replies:
    2
    Views:
    338
  3. Luigi
    Replies:
    5
    Views:
    388
    Donn Cave
    Apr 3, 2006
  4. Fuzzyman
    Replies:
    3
    Views:
    618
    Gabriel Genellina
    Jan 7, 2007
  5. JemPower

    Capturing checked event for Template based checkbox.

    JemPower, Oct 30, 2003, in forum: ASP .Net Datagrid Control
    Replies:
    0
    Views:
    165
    JemPower
    Oct 30, 2003
Loading...

Share This Page