capturing stdout from so library

Discussion in 'Ruby' started by Bjorn Nordbo, Sep 5, 2004.

  1. Bjorn Nordbo

    Bjorn Nordbo Guest

    I have a package (rrdtool) that provides functionality through a shared
    library and a Ruby interface (ruby-rrd) to it.

    The problem is that one of the functions in the library (rrd_graph) only
    returns data through a file descriptor; a file or standard output.

    This is rather dumb in my opinion, but probably hard to fix. Also, a
    a workaround seems easy:

    class StringBuffer
    attr_reader :buffer

    def initialize
    @buffer = String.new
    end

    def write(string)
    @buffer << string
    end
    end

    begin
    oldout = $stdout
    $stdout = StringBuffer.new
    RRD.graph(... tons of options ...)
    image = $stdout.buffer
    $stdout = oldout
    end

    According to my theory, the StringBuffer object should have captured all
    data sent to standard output while $stdout points to it, and image sould
    contain a beautiful graph produced by RRD.graph.

    However, what happens is that RRD.graph blatantly ignores the fact that
    $stdout is redirected, and bangs its data to standard output anyway.

    I assume this means that there's nothing I can do in Ruby to capture the
    output to standard out from a library written in C. If this is true,
    I have two options:

    - Do something (but what?) in the Ruby/C interface
    - Fix the library

    Please tell me I'm wrong. :)


    --
    Bjørn
    Bjorn Nordbo, Sep 5, 2004
    #1
    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:
    516
    David Bolen
    Apr 7, 2004
  2. Replies:
    2
    Views:
    346
  3. Luigi
    Replies:
    5
    Views:
    404
    Donn Cave
    Apr 3, 2006
  4. Fuzzyman
    Replies:
    3
    Views:
    632
    Gabriel Genellina
    Jan 7, 2007
  5. Falcolas
    Replies:
    1
    Views:
    243
    Thomas Jollans
    Jun 27, 2007
Loading...

Share This Page