virError
structure for reporting
* errors back to the application programmer. The libvirt API errors are
* provided in thread-local variables, while the GLib standard practice is
* to return errors via out parameters. This library provides a simple way
* to fill in GError **
output parameters with the contents
* of the most recent libvirt error object in the current thread.
*
* The gvir_error_new
, gvir_error_new_literal
and
* gvir_error_new_valist
methods all return a newly created
* GError *
object instance, differing only in the way the
* message needs to be provided. For most usage though, it is preferrable
* to use the gvir_set_error
, gvir_set_error_literal
* and gvir_set_error_valist
methods. These all accept a
* GError **
argument and take care to only fill it if it
* points to a non-NULL location.
*
* gvir_event_register()
* should be invoked. Once this is done, it is mandatory to have the default
* GMain event loop run by a thread in the application, usually the primary
* thread, eg by using gtk_main()
or g_application_run()
*
* GMain
context. If invoked more
* than once this method will be a no-op. Applications should,
* however, take care not to register any another non-GLib
* event loop with libvirt.
*
* After invoking this method, it is mandatory to run the
* default GMain event loop. Typically this can be satisfied
* by invoking gtk_main
or g_application_run
* in the application's main thread. Failure to run the event
* loop will mean no libvirt events get dispatched, and the
* libvirt keepalive timer will kill off libvirt connections
* frequently.
*/
void gvir_event_register(void)
{
static GOnce once = G_ONCE_INIT;
g_once(&once, event_register_once, NULL);
}
libvirt-glib-0.2.2/libvirt-glib/libvirt-glib-error.h 0000664 0000000 0000000 00000004336 12553465674 017275 0000000 0000000 /*
* libvirt-glib-error.h: libvirt glib integration
*
* Copyright (C) 2008 Daniel P. Berrange
* Copyright (C) 2010-2011 Red Hat, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library. If not, see
* gvir_init
or gvir_init_check
.
*
* gvir_init_check
instead.
*/
void gvir_init(int *argc,
char ***argv)
{
GError *err = NULL;
if (!gvir_init_check(argc, argv, &err)) {
g_error("Could not initialize libvirt-glib: %s\n",
err->message);
}
}
static void gvir_log_handler(const gchar *log_domain G_GNUC_UNUSED,
GLogLevelFlags log_level G_GNUC_UNUSED,
const gchar *message,
gpointer user_data)
{
if (user_data)
fprintf(stderr, "%s\n", message);
}
/**
* gvir_init_check:
* @argc: (inout): Address of the argc parameter of your main() function (or 0
* if argv is NULL). This will be changed if any arguments were handled.
* @argv: (array length=argc) (inout) (allow-none) (transfer none): Address of the
* Top | ![]() |
![]() |
![]() |
![]() |
The libvirt API has the ability to provide applications with asynchronous notifications of interesting events. To enable this functionality though, applications must provide libvirt with an event loop implementation. The libvirt-glib API provides such an implementation, which naturally integrates with the GMain event loop framework.
To enable use of the GMain event loop glue, the
should be invoked. Once this is done, it is mandatory to have the default
GMain event loop run by a thread in the application, usually the primary
thread, eg by using gvir_event_register()
or gtk_main()
g_application_run()
Example 3. Registering for events with a GTK application
1 2 3 4 5 6 7 |
int main(int argc, char **argv) { ...setup... gvir_event_register(); ...more setup... gtk_main(); return 0; } |
Example 4. Registering for events using Appplication
1 2 3 4 5 6 7 8 |
int main(int argc, char **argv) { ...setup... GApplication *app = ...create some impl of GApplication... gvir_event_register(); ...more setup... g_application_run(app); return 0; } |
void
gvir_event_register (void
);
Registers a libvirt event loop implementation that is backed
by the default GMain
context. If invoked more
than once this method will be a no-op. Applications should,
however, take care not to register any another non-GLib
event loop with libvirt.
After invoking this method, it is mandatory to run the
default GMain event loop. Typically this can be satisfied
by invoking gtk_main
or g_application_run
in the application's main thread. Failure to run the event
loop will mean no libvirt events get dispatched, and the
libvirt keepalive timer will kill off libvirt connections
frequently.