capturing stdout from so library

B

Bjorn Nordbo

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. :)
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top