Red Hat Bugzilla – Bug 664558
RFE: Allow to set log callback in Ruby bindings
Last modified: 2011-03-28 05:06:37 EDT
Currently there is no way to set a log callback using Ruby bindings. It would be very helpful for Ruby tools that want to integrate with libguestfs to catch the log output.
I think this would be useful.
As discussed on IRC, won't get done until mid Jan (by me)
but if you want to have a go at a patch then be my guest.
More thoughts on this issue:
Currently g->log_message_cb will only see messages sent by
the daemon to the VM console. If g->verbose is set, then
these messages are *also* printed to stderr (as well as being
sent to the g->log_message_cb handler if any).
Some types of message that g->log_message_cb would never see:
* extra debug from the library when g->verbose is set
* trace messages (LIBGUESTFS_TRACE=1: these go to stderr)
* debug from other things that use the guestfs_get_verbose
call, eg. capitests, guestfish
So g->log_message_cb is not very useful. At the same time
there is obviously a need to be able to capture debug and
trace messages separately in GUI programs
(guestfs-browser, BoxGrinder and RHEV-M all need it).
We cannot change g->log_message_cb, because of the ABI contract.
I will think about some alternate way and post about it on the
(In reply to comment #2)
> I will think about some alternate way and post about it on the
> mailing list.
Interim patch posted for review here:
Full patch series including Ruby bindings posted:
One aspect that is broken is that the Ruby interpreter segfaults
if the callback raises any sort of exception. Apparently one
can prevent this using the 'rb_rescue' function, but I have yet
to find a coherent explanation of how exactly to use this.
While the rb_* calls can be a bit dense, in the end they are relatively easy to use. rb_rescue in particular takes exactly 4 arguments: the function that you want to call, the (single) argument to that function, the function to call if the original function throws an exception, and the (single) argument to the rescue function. If you compile and run the below example, the first call to rb_rescue() calls cb, which succeeds without doing much, so only "Hello from cb" is printed. The second call to rb_rescue() calls cb, which then raises an exception, at which point rescue() is called to do cleanup work. You can compile the program with: gcc -g -Wall test.c -I/usr/lib64/ruby/1.8/x86_64-linux -lruby
static VALUE cb(VALUE args)
fprintf(stderr, "Hello from cb\n");
if (TYPE(args) != T_FIXNUM)
rb_raise(rb_eTypeError, "expected a number");
static VALUE rescue(VALUE args, VALUE exception_object)
fprintf(stderr, "Rescue args %s, object classname %s\n",
r = rb_rescue(cb, INT2NUM(0), rescue, rb_str_new2("data"));
r = rb_rescue(cb, rb_str_new2("bad"), rescue, rb_str_new2("data"));
Upstream (with broken exception handling):
Thanks Chris; Ruby exceptions are fixed now too: