|
Packit Service |
ea0369 |
/*
|
|
Packit Service |
ea0369 |
* libvirt-glib-error.c: libvirt glib integration
|
|
Packit Service |
ea0369 |
*
|
|
Packit Service |
ea0369 |
* Copyright (C) 2008 Daniel P. Berrange
|
|
Packit Service |
ea0369 |
* Copyright (C) 2010-2011 Red Hat, Inc.
|
|
Packit Service |
ea0369 |
*
|
|
Packit Service |
ea0369 |
* This library is free software; you can redistribute it and/or
|
|
Packit Service |
ea0369 |
* modify it under the terms of the GNU Lesser General Public
|
|
Packit Service |
ea0369 |
* License as published by the Free Software Foundation; either
|
|
Packit Service |
ea0369 |
* version 2.1 of the License, or (at your option) any later version.
|
|
Packit Service |
ea0369 |
*
|
|
Packit Service |
ea0369 |
* This library is distributed in the hope that it will be useful,
|
|
Packit Service |
ea0369 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
Packit Service |
ea0369 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
Packit Service |
ea0369 |
* Lesser General Public License for more details.
|
|
Packit Service |
ea0369 |
*
|
|
Packit Service |
ea0369 |
* You should have received a copy of the GNU Lesser General Public
|
|
Packit Service |
ea0369 |
* License along with this library. If not, see
|
|
Packit Service |
ea0369 |
* <http://www.gnu.org/licenses/>.
|
|
Packit Service |
ea0369 |
*
|
|
Packit Service |
ea0369 |
* Author: Daniel P. Berrange <berrange@redhat.com>
|
|
Packit Service |
ea0369 |
*/
|
|
Packit Service |
ea0369 |
|
|
Packit Service |
ea0369 |
#include <config.h>
|
|
Packit Service |
ea0369 |
|
|
Packit Service |
ea0369 |
#include <libvirt/libvirt.h>
|
|
Packit Service |
ea0369 |
#include <libvirt/virterror.h>
|
|
Packit Service |
ea0369 |
|
|
Packit Service |
ea0369 |
#include "libvirt-glib/libvirt-glib.h"
|
|
Packit Service |
ea0369 |
|
|
Packit Service |
ea0369 |
/**
|
|
Packit Service |
ea0369 |
* SECTION:libvirt-glib-error
|
|
Packit Service |
ea0369 |
* @short_description: Convert libvirt error reports to GLib error reports
|
|
Packit Service |
ea0369 |
* @title: Error reporting
|
|
Packit Service |
ea0369 |
* @stability: Stable
|
|
Packit Service |
ea0369 |
* @include: libvirt-glib/libvirt-glib.h
|
|
Packit Service |
ea0369 |
*
|
|
Packit Service |
ea0369 |
* The libvirt API uses the virError structure for reporting
|
|
Packit Service |
ea0369 |
* errors back to the application programmer. The libvirt API errors are
|
|
Packit Service |
ea0369 |
* provided in thread-local variables, while the GLib standard practice is
|
|
Packit Service |
ea0369 |
* to return errors via out parameters. This library provides a simple way
|
|
Packit Service |
ea0369 |
* to fill in GError ** output parameters with the contents
|
|
Packit Service |
ea0369 |
* of the most recent libvirt error object in the current thread.
|
|
Packit Service |
ea0369 |
*
|
|
Packit Service |
ea0369 |
* The gvir_error_new , gvir_error_new_literal and
|
|
Packit Service |
ea0369 |
* gvir_error_new_valist methods all return a newly created
|
|
Packit Service |
ea0369 |
* GError * object instance, differing only in the way the
|
|
Packit Service |
ea0369 |
* message needs to be provided. For most usage though, it is preferrable
|
|
Packit Service |
ea0369 |
* to use the gvir_set_error , gvir_set_error_literal
|
|
Packit Service |
ea0369 |
* and gvir_set_error_valist methods. These all accept a
|
|
Packit Service |
ea0369 |
* GError ** argument and take care to only fill it if it
|
|
Packit Service |
ea0369 |
* points to a non-NULL location.
|
|
Packit Service |
ea0369 |
*
|
|
Packit Service |
ea0369 |
* <example>
|
|
Packit Service |
ea0369 |
* <title>Reporting GLib errors with libvirt APIs</title>
|
|
Packit Service |
ea0369 |
* <programlisting>
|
|
Packit Service |
ea0369 |
* gboolean myapp_start_guest(const gchar *xml, GError **error)
|
|
Packit Service |
ea0369 |
* {
|
|
Packit Service |
ea0369 |
* if (virDomainCreate(conn, xml, 0) < 0) {
|
|
Packit Service |
ea0369 |
* gvir_set_error_literal(error, "Unable to start virtual machine");
|
|
Packit Service |
ea0369 |
* return FALSE;
|
|
Packit Service |
ea0369 |
* }
|
|
Packit Service |
ea0369 |
*
|
|
Packit Service |
ea0369 |
* return TRUE;
|
|
Packit Service |
ea0369 |
* }
|
|
Packit Service |
ea0369 |
* ]]></programlisting>
|
|
Packit Service |
ea0369 |
* </example>
|
|
Packit Service |
ea0369 |
*
|
|
Packit Service |
ea0369 |
*/
|
|
Packit Service |
ea0369 |
|
|
Packit Service |
ea0369 |
/**
|
|
Packit Service |
ea0369 |
* gvir_error_new:
|
|
Packit Service |
ea0369 |
* @domain: error domain
|
|
Packit Service |
ea0369 |
* @code: error code
|
|
Packit Service |
ea0369 |
* @format: printf()-style format for error message
|
|
Packit Service |
ea0369 |
* @...: parameters for message format
|
|
Packit Service |
ea0369 |
*
|
|
Packit Service |
ea0369 |
* Creates a new #GError with the given @domain and @code,
|
|
Packit Service |
ea0369 |
* and a message formatted with @format.
|
|
Packit Service |
ea0369 |
*
|
|
Packit Service |
ea0369 |
* Returns: a new #GError
|
|
Packit Service |
ea0369 |
*/
|
|
Packit Service |
ea0369 |
GError *gvir_error_new(GQuark domain,
|
|
Packit Service |
ea0369 |
gint code,
|
|
Packit Service |
ea0369 |
const gchar *format,
|
|
Packit Service |
ea0369 |
...)
|
|
Packit Service |
ea0369 |
{
|
|
Packit Service |
ea0369 |
GError *err;
|
|
Packit Service |
ea0369 |
va_list args;
|
|
Packit Service |
ea0369 |
gchar *message;
|
|
Packit Service |
ea0369 |
|
|
Packit Service |
ea0369 |
va_start(args, format);
|
|
Packit Service |
ea0369 |
message = g_strdup_vprintf(format, args);
|
|
Packit Service |
ea0369 |
va_end(args);
|
|
Packit Service |
ea0369 |
|
|
Packit Service |
ea0369 |
err = gvir_error_new_literal(domain, code, message);
|
|
Packit Service |
ea0369 |
|
|
Packit Service |
ea0369 |
g_free(message);
|
|
Packit Service |
ea0369 |
|
|
Packit Service |
ea0369 |
return err;
|
|
Packit Service |
ea0369 |
}
|
|
Packit Service |
ea0369 |
|
|
Packit Service |
ea0369 |
/**
|
|
Packit Service |
ea0369 |
* gvir_error_new_literal:
|
|
Packit Service |
ea0369 |
* @domain: error domain
|
|
Packit Service |
ea0369 |
* @code: error code
|
|
Packit Service |
ea0369 |
* @message: error message
|
|
Packit Service |
ea0369 |
*
|
|
Packit Service |
ea0369 |
* Creates a new #GError; unlike gvir_error_new(), @message is
|
|
Packit Service |
ea0369 |
* not a printf()-style format string. Use this function if
|
|
Packit Service |
ea0369 |
* @message contains text you don't have control over,
|
|
Packit Service |
ea0369 |
* that could include printf() escape sequences.
|
|
Packit Service |
ea0369 |
*
|
|
Packit Service |
ea0369 |
* Returns: a new #GError
|
|
Packit Service |
ea0369 |
*/
|
|
Packit Service |
ea0369 |
GError *gvir_error_new_literal(GQuark domain,
|
|
Packit Service |
ea0369 |
gint code,
|
|
Packit Service |
ea0369 |
const gchar *message)
|
|
Packit Service |
ea0369 |
{
|
|
Packit Service |
ea0369 |
virErrorPtr verr = virGetLastError();
|
|
Packit Service |
ea0369 |
|
|
Packit Service |
ea0369 |
if (!verr)
|
|
Packit Service |
ea0369 |
return g_error_new(domain,
|
|
Packit Service |
ea0369 |
code,
|
|
Packit Service |
ea0369 |
"%s",
|
|
Packit Service |
ea0369 |
message);
|
|
Packit Service |
ea0369 |
|
|
Packit Service |
ea0369 |
if (message)
|
|
Packit Service |
ea0369 |
return g_error_new(domain,
|
|
Packit Service |
ea0369 |
code,
|
|
Packit Service |
ea0369 |
"%s: %s",
|
|
Packit Service |
ea0369 |
message,
|
|
Packit Service |
ea0369 |
verr->message);
|
|
Packit Service |
ea0369 |
else
|
|
Packit Service |
ea0369 |
return g_error_new(domain,
|
|
Packit Service |
ea0369 |
code,
|
|
Packit Service |
ea0369 |
"%s",
|
|
Packit Service |
ea0369 |
verr->message);
|
|
Packit Service |
ea0369 |
}
|
|
Packit Service |
ea0369 |
|
|
Packit Service |
ea0369 |
/**
|
|
Packit Service |
ea0369 |
* gvir_error_new_valist:
|
|
Packit Service |
ea0369 |
* @domain: error domain
|
|
Packit Service |
ea0369 |
* @code: error code
|
|
Packit Service |
ea0369 |
* @format: printf()-style format for error message
|
|
Packit Service |
ea0369 |
* @args: #va_list of parameters for the message format
|
|
Packit Service |
ea0369 |
*
|
|
Packit Service |
ea0369 |
* Creates a new #GError with the given @domain and @code,
|
|
Packit Service |
ea0369 |
* and a message formatted with @format.
|
|
Packit Service |
ea0369 |
*
|
|
Packit Service |
ea0369 |
* Returns: a new #GError
|
|
Packit Service |
ea0369 |
*/
|
|
Packit Service |
ea0369 |
GError *gvir_error_new_valist(GQuark domain,
|
|
Packit Service |
ea0369 |
gint code,
|
|
Packit Service |
ea0369 |
const gchar *format,
|
|
Packit Service |
ea0369 |
va_list args)
|
|
Packit Service |
ea0369 |
{
|
|
Packit Service |
ea0369 |
GError *err;
|
|
Packit Service |
ea0369 |
gchar *message;
|
|
Packit Service |
ea0369 |
|
|
Packit Service |
ea0369 |
message = g_strdup_vprintf(format, args);
|
|
Packit Service |
ea0369 |
|
|
Packit Service |
ea0369 |
err = gvir_error_new_literal(domain, code, message);
|
|
Packit Service |
ea0369 |
|
|
Packit Service |
ea0369 |
g_free(message);
|
|
Packit Service |
ea0369 |
|
|
Packit Service |
ea0369 |
return err;
|
|
Packit Service |
ea0369 |
}
|
|
Packit Service |
ea0369 |
|
|
Packit Service |
ea0369 |
|
|
Packit Service |
ea0369 |
/**
|
|
Packit Service |
ea0369 |
* gvir_set_error:
|
|
Packit Service |
ea0369 |
* @error: pointer to error location
|
|
Packit Service |
ea0369 |
* @domain: error domain
|
|
Packit Service |
ea0369 |
* @code: error code
|
|
Packit Service |
ea0369 |
* @format: printf()-style format for error message
|
|
Packit Service |
ea0369 |
* @...: parameters for message format
|
|
Packit Service |
ea0369 |
*
|
|
Packit Service |
ea0369 |
* If @error is NULL this does nothing. Otherwise it
|
|
Packit Service |
ea0369 |
* creates a new #GError with the given @domain and @code,
|
|
Packit Service |
ea0369 |
* and a message formatted with @format, and stores it
|
|
Packit Service |
ea0369 |
* in @error.
|
|
Packit Service |
ea0369 |
*/
|
|
Packit Service |
ea0369 |
void gvir_set_error(GError **error,
|
|
Packit Service |
ea0369 |
GQuark domain,
|
|
Packit Service |
ea0369 |
gint code,
|
|
Packit Service |
ea0369 |
const gchar *format,
|
|
Packit Service |
ea0369 |
...)
|
|
Packit Service |
ea0369 |
{
|
|
Packit Service |
ea0369 |
va_list args;
|
|
Packit Service |
ea0369 |
gchar *message;
|
|
Packit Service |
ea0369 |
|
|
Packit Service |
ea0369 |
if (!error)
|
|
Packit Service |
ea0369 |
return;
|
|
Packit Service |
ea0369 |
|
|
Packit Service |
ea0369 |
va_start(args, format);
|
|
Packit Service |
ea0369 |
message = g_strdup_vprintf(format, args);
|
|
Packit Service |
ea0369 |
va_end(args);
|
|
Packit Service |
ea0369 |
|
|
Packit Service |
ea0369 |
*error = gvir_error_new_literal(domain, code, message);
|
|
Packit Service |
ea0369 |
|
|
Packit Service |
ea0369 |
g_free(message);
|
|
Packit Service |
ea0369 |
}
|
|
Packit Service |
ea0369 |
|
|
Packit Service |
ea0369 |
|
|
Packit Service |
ea0369 |
/**
|
|
Packit Service |
ea0369 |
* gvir_set_error_literal:
|
|
Packit Service |
ea0369 |
* @error: pointer to error location
|
|
Packit Service |
ea0369 |
* @domain: error domain
|
|
Packit Service |
ea0369 |
* @code: error code
|
|
Packit Service |
ea0369 |
* @message: error message
|
|
Packit Service |
ea0369 |
*
|
|
Packit Service |
ea0369 |
* If @error is NULL this does nothing. Otherwise it
|
|
Packit Service |
ea0369 |
* creates a new #GError and stores it in @error; unlike
|
|
Packit Service |
ea0369 |
* gvir_set_error(), @message is not a printf()-style
|
|
Packit Service |
ea0369 |
* format string. Use this function if @message contains
|
|
Packit Service |
ea0369 |
* text you don't have control over, that could include
|
|
Packit Service |
ea0369 |
* printf() escape sequences.
|
|
Packit Service |
ea0369 |
*/
|
|
Packit Service |
ea0369 |
void gvir_set_error_literal(GError **error,
|
|
Packit Service |
ea0369 |
GQuark domain,
|
|
Packit Service |
ea0369 |
gint code,
|
|
Packit Service |
ea0369 |
const gchar *message)
|
|
Packit Service |
ea0369 |
{
|
|
Packit Service |
ea0369 |
if (!error)
|
|
Packit Service |
ea0369 |
return;
|
|
Packit Service |
ea0369 |
|
|
Packit Service |
ea0369 |
*error = gvir_error_new_literal(domain, code, message);
|
|
Packit Service |
ea0369 |
}
|
|
Packit Service |
ea0369 |
|
|
Packit Service |
ea0369 |
|
|
Packit Service |
ea0369 |
/**
|
|
Packit Service |
ea0369 |
* gvir_set_error_valist:
|
|
Packit Service |
ea0369 |
* @error: pointer to error location
|
|
Packit Service |
ea0369 |
* @domain: error domain
|
|
Packit Service |
ea0369 |
* @code: error code
|
|
Packit Service |
ea0369 |
* @format: printf()-style format for error message
|
|
Packit Service |
ea0369 |
* @args: #va_list of parameters for the message format
|
|
Packit Service |
ea0369 |
*
|
|
Packit Service |
ea0369 |
* If @error is NULL this does nothing. Otherwise it
|
|
Packit Service |
ea0369 |
* creates a new #GError with the given @domain and @code,
|
|
Packit Service |
ea0369 |
* and a message formatted with @format, and stores it
|
|
Packit Service |
ea0369 |
* in @error.
|
|
Packit Service |
ea0369 |
*/
|
|
Packit Service |
ea0369 |
void gvir_set_error_valist(GError **error,
|
|
Packit Service |
ea0369 |
GQuark domain,
|
|
Packit Service |
ea0369 |
gint code,
|
|
Packit Service |
ea0369 |
const gchar *format,
|
|
Packit Service |
ea0369 |
va_list args)
|
|
Packit Service |
ea0369 |
{
|
|
Packit Service |
ea0369 |
gchar *message;
|
|
Packit Service |
ea0369 |
if (!error)
|
|
Packit Service |
ea0369 |
return;
|
|
Packit Service |
ea0369 |
|
|
Packit Service |
ea0369 |
message = g_strdup_vprintf(format, args);
|
|
Packit Service |
ea0369 |
*error = gvir_error_new_literal(domain, code, message);
|
|
Packit Service |
ea0369 |
|
|
Packit Service |
ea0369 |
g_free(message);
|
|
Packit Service |
ea0369 |
}
|
|
Packit Service |
ea0369 |
|
|
Packit Service |
ea0369 |
static void
|
|
Packit Service |
ea0369 |
gvir_log_valist(GLogLevelFlags level, const gchar *format, va_list args) G_GNUC_PRINTF(2, 0);
|
|
Packit Service |
ea0369 |
|
|
Packit Service |
ea0369 |
static void
|
|
Packit Service |
ea0369 |
gvir_log_valist(GLogLevelFlags level, const gchar *format, va_list args)
|
|
Packit Service |
ea0369 |
{
|
|
Packit Service |
ea0369 |
gchar *message;
|
|
Packit Service |
ea0369 |
virErrorPtr verr = virGetLastError();
|
|
Packit Service |
ea0369 |
|
|
Packit Service |
ea0369 |
message = g_strdup_vprintf(format, args);
|
|
Packit Service |
ea0369 |
|
|
Packit Service |
ea0369 |
if (verr)
|
|
Packit Service |
ea0369 |
g_log(G_LOG_DOMAIN, level, "%s: %s", message, verr->message);
|
|
Packit Service |
ea0369 |
else
|
|
Packit Service |
ea0369 |
g_log(G_LOG_DOMAIN, level, "%s", message);
|
|
Packit Service |
ea0369 |
|
|
Packit Service |
ea0369 |
g_free(message);
|
|
Packit Service |
ea0369 |
}
|
|
Packit Service |
ea0369 |
|
|
Packit Service |
ea0369 |
void gvir_warning(const gchar *format, ...)
|
|
Packit Service |
ea0369 |
{
|
|
Packit Service |
ea0369 |
va_list args;
|
|
Packit Service |
ea0369 |
|
|
Packit Service |
ea0369 |
va_start(args, format);
|
|
Packit Service |
ea0369 |
gvir_log_valist(G_LOG_LEVEL_WARNING, format, args);
|
|
Packit Service |
ea0369 |
va_end(args);
|
|
Packit Service |
ea0369 |
}
|
|
Packit Service |
ea0369 |
|
|
Packit Service |
ea0369 |
void gvir_critical(const gchar *format, ...)
|
|
Packit Service |
ea0369 |
{
|
|
Packit Service |
ea0369 |
va_list args;
|
|
Packit Service |
ea0369 |
|
|
Packit Service |
ea0369 |
va_start(args, format);
|
|
Packit Service |
ea0369 |
gvir_log_valist(G_LOG_LEVEL_CRITICAL, format, args);
|
|
Packit Service |
ea0369 |
va_end(args);
|
|
Packit Service |
ea0369 |
}
|