Blame libvirt-glib/libvirt-glib-main.c

Packit a07778
/*
Packit a07778
 * libvirt-glib-main.c: libvirt glib integration
Packit a07778
 *
Packit a07778
 * Copyright (C) 2008 Daniel P. Berrange
Packit a07778
 * Copyright (C) 2010-2011 Red Hat, Inc.
Packit a07778
 *
Packit a07778
 * This library is free software; you can redistribute it and/or
Packit a07778
 * modify it under the terms of the GNU Lesser General Public
Packit a07778
 * License as published by the Free Software Foundation; either
Packit a07778
 * version 2.1 of the License, or (at your option) any later version.
Packit a07778
 *
Packit a07778
 * This library is distributed in the hope that it will be useful,
Packit a07778
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit a07778
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit a07778
 * Lesser General Public License for more details.
Packit a07778
 *
Packit a07778
 * You should have received a copy of the GNU Lesser General Public
Packit a07778
 * License along with this library. If not, see
Packit a07778
 * <http://www.gnu.org/licenses/>.
Packit a07778
 *
Packit a07778
 * Author: Daniel P. Berrange <berrange@redhat.com>
Packit a07778
 */
Packit a07778
Packit a07778
#include <config.h>
Packit a07778
Packit a07778
#include <stdlib.h>
Packit a07778
#include <stdio.h>
Packit a07778
#include <libvirt/virterror.h>
Packit a07778
#include <glib/gi18n-lib.h>
Packit a07778
Packit a07778
#include "libvirt-glib-main.h"
Packit a07778
Packit a07778
/**
Packit a07778
 * SECTION:libvirt-glib-main
Packit a07778
 * @short_description: Initialize the library
Packit a07778
 * @title: Library initialization
Packit a07778
 * @stability: Stable
Packit a07778
 * @include: libvirt-glib/libvirt-glib.h
Packit a07778
 *
Packit a07778
 * The Libvirt GLib library provides glue to integrate core libvirt
Packit a07778
 * infrastructure with the GLib library. This enables consistent
Packit a07778
 * error reporting procedures and a common event loop implementation
Packit a07778
 * for applications.
Packit a07778
 *
Packit a07778
 * Before using any functions in the Libvirt GLib library, it must be initialized
Packit a07778
 * by calling gvir_init or gvir_init_check.
Packit a07778
 *
Packit a07778
 * <example>
Packit a07778
 * <title>Initializing the Libvirt GLib library</title>
Packit a07778
 * <programlisting>
Packit a07778
 * int main(int argc, char **argv) {
Packit a07778
 *   ...setup...
Packit a07778
 *   gvir_init(&argc, &argv);
Packit a07778
 *   ...more setup...
Packit a07778
 *   gtk_main();
Packit a07778
 *   return 0;
Packit a07778
 * }
Packit a07778
 * ]]></programlisting>
Packit a07778
 * </example>
Packit a07778
 *
Packit a07778
 */
Packit a07778
Packit a07778
static void
Packit a07778
gvir_error_func(gpointer opaque G_GNUC_UNUSED,
Packit a07778
                virErrorPtr err)
Packit a07778
{
Packit a07778
    g_debug("Error: %s", err->message);
Packit a07778
}
Packit a07778
Packit a07778
Packit a07778
Packit a07778
/**
Packit a07778
 * gvir_init:
Packit a07778
 * @argc: (inout): Address of the argc parameter of your main() function (or 0
Packit a07778
 *     if argv is NULL). This will be changed if any arguments were handled.
Packit a07778
 * @argv: (array length=argc) (inout) (allow-none) (transfer none): Address of the
Packit a07778
 *     <parameter>argv</parameter> parameter of main(), or %NULL. Any options
Packit a07778
 *     understood by GTK+ are stripped before return.
Packit a07778
 *
Packit a07778
 * Call this function before using any other Libvirt GLib functions in your applications.
Packit a07778
 * It will initialize everything needed to operate the toolkit and parses some standard
Packit a07778
 * command line options.
Packit a07778
 *
Packit a07778
 * Although you are expected to pass the @argc, @argv parameters from main() to this
Packit a07778
 * function, it is possible to pass NULL if @argv is not available or commandline
Packit a07778
 * handling is not required.
Packit a07778
 *
Packit a07778
 * @argc and @argv are adjusted accordingly so your own code will never see those
Packit a07778
 * standard arguments.
Packit a07778
 *
Packit a07778
 * This method will also turn on debug logging of the library if the
Packit a07778
 * <literal>LIBVIRT_GLIB_DEBUG</literal> environment variable is set.
Packit a07778
 *
Packit a07778
 * This function will terminate your program if it was unable to initialize
Packit a07778
 * for some reason. If you want the program to fall back to an alternate
Packit a07778
 * mode of operation call gvir_init_check instead.
Packit a07778
 */
