1 /* -*- Mode: C; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2; -*- */
3 * Copyright (C) 2007 Collabora Ltd.
4 * Copyright (C) 2007 Nokia Corporation
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.
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.
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
23 #include "gcr-debug.h"
32 #include <glib/gstdio.h>
36 static GcrDebugFlags current_flags = 0;
38 static GDebugKey keys[] = {
39 { "library", GCR_DEBUG_LIBRARY },
40 { "certificate-chain", GCR_DEBUG_CERTIFICATE_CHAIN },
41 { "parse", GCR_DEBUG_PARSE },
42 { "gnupg", GCR_DEBUG_GNUPG },
43 { "trust", GCR_DEBUG_TRUST },
44 { "import", GCR_DEBUG_IMPORT },
45 { "key", GCR_DEBUG_KEY },
46 { "prompt", GCR_DEBUG_PROMPT },
47 { "secret-exchange", GCR_DEBUG_SECRET_EXCHANGE },
52 debug_set_flags (GcrDebugFlags new_flags)
54 current_flags |= new_flags;
58 _gcr_debug_set_flags (const gchar *flags_string)
62 for (nkeys = 0; keys[nkeys].value; nkeys++);
65 debug_set_flags (g_parse_debug_string (flags_string, keys, nkeys));
69 _gcr_debug_flag_is_set (GcrDebugFlags flag)
71 return (flag & current_flags) != 0;
75 on_gcr_log_debug (const gchar *log_domain,
76 GLogLevelFlags log_level,
81 const gchar *progname;
83 gstring = g_string_new (NULL);
85 progname = g_get_prgname ();
86 g_string_append_printf (gstring, "(%s:%lu): %s-DEBUG: %s\n",
87 progname ? progname : "process",
88 (gulong)getpid (), log_domain,
89 message ? message : "(NULL) message");
92 * Give up on debug messages if stdout got lost.
94 if (write (1, gstring->str, gstring->len) != gstring->len)
97 g_string_free (gstring, TRUE);
101 _gcr_debug_message (GcrDebugFlags flag, const gchar *format, ...)
103 static gsize initialized_flags = 0;
104 const gchar *messages_env;
105 const gchar *debug_env;
108 if (g_once_init_enter (&initialized_flags)) {
109 messages_env = g_getenv ("G_MESSAGES_DEBUG");
110 debug_env = g_getenv ("GCR_DEBUG");
112 if (debug_env == NULL)
113 debug_env = G_STRINGIFY (GCR_DEBUG);
117 * If the caller is selectively asking for certain debug
118 * messages with the GCR_DEBUG environment variable, then
119 * we install our own output handler and only print those
120 * messages. This happens irrespective of G_MESSAGES_DEBUG
122 if (messages_env == NULL && debug_env != NULL)
123 g_log_set_handler (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG,
124 on_gcr_log_debug, NULL);
127 * If the caller is using G_MESSAGES_DEBUG then we enable
128 * all our debug messages, and let Glib filter which ones
131 if (messages_env != NULL && debug_env == NULL)
134 _gcr_debug_set_flags (debug_env);
136 g_once_init_leave (&initialized_flags, 1);
139 if (flag & current_flags) {
140 va_start (args, format);
141 g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, format, args);
146 #else /* !WITH_DEBUG */
149 _gcr_debug_flag_is_set (GcrDebugFlags flag)
155 _gcr_debug_message (GcrDebugFlags flag, const gchar *format, ...)
160 _gcr_debug_set_flags (const gchar *flags_string)
164 #endif /* !WITH_DEBUG */