[libvirt] [Python] libvirt.libvirtError output

Cole Robinson crobinso at redhat.com
Tue Dec 9 20:32:17 UTC 2008


Ivan Vovk wrote:
> Hello,
> 
> i have the following piece of pyhton test code:
> 
> try:
>      dom = conn.createXML(xmldata, 0)
> except libvirt.libvirtError, le:
>       sys.stderr.write(le.message)
> as a result in case of exception i get 2 outputs in stderr:
> 
> libvir: OpenVZ error : operation failed: Already an OPENVZ VM defined with the id '3005'
> operation failed: Already an OPENVZ VM defined with the id '3005'
> 
> Actually, i just need only second string, which is generated by sys.stderr.write(le.message).
> 
> Can an exception be raised without "uncontrolled" direction to stderr (fisrt string)?
> 

Yes, you have to set a libvirt error handler, which allows the app to do
whatever they want with the raised libvirt message that by default is
printed to stderr. Here is what we use in virt-manager and virt-install:

# Register libvirt handler
def libvirt_callback(ignore, err):
   if err[3] != libvirt.VIR_ERR_ERROR:
       # Don't log libvirt errors: global error handler will do that
       logging.warn("Non-error from libvirt: '%s'" % err[2])
libvirt.registerErrorHandler(f=libvirt_callback, ctx=None)

- Cole




More information about the libvir-list mailing list