Packit a07778
void gvir_init(int *argc,
Packit a07778
                char ***argv)
Packit a07778
{
Packit a07778
    GError *err = NULL;
Packit a07778
    if (!gvir_init_check(argc, argv, &err)) {
Packit a07778
        g_error("Could not initialize libvirt-glib: %s\n",
Packit a07778
                err->message);
Packit a07778
    }
Packit a07778
}
Packit a07778
Packit a07778
Packit a07778
static void gvir_log_handler(const gchar *log_domain G_GNUC_UNUSED,
Packit a07778
                             GLogLevelFlags log_level G_GNUC_UNUSED,
Packit a07778
                             const gchar *message,
Packit a07778
                             gpointer user_data)
Packit a07778
{
Packit a07778
    if (user_data)
Packit a07778
        fprintf(stderr, "%s\n", message);
Packit a07778
}
Packit a07778
Packit a07778
Packit a07778
/**
Packit a07778
 * gvir_init_check:
Packit a07778
 * @argc: (inout): Address of the argc parameter of your main() function (or 0
Packit a07778
 *     if argv is NULL). This will be changed if any arguments were handled.
Packit a07778
 * @argv: (array length=argc) (inout) (allow-none) (transfer none): Address of the
Packit a07778
 *     <parameter>argv</parameter> parameter of main(), or %NULL. Any options
Packit a07778
 *     understood by GTK+ are stripped before return.
Packit a07778
 * @err: filled with the error information if initialized failed.
Packit a07778
 *
Packit a07778
 * This function does the same work as gvir_init() with only a single
Packit a07778
 * change: It does not terminate the program if the Libvirt GLib library
Packit a07778
 * can't be initialized. Instead it returns %FALSE on failure.
Packit a07778
 *
Packit a07778
 * This way the application can fall back to some other mode of
Packit a07778
 * operation.
Packit a07778
 *
Packit a07778
 * Return value: %TRUE if the library was successfully initialized,
Packit a07778
 *     %FALSE otherwise
Packit a07778
 */
Packit a07778
gboolean gvir_init_check(int *argc G_GNUC_UNUSED,
Packit a07778
                         char ***argv G_GNUC_UNUSED,
Packit a07778
                         GError **err G_GNUC_UNUSED)
Packit a07778
{
Packit a07778
    virSetErrorFunc(NULL, gvir_error_func);
Packit a07778
Packit a07778
    /* Threading is always enabled from 2.31.0 onwards */
Packit a07778
#if !GLIB_CHECK_VERSION(2, 31, 0)
Packit a07778
    if (!g_thread_supported())
Packit a07778
        g_thread_init(NULL);
Packit a07778
#endif
Packit a07778
Packit a07778
    virInitialize();
Packit a07778
Packit a07778
    if (!bindtextdomain(PACKAGE, LOCALEDIR))
Packit a07778
        return FALSE;
Packit a07778
Packit a07778
    /* GLib >= 2.31.0 debug is off by default, so we need to
Packit a07778
     * enable it. Older versions are on by default, so we need
Packit a07778
     * to disable it.
Packit a07778
     */
Packit a07778
#if GLIB_CHECK_VERSION(2, 31, 0)
Packit a07778
    if (getenv("LIBVIRT_GLIB_DEBUG"))
Packit a07778
        g_log_set_handler(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG,
Packit a07778
                          gvir_log_handler, (void*)0x1);
Packit a07778
#else
Packit a07778
    if (!getenv("LIBVIRT_GLIB_DEBUG"))
Packit a07778
        g_log_set_handler(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG,
Packit a07778
                          gvir_log_handler, NULL);
Packit a07778
#endif
Packit a07778
Packit a07778
    return TRUE;
Packit a07778
}