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