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