| 1 |
/* -*- Mode: C; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2; -*- */ |
| 2 |
/* |
| 3 |
* Copyright (C) 2007 Collabora Ltd. |
| 4 |
* Copyright (C) 2007 Nokia Corporation |
| 5 |
* |
| 6 |
* This library is free software; you can redistribute it and/or |
| 7 |
* modify it under the terms of the GNU Lesser General Public |
| 8 |
* License as published by the Free Software Foundation; either |
| 9 |
* version 2.1 of the License, or (at your option) any later version. |
| 10 |
* |
| 11 |
* This library is distributed in the hope that it will be useful, |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 |
* Lesser General Public License for more details. |
| 15 |
* |
| 16 |
* You should have received a copy of the GNU Lesser General Public |
| 17 |
* License along with this library; if not, write to the Free Software |
| 18 |
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 19 |
*/ |
| 20 |
|
| 21 |
#include "config.h" |
| 22 |
|
| 23 |
#include "gkm-debug.h" |
| 24 |
|
| 25 |
#include <stdarg.h> |
| 26 |
#include <unistd.h> |
| 27 |
|
| 28 |
#include <glib.h> |
| 29 |
#include <glib/gstdio.h> |
| 30 |
|
| 31 |
#ifdef WITH_DEBUG |
| 32 |
|
| 33 |
static GkmDebugFlags current_flags = 0; |
| 34 |
|
| 35 |
static GDebugKey keys[] = { |
| 36 |
{ "storage", GKM_DEBUG_STORAGE }, |
| 37 |
{ "object", GKM_DEBUG_OBJECT }, |
| 38 |
{ 0, } |
| 39 |
}; |
| 40 |
|
| 41 |
static void |
| 42 |
debug_set_flags (GkmDebugFlags new_flags) |
| 43 |
{ |
| 44 |
current_flags |= new_flags; |
| 45 |
} |
| 46 |
|
| 47 |
void |
| 48 |
gkm_debug_set_flags (const gchar *flags_string) |
| 49 |
{ |
| 50 |
guint nkeys; |
| 51 |
|
| 52 |
for (nkeys = 0; keys[nkeys].value; nkeys++); |
| 53 |
|
| 54 |
if (flags_string) |
| 55 |
debug_set_flags (g_parse_debug_string (flags_string, keys, nkeys)); |
| 56 |
} |
| 57 |
|
| 58 |
gboolean |
| 59 |
gkm_debug_flag_is_set (GkmDebugFlags flag) |
| 60 |
{ |
| 61 |
return (flag & current_flags) != 0; |
| 62 |
} |
| 63 |
|
| 64 |
static void |
| 65 |
on_gkm_log_debug (const gchar *log_domain, |
| 66 |
GLogLevelFlags log_level, |
| 67 |
const gchar *message, |
| 68 |
gpointer user_data) |
| 69 |
{ |
| 70 |
GString *gstring; |
| 71 |
const gchar *progname; |
| 72 |
|
| 73 |
gstring = g_string_new (NULL); |
| 74 |
|
| 75 |
progname = g_get_prgname (); |
| 76 |
g_string_append_printf (gstring, "(%s:%lu): %s-DEBUG: %s\n", |
| 77 |
progname ? progname : "process", |
| 78 |
(gulong)getpid (), log_domain, |
| 79 |
message ? message : "(NULL) message"); |
| 80 |
|
| 81 |
write (1, gstring->str, gstring->len); |
| 82 |
g_string_free (gstring, TRUE); |
| 83 |
} |
| 84 |
|
| 85 |
void |
| 86 |
gkm_debug_message (GkmDebugFlags flag, |
| 87 |
const gchar *format, |
| 88 |
...) |
| 89 |
{ |
| 90 |
static gsize initialized_flags = 0; |
| 91 |
const gchar *messages_env; |
| 92 |
const gchar *debug_env; |
| 93 |
va_list args; |
| 94 |
|
| 95 |
if (g_once_init_enter (&initialized_flags)) { |
| 96 |
messages_env = g_getenv ("G_MESSAGES_DEBUG"); |
| 97 |
debug_env = g_getenv ("GKM_DEBUG"); |
| 98 |
#ifdef GKM_DEBUG |
| 99 |
if (debug_env == NULL) |
| 100 |
debug_env = G_STRINGIFY (GKM_DEBUG); |
| 101 |
#endif |
| 102 |
|
| 103 |
/* |
| 104 |
* If the caller is selectively asking for certain debug |
| 105 |
* messages with the GKM_DEBUG environment variable, then |
| 106 |
* we install our own output handler and only print those |
| 107 |
* messages. This happens irrespective of G_MESSAGES_DEBUG |
| 108 |
*/ |
| 109 |
if (messages_env == NULL && debug_env != NULL) |
| 110 |
g_log_set_handler (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, |
| 111 |
on_gkm_log_debug, NULL); |
| 112 |
|
| 113 |
/* |
| 114 |
* If the caller is using G_MESSAGES_DEBUG then we enable |
| 115 |
* all our debug messages, and let Glib filter which ones |
| 116 |
* to display. |
| 117 |
*/ |
| 118 |
if (messages_env != NULL && debug_env == NULL) |
| 119 |
debug_env = "all"; |
| 120 |
|
| 121 |
gkm_debug_set_flags (debug_env); |
| 122 |
|
| 123 |
g_once_init_leave (&initialized_flags, 1); |
| 124 |
} |
| 125 |
|
| 126 |
if (flag & current_flags) { |
| 127 |
va_start (args, format); |
| 128 |
g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, format, args); |
| 129 |
va_end (args); |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
#else /* !WITH_DEBUG */ |
| 134 |
|
| 135 |
gboolean |
| 136 |
gkm_debug_flag_is_set (GkmDebugFlags flag) |
| 137 |
{ |
| 138 |
return FALSE; |
| 139 |
} |
| 140 |
|
| 141 |
void |
| 142 |
gkm_debug_message (GkmDebugFlags flag, |
| 143 |
const gchar *format, |
| 144 |
...) |
| 145 |
{ |
| 146 |
} |
| 147 |
|
| 148 |
void |
| 149 |
gkm_debug_set_flags (const gchar *flags_string) |
| 150 |
{ |
| 151 |
} |
| 152 |
|
| 153 |
#endif /* !WITH_DEBUG */ |