1 /* debug.c - helpful output in desperate situations
2 Copyright (C) 2000 Werner Koch (dd9jn)
3 Copyright (C) 2001, 2002, 2003, 2004, 2005, 2007 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 (errfp);
149 assuan_set_assuan_log_level (debug_level >= 0? debug_level:0);
150 #endif /* HAVE_ASSUAN_H*/
157 /* This should be called as soon as the locks are intialized. It is
158 required so that the assuan logging gets conncted to the gpgme log
159 stream as early as possible. */
161 _gpgme_debug_subsystem_init (void)
169 /* Log the formatted string FORMAT at debug level LEVEL or higher. */
171 _gpgme_debug (int level, const char *format, ...)
179 if (debug_level < level)
182 va_start (arg_ptr, format);
184 vfprintf (errfp, format, arg_ptr);
186 if(format && *format && format[strlen (format) - 1] != '\n')
195 /* Start a new debug line in *LINE, logged at level LEVEL or higher,
196 and starting with the formatted string FORMAT. */
198 _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 vasprintf ((char **) line, format, arg_ptr);
216 /* Add the formatted string FORMAT to the debug line *LINE. */
218 _gpgme_debug_add (void **line, const char *format, ...)
227 va_start (arg_ptr, format);
228 vasprintf (&toadd, format, arg_ptr);
230 asprintf (&result, "%s%s", *(char **) line, toadd);
237 /* Finish construction of *LINE and send it to the debug output
240 _gpgme_debug_end (void **line)
245 /* The smallest possible level is 1, so force logging here by
247 _gpgme_debug (1, "%s", *line);
253 #define TOHEX(val) (((val) < 10) ? ((val) + '0') : ((val) - 10 + 'a'))
256 _gpgme_debug_buffer (int lvl, const char *const fmt,
257 const char *const func, const char *const tagname,
258 void *tag, const char *const buffer, size_t len)
263 if (!_gpgme_debug_trace ())
270 char *strp2 = &str[34];
272 for (j = 0; j < 16; j++)
278 *(strp++) = TOHEX (val >> 4);
279 *(strp++) = TOHEX (val % 16);
280 *(strp2++) = isprint (val) ? val : '.';
293 _gpgme_debug (lvl, fmt, func, tagname, tag, str);