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. */
35 #ifndef HAVE_DOSISH_SYSTEM
36 # ifdef HAVE_SYS_TYPES_H
37 # include <sys/types.h>
39 # ifdef HAVE_SYS_STAT_H
40 # include <sys/stat.h>
52 /* Lock to serialize initialization of the debug output subsystem and
53 output of actual debug messages. */
54 DEFINE_STATIC_LOCK (debug_lock);
56 /* The amount of detail requested by the user, per environment
57 variable GPGME_DEBUG. */
58 static int debug_level;
60 /* The output stream for the debug messages. */
66 static __thread int frame_nr = 0;
70 _gpgme_debug_frame_begin (void)
77 void _gpgme_debug_frame_end (void)
86 /* Remove leading and trailing white spaces. */
88 trim_spaces (char *str)
90 char *string, *p, *mark;
93 /* Find first non space character. */
94 for (p = string; *p && isspace (*(unsigned char *) p); p++)
96 /* Move characters. */
97 for (mark = NULL; (*string = *p); string++, p++)
98 if (isspace (*(unsigned char *) p))
106 *mark = '\0'; /* Remove trailing spaces. */
115 static int initialized;
122 const char *s1, *s2;;
124 #ifdef HAVE_W32CE_SYSTEM
125 e = _gpgme_w32ce_get_debug_envvar ();
126 #else /*!HAVE_W32CE_SYSTEM*/
127 err = _gpgme_getenv ("GPGME_DEBUG", &e);
133 #endif /*!HAVE_W32CE_SYSTEM*/
139 debug_level = atoi (e);
140 s1 = strchr (e, PATHSEP_C);
143 #ifndef HAVE_DOSISH_SYSTEM
144 if (getuid () == geteuid ()
145 #if defined(HAVE_GETGID) && defined(HAVE_GETEGID)
146 && getgid () == getegid ()
155 if (!(s2 = strchr (s1, PATHSEP_C)))
156 s2 = s1 + strlen (s1);
157 p = malloc (s2 - s1 + 1);
160 memcpy (p, s1, s2 - s1);
166 setvbuf (fp, NULL, _IOLBF, 0);
171 #ifndef HAVE_DOSISH_SYSTEM
181 _gpgme_debug (DEBUG_INIT, "gpgme_debug: level=%d\n", debug_level);
186 /* This should be called as soon as the locks are intialized. It is
187 required so that the assuan logging gets conncted to the gpgme log
188 stream as early as possible. */
190 _gpgme_debug_subsystem_init (void)
198 /* Log the formatted string FORMAT at debug level LEVEL or higher. */
200 _gpgme_debug (int level, const char *format, ...)
206 if (debug_level < level)
209 va_start (arg_ptr, format);
212 #ifdef HAVE_W32CE_SYSTEM
216 fprintf (errfp, "GPGME %04d-%02d-%02d %02d:%02d:%02d <0x%04llx> ",
217 t.wYear, t.wMonth, t.wDay,
218 t.wHour, t.wMinute, t.wSecond,
219 (unsigned long long) ath_self ());
222 time_t atime = time (NULL);
224 tp = localtime (&atime);
225 fprintf (errfp, "GPGME %04d-%02d-%02d %02d:%02d:%02d <0x%04llx> ",
226 1900+tp->tm_year, tp->tm_mon+1, tp->tm_mday,
227 tp->tm_hour, tp->tm_min, tp->tm_sec,
228 (unsigned long long) ath_self ());
234 int nr_spaces = sizeof (spaces) - 1;
237 nr_columns = 2 * (frame_nr - 1);
238 if (nr_columns > nr_spaces)
239 nr_columns = nr_spaces;
242 spaces[nr_columns] = '\0';
243 fprintf (errfp, "%s", spaces);
247 vfprintf (errfp, format, arg_ptr);
249 if(format && *format && format[strlen (format) - 1] != '\n')
254 gpg_err_set_errno (saved_errno);
258 /* Start a new debug line in *LINE, logged at level LEVEL or higher,
259 and starting with the formatted string FORMAT. */
261 _gpgme_debug_begin (void **line, int level, const char *format, ...)
266 if (debug_level < level)
268 /* Disable logging of this line. */
273 va_start (arg_ptr, format);
274 res = vasprintf ((char **) line, format, arg_ptr);
281 /* Add the formatted string FORMAT to the debug line *LINE. */
283 _gpgme_debug_add (void **line, const char *format, ...)
293 va_start (arg_ptr, format);
294 res = vasprintf (&toadd, format, arg_ptr);
301 res = asprintf (&result, "%s%s", *(char **) line, toadd);
311 /* Finish construction of *LINE and send it to the debug output
314 _gpgme_debug_end (void **line)
319 /* The smallest possible level is 1, so force logging here by
321 _gpgme_debug (1, "%s", *line);
327 #define TOHEX(val) (((val) < 10) ? ((val) + '0') : ((val) - 10 + 'a'))
330 _gpgme_debug_buffer (int lvl, const char *const fmt,
331 const char *const func, const char *const buffer,
337 if (!_gpgme_debug_trace ())
344 char *strp2 = &str[34];
346 for (j = 0; j < 16; j++)
352 *(strp++) = TOHEX (val >> 4);
353 *(strp++) = TOHEX (val % 16);
354 *(strp2++) = isprint (val) ? val : '.';
367 _gpgme_debug (lvl, fmt, func, str);