1 /*
2 * libvirt-gconfig-main.c: libvirt gconfig integration
3 *
4 * Copyright (C) 2008 Daniel P. Berrange
5 * Copyright (C) 2010-2011 Red Hat, Inc.
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library. If not, see
19 * <http://www.gnu.org/licenses/>.
20 *
21 * Author: Daniel P. Berrange <berrange@redhat.com>
22 */
23
24 #include <config.h>
25
26 #include <stdlib.h>
27 #include <stdio.h>
28
29 #include "libvirt-glib/libvirt-glib.h"
30 #include "libvirt-gconfig/libvirt-gconfig.h"
31 #include "libvirt-gconfig/libvirt-gconfig-compat.h"
32
33 /**
34 * gvir_config_init:
35 * @argc: (inout): pointer to application's argc
36 * @argv: (inout) (array length=argc) (allow-none) (transfer none): pointer to application's argv
37 */
gvir_config_init(int * argc,char *** argv)38 void gvir_config_init(int *argc,
39 char ***argv)
40 {
41 GError *err = NULL;
42 if (!gvir_config_init_check(argc, argv, &err)) {
43 g_error("Could not initialize libvirt-gconfig: %s\n",
44 err->message);
45 }
46 }
47
gvir_log_handler(const gchar * log_domain G_GNUC_UNUSED,GLogLevelFlags log_level G_GNUC_UNUSED,const gchar * message,gpointer user_data)48 static void gvir_log_handler(const gchar *log_domain G_GNUC_UNUSED,
49 GLogLevelFlags log_level G_GNUC_UNUSED,
50 const gchar *message,
51 gpointer user_data)
52 {
53 if (user_data)
54 fprintf(stderr, "%s\n", message);
55 }
56
57
58 /**
59 * gvir_config_init_check:
60 * @argc: (inout): pointer to application's argc
61 * @argv: (inout) (array length=argc) (allow-none) (transfer none): pointer to application's argv
62 * @err: pointer to a #GError to which a message will be posted on error
63 */
gvir_config_init_check(int * argc G_GNUC_UNUSED,char *** argv G_GNUC_UNUSED,GError ** err G_GNUC_UNUSED)64 gboolean gvir_config_init_check(int *argc G_GNUC_UNUSED,
65 char ***argv G_GNUC_UNUSED,
66 GError **err G_GNUC_UNUSED)
67 {
68 g_type_init();
69
70 /* GLib >= 2.31.0 debug is off by default, so we need to
71 * enable it. Older versions are on by default, so we need
72 * to disable it.
73 */
74 #if GLIB_CHECK_VERSION(2, 31, 0)
75 if (getenv("LIBVIRT_GCONFIG_DEBUG"))
76 g_log_set_handler(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG,
77 gvir_log_handler, (void*)0x1);
78 #else
79 if (!getenv("LIBVIRT_GCONFIG_DEBUG"))
80 g_log_set_handler(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG,
81 gvir_log_handler, NULL);
82 #endif
83
84 return TRUE;
85 }
86