Bug 664558 - RFE: Allow to set log callback in Ruby bindings
RFE: Allow to set log callback in Ruby bindings
Product: Virtualization Tools
Classification: Community
Component: libguestfs (Show other bugs)
Unspecified Unspecified
low Severity medium
: ---
: ---
Assigned To: Richard W.M. Jones
Depends On:
  Show dependency treegraph
Reported: 2010-12-20 14:05 EST by Marek Goldmann
Modified: 2011-03-28 05:06 EDT (History)
3 users (show)

See Also:
Fixed In Version:
Doc Type: Bug Fix
Doc Text:
Story Points: ---
Clone Of:
Last Closed: 2011-03-15 18:47:01 EDT
Type: ---
Regression: ---
Mount Type: ---
Documentation: ---
Verified Versions:
Category: ---
oVirt Team: ---
RHEL 7.3 requirements from Atomic Host:
Cloudforms Team: ---

Attachments (Terms of Use)

  None (edit)
Description Marek Goldmann 2010-12-20 14:05:44 EST

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.
Comment 1 Richard W.M. Jones 2010-12-20 14:10:27 EST
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.
Comment 2 Richard W.M. Jones 2010-12-22 08:57:20 EST
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
mailing list.
Comment 3 Richard W.M. Jones 2010-12-22 12:25:33 EST
(In reply to comment #2)
> I will think about some alternate way and post about it on the
> mailing list.

Comment 4 Richard W.M. Jones 2011-03-10 10:25:12 EST
Interim patch posted for review here:

Comment 5 Richard W.M. Jones 2011-03-15 12:12:45 EDT
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.
Comment 6 Chris Lalancette 2011-03-15 14:39:13 EDT
Hey Rich,
     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

#include <stdio.h>
#include <stdlib.h>

#include <ruby.h>

static VALUE cb(VALUE args)
  fprintf(stderr, "Hello from cb\n");
  if (TYPE(args) != T_FIXNUM)
    rb_raise(rb_eTypeError, "expected a number");
  return Qnil;

static VALUE rescue(VALUE args, VALUE exception_object)
  fprintf(stderr, "Rescue args %s, object classname %s\n",

  return Qnil;

int main()
  int r;


  r = rb_rescue(cb, INT2NUM(0), rescue, rb_str_new2("data"));
  r = rb_rescue(cb, rb_str_new2("bad"), rescue, rb_str_new2("data"));

  return 0;
Comment 7 Richard W.M. Jones 2011-03-15 18:46:39 EDT
Upstream (with broken exception handling):
Comment 8 Richard W.M. Jones 2011-03-28 05:06:37 EDT
Thanks Chris; Ruby exceptions are fixed now too:


Note You need to log in before you can comment on or make changes to this bug.