1 /* debug.c - helpful output in desperate situations
2 Copyright (C) 2000 Werner Koch (dd9jn)
3 Copyright (C) 2001, 2002, 2003, 2004, 2005, 2007, 2009 g10 Code GmbH
5 This file is part of GPGME.
7 GPGME is free software; you can redistribute it and/or modify it
8 under the terms of the GNU Lesser General Public License as
9 published by the Free Software Foundation; either version 2.1 of
10 the License, or (at your option) any later version.
12 GPGME is distributed in the hope that it will be useful, but
13 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.
17 You should have received a copy of the GNU Lesser General Public
18 License along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
20 MA 02110-1301, USA. */
32 #ifndef HAVE_DOSISH_SYSTEM
33 # include <sys/types.h>
34 # include <sys/stat.h>
48 /* Lock to serialize initialization of the debug output subsystem and
49 output of actual debug messages. */
50 DEFINE_STATIC_LOCK (debug_lock);
52 /* The amount of detail requested by the user, per environment
53 variable GPGME_DEBUG. */
54 static int debug_level;
56 /* The output stream for the debug messages. */
60 /* Remove leading and trailing white spaces. */
62 trim_spaces (char *str)
64 char *string, *p, *mark;
67 /* Find first non space character. */
68 for (p = string; *p && isspace (*(unsigned char *) p); p++)
70 /* Move characters. */
71 for (mark = NULL; (*string = *p); string++, p++)
72 if (isspace (*(unsigned char *) p))
80 *mark = '\0'; /* Remove trailing spaces. */
89 static int initialized;
98 err = _gpgme_getenv ("GPGME_DEBUG", &e);
109 debug_level = atoi (e);
110 s1 = strchr (e, PATHSEP_C);
113 #ifndef HAVE_DOSISH_SYSTEM
114 if (getuid () == geteuid ())
121 if (!(s2 = strchr (s1, PATHSEP_C)))
122 s2 = s1 + strlen (s1);
123 p = malloc (s2 - s1 + 1);
126 memcpy (p, s1, s2 - s1);
132 setvbuf (fp, NULL, _IOLBF, 0);
137 #ifndef HAVE_DOSISH_SYSTEM
145 fprintf (errfp, "gpgme_debug: level=%d\n", debug_level);
147 assuan_set_assuan_log_prefix ("gpgme-assuan");
148 assuan_set_assuan_log_stream (debug_level > 0 ? errfp : NULL);
149 #endif /* HAVE_ASSUAN_H*/
156 /* This should be called as soon as the locks are intialized. It is
157 required so that the assuan logging gets conncted to the gpgme log
158 stream as early as possible. */
160 _gpgme_debug_subsystem_init (void)
168 /* Log the formatted string FORMAT at debug level LEVEL or higher. */
170 _gpgme_debug (int level, const char *format, ...)
178 if (debug_level < level)
181 va_start (arg_ptr, format);
183 vfprintf (errfp, format, arg_ptr);
185 if(format && *format && format[strlen (format) - 1] != '\n')
194 /* Start a new debug line in *LINE, logged at level LEVEL or higher,
195 and starting with the formatted string FORMAT. */
197 _gpgme_debug_begin (void **line, int level, const char *format, ...)
203 if (debug_level < level)
205 /* Disable logging of this line. */
210 va_start (arg_ptr, format);
211 res = vasprintf ((char **) line, format, arg_ptr);
218 /* Add the formatted string FORMAT to the debug line *LINE. */
220 _gpgme_debug_add (void **line, const char *format, ...)
230 va_start (arg_ptr, format);
231 res = vasprintf (&toadd, format, arg_ptr);
238 res = asprintf (&result, "%s%s", *(char **) line, toadd);
248 /* Finish construction of *LINE and send it to the debug output
251 _gpgme_debug_end (void **line)
256 /* The smallest possible level is 1, so force logging here by
258 _gpgme_debug (1, "%s", *line);
264 #define TOHEX(val) (((val) < 10) ? ((val) + '0') : ((val) - 10 + 'a'))
267 _gpgme_debug_buffer (int lvl, const char *const fmt,
268 const char *const func, const char *const tagname,
269 const void *const tag, const char *const buffer,
275 if (!_gpgme_debug_trace ())
282 char *strp2 = &str[34];
284 for (j = 0; j < 16; j++)
290 *(strp++) = TOHEX (val >> 4);
291 *(strp++) = TOHEX (val % 16);
292 *(strp2++) = isprint (val) ? val : '.';
305 _gpgme_debug (lvl, fmt, func, tagname, tag, str);