On Wed, Jul 17, 2013 at 04:43:34PM +0300, Or Goshen wrote:
Hi,
When I register a callback for events with this function call:
eh = guestfs_set_event_callback(g, message_callback, GUESTFS_EVENT_ALL, 0,
dev);
Shouldnt it capture and redirect messages like this to message_callback():
"libguestfs: error: lstat: /.Trash: No such file or directory"
I still get them in stderr ..
Right. Error messages are handled by a separate path from
log/trace/debug messages.
It's possible to capture error messages and send them somewhere else
(or nowhere). Here's how to do it from C:
(1) Replace guestfs_create with guestfs_create_flags:
guestfs_h *g = guestfs_create_flags (GUESTFS_CREATE_NO_ENVIRONMENT);
if (!g) {
/* guestfs_create_flags with the flag GUESTFS_CREATE_NO_ENVIRONMENT
* never prints anything on stderr. If it fails, it sets the
* global errno. So something like this should be used:
*/
fprintf (logfp, "error: %s\n", strerror (errno));
exit (EXIT_FAILURE);
}
(2) Call guestfs_set_error_handler just after creating the handle:
guestfs_set_error_handler (g, NULL, NULL);
(3) Call guestfs_parse_environment to parse the environment. This
would have been done by guestfs_create, except you set the
NO_ENVIRONMENT flag.
if (guestfs_parse_environment (g) == -1) {
/* see below ... */
(4) For guestfs_parse_environment and all other libguestfs calls, you
should check the return codes, and handle errors, like this:
if (guestfs_parse_environment (g) == -1) {
const char *errstr = guestfs_last_error (g);
int errnum = guestfs_last_errno (g);
fprintf (logfp, "error: %s", errstr);
if (errnum > 0)
fprintf (logfp, " (%s)", strerror (errnum));
fprintf (logfp, "\n");
}
It's usually helpful to put all of that in a separate utility function
or macro. Note that 'errstr' does not need to be freed, but the
string only valid for as long as the handle is open and there are no
more errors on the handle, so if you need to store it you should
probably copy (strdup) it.
(5) Note that guestfs_close does not return an error indication. If
you want to detect errors from shutting down the handle, you have to do:
if (guestfs_shutdown (g) == -1) {
/* error handling as above */
}
guestfs_close (g);
Non C bindings are a little bit different. Most (possibly all?) of
them already set the error handler to NULL and handle errors as above.
Typically they are turned into exceptions. Most (but not all) also
support the new guestfs_create_flags call. All support
guestfs_parse_environment and guestfs_shutdown.
Rich.
--
Richard Jones, Virtualization Group, Red Hat
http://people.redhat.com/~rjones
virt-top is 'top' for virtual machines. Tiny program with many
powerful monitoring features, net stats, disk stats, logging, etc.
http://people.redhat.com/~rjones/virt-top