minimal build
[platform/upstream/gcr.git] / gcr / gcr-debug.c
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 "gcr-debug.h"
24
25 #include <errno.h>
26 #include <fcntl.h>
27 #include <stdarg.h>
28 #include <sys/stat.h>
29 #include <unistd.h>
30
31 #include <glib.h>
32 #include <glib/gstdio.h>
33
34 #ifdef WITH_DEBUG
35
36 static GcrDebugFlags current_flags = 0;
37
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 },
48         { 0, }
49 };
50
51 static void
52 debug_set_flags (GcrDebugFlags new_flags)
53 {
54         current_flags |= new_flags;
55 }
56
57 void
58 _gcr_debug_set_flags (const gchar *flags_string)
59 {
60         guint nkeys;
61
62         for (nkeys = 0; keys[nkeys].value; nkeys++);
63
64         if (flags_string)
65                 debug_set_flags (g_parse_debug_string (flags_string, keys, nkeys));
66 }
67
68 gboolean
69 _gcr_debug_flag_is_set (GcrDebugFlags flag)
70 {
71         return (flag & current_flags) != 0;
72 }
73
74 static void
75 on_gcr_log_debug (const gchar *log_domain,
76                   GLogLevelFlags log_level,
77                   const gchar *message,
78                   gpointer user_data)
79 {
80         GString *gstring;
81         const gchar *progname;
82
83         gstring = g_string_new (NULL);
84
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");
90
91         /*
92          * Give up on debug messages if stdout got lost.
93          */
94         if (write (1, gstring->str, gstring->len) != gstring->len)
95                 current_flags = 0;
96
97         g_string_free (gstring, TRUE);
98 }
99
100 void
101 _gcr_debug_message (GcrDebugFlags flag, const gchar *format, ...)
102 {
103         static gsize initialized_flags = 0;
104         const gchar *messages_env;
105         const gchar *debug_env;
106         va_list args;
107
108         if (g_once_init_enter (&initialized_flags)) {
109                 messages_env = g_getenv ("G_MESSAGES_DEBUG");
110                 debug_env = g_getenv ("GCR_DEBUG");
111 #ifdef GCR_DEBUG
112                 if (debug_env == NULL)
113                         debug_env = G_STRINGIFY (GCR_DEBUG);
114 #endif
115
116                 /*
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
121                  */
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);
125
126                 /*
127                  * If the caller is using G_MESSAGES_DEBUG then we enable
128                  * all our debug messages, and let Glib filter which ones
129                  * to display.
130                  */
131                 if (messages_env != NULL && debug_env == NULL)
132                         debug_env = "all";
133
134                 _gcr_debug_set_flags (debug_env);
135
136                 g_once_init_leave (&initialized_flags, 1);
137         }
138
139         if (flag & current_flags) {
140                 va_start (args, format);
141                 g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, format, args);
142                 va_end (args);
143         }
144 }
145
146 #else /* !WITH_DEBUG */
147
148 gboolean
149 _gcr_debug_flag_is_set (GcrDebugFlags flag)
150 {
151         return FALSE;
152 }
153
154 void
155 _gcr_debug_message (GcrDebugFlags flag, const gchar *format, ...)
156 {
157 }
158
159 void
160 _gcr_debug_set_flags (const gchar *flags_string)
161 {
162 }
163
164 #endif /* !WITH_DEBUG */