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

Packit Service ea0369
/*
Packit Service ea0369
 * libvirt-glib-main.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 <stdlib.h>
Packit Service ea0369
#include <stdio.h>
Packit Service ea0369
#include <libvirt/virterror.h>
Packit Service ea0369
#include <glib/gi18n-lib.h>
Packit Service ea0369
Packit Service ea0369
#include "libvirt-glib-main.h"
Packit Service ea0369
Packit Service ea0369
/**
Packit Service ea0369
 * SECTION:libvirt-glib-main
Packit Service ea0369
 * @short_description: Initialize the library
Packit Service ea0369
 * @title: Library initialization
Packit Service ea0369
 * @stability: Stable
Packit Service ea0369
 * @include: libvirt-glib/libvirt-glib.h
Packit Service ea0369
 *
Packit Service ea0369
 * The Libvirt GLib library provides glue to integrate core libvirt
Packit Service ea0369
 * infrastructure with the GLib library. This enables consistent
Packit Service ea0369
 * error reporting procedures and a common event loop implementation
Packit Service ea0369
 * for applications.
Packit Service ea0369
 *
Packit Service ea0369
 * Before using any functions in the Libvirt GLib library, it must be initialized
Packit Service ea0369
 * by calling gvir_init or gvir_init_check.
Packit Service ea0369
 *
Packit Service ea0369
 * <example>
Packit Service ea0369
 * <title>Initializing the Libvirt GLib library</title>
Packit Service ea0369
 * <programlisting>
Packit Service ea0369
 * int main(int argc, char **argv) {
Packit Service ea0369
 *   ...setup...
Packit Service ea0369
 *   gvir_init(&argc, &argv);
Packit Service ea0369
 *   ...more setup...
Packit Service ea0369
 *   gtk_main();
Packit Service ea0369
 *   return 0;
Packit Service ea0369
 * }
Packit Service ea0369
 * ]]></programlisting>
Packit Service ea0369
 * </example>
Packit Service ea0369
 *
Packit Service ea0369
 */
Packit Service ea0369
Packit Service ea0369
static void
Packit Service ea0369
gvir_error_func(gpointer opaque G_GNUC_UNUSED,
Packit Service ea0369
                virErrorPtr err)
Packit Service ea0369
{
Packit Service ea0369
    g_debug("Error: %s", err->message);
Packit Service ea0369
}
Packit Service ea0369
Packit Service ea0369
Packit Service ea0369
Packit Service ea0369
/**
Packit Service ea0369
 * gvir_init:
Packit Service ea0369
 * @argc: (inout): Address of the argc parameter of your main() function (or 0
Packit Service ea0369
 *     if argv is NULL). This will be changed if any arguments were handled.
Packit Service ea0369
 * @argv: (array length=argc) (inout) (allow-none) (transfer none): Address of the
Packit Service ea0369
 *     <parameter>argv</parameter> parameter of main(), or %NULL. Any options
Packit Service ea0369
 *     understood by GTK+ are stripped before return.
Packit Service ea0369
 *
Packit Service ea0369
 * Call this function before using any other Libvirt GLib functions in your applications.
Packit Service ea0369
 * It will initialize everything needed to operate the toolkit and parses some standard
Packit Service ea0369
 * command line options.
Packit Service ea0369
 *
Packit Service ea0369
 * Although you are expected to pass the @argc, @argv parameters from main() to this
Packit Service ea0369
 * function, it is possible to pass NULL if @argv is not available or commandline
Packit Service ea0369
 * handling is not required.
Packit Service ea0369
 *
Packit Service ea0369
 * @argc and @argv are adjusted accordingly so your own code will never see those
Packit Service ea0369
 * standard arguments.
Packit Service ea0369
 *
Packit Service ea0369
 * This method will also turn on debug logging of the library if the
Packit Service ea0369
 * <literal>LIBVIRT_GLIB_DEBUG</literal> environment variable is set.
Packit Service ea0369
 *
Packit Service ea0369
 * This function will terminate your program if it was unable to initialize
Packit Service ea0369
 * for some reason. If you want the program to fall back to an alternate
Packit Service ea0369
 * mode of operation call gvir_init_check instead.
Packit Service ea0369
 */
Packit Service ea0369
void gvir_init(int *argc,
Packit Service ea0369
                char ***argv)
Packit Service ea0369
{
Packit Service ea0369
    GError *err = NULL;
Packit Service ea0369
    if (!gvir_init_check(argc, argv, &err)) {
Packit Service ea0369
        g_error("Could not initialize libvirt-glib: %s\n",
Packit Service ea0369
                err->message);
Packit Service ea0369
    }
Packit Service ea0369
}
Packit Service ea0369
Packit Service ea0369
Packit Service ea0369
static void gvir_log_handler(const gchar *log_domain G_GNUC_UNUSED,
Packit Service ea0369
                             GLogLevelFlags log_level G_GNUC_UNUSED,
Packit Service ea0369
                             const gchar *message,
Packit Service ea0369
                             gpointer user_data)
Packit Service ea0369
{
Packit Service ea0369
    if (user_data)
Packit Service ea0369
        fprintf(stderr, "%s\n", message);
Packit Service ea0369
}
Packit Service ea0369
Packit Service ea0369
Packit Service ea0369
/**
Packit Service ea0369
 * gvir_init_check:
Packit Service ea0369
 * @argc: (inout): Address of the argc parameter of your main() function (or 0
Packit Service ea0369
 *     if argv is NULL). This will be changed if any arguments were handled.
Packit Service ea0369
 * @argv: (array length=argc) (inout) (allow-none) (transfer none): Address of the
Packit Service ea0369
 *     <parameter>argv</parameter> parameter of main(), or %NULL. Any options
Packit Service ea0369
 *     understood by GTK+ are stripped before return.
Packit Service ea0369
 * @err: filled with the error information if initialized failed.
Packit Service ea0369
 *
Packit Service ea0369
 * This function does the same work as gvir_init() with only a single
Packit Service ea0369
 * change: It does not terminate the program if the Libvirt GLib library
Packit Service ea0369
 * can't be initialized. Instead it returns %FALSE on failure.
Packit Service ea0369
 *
Packit Service ea0369
 * This way the application can fall back to some other mode of
Packit Service ea0369
 * operation.
Packit Service ea0369
 *
Packit Service ea0369
 * Return value: %TRUE if the library was successfully initialized,
Packit Service ea0369
 *     %FALSE otherwise
Packit Service ea0369
 */
Packit Service ea0369
gboolean gvir_init_check(int *argc G_GNUC_UNUSED,
Packit Service ea0369
                         char ***argv G_GNUC_UNUSED,
Packit Service ea0369
                         GError **err G_GNUC_UNUSED)
Packit Service ea0369
{
Packit Service ea0369
    virSetErrorFunc(NULL, gvir_error_func);
Packit Service ea0369
Packit Service ea0369
    /* Threading is always enabled from 2.31.0 onwards */
Packit Service ea0369
#if !GLIB_CHECK_VERSION(2, 31, 0)
Packit Service ea0369
    if (!g_thread_supported())
Packit Service ea0369
        g_thread_init(NULL);
Packit Service ea0369
#endif
Packit Service ea0369
Packit Service ea0369
    virInitialize();
Packit Service ea0369
Packit Service ea0369
    if (!bindtextdomain(PACKAGE, LOCALEDIR))
Packit Service ea0369
        return FALSE;
Packit Service ea0369
Packit Service ea0369
    /* GLib >= 2.31.0 debug is off by default, so we need to
Packit Service ea0369
     * enable it. Older versions are on by default, so we need
Packit Service ea0369
     * to disable it.
Packit Service ea0369
     */
Packit Service ea0369
#if GLIB_CHECK_VERSION(2, 31, 0)
Packit Service ea0369
    if (getenv("LIBVIRT_GLIB_DEBUG"))
Packit Service ea0369
        g_log_set_handler(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG,
Packit Service ea0369
                          gvir_log_handler, (void*)0x1);
Packit Service ea0369
#else
Packit Service ea0369
    if (!getenv("LIBVIRT_GLIB_DEBUG"))
Packit Service ea0369
        g_log_set_handler(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG,
Packit Service ea0369
                          gvir_log_handler, NULL);
Packit Service ea0369
#endif
Packit Service ea0369
Packit Service ea0369
    return TRUE;
Packit Service ea0369
}