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. */
63 /* If not NULL, this malloced string is used instead of the
64 GPGME_DEBUG envvar. It must have been set before the debug
65 subsystem has been initialized. Using it later may or may not have
67 static char *envvar_override;
72 static __thread int frame_nr = 0;
76 _gpgme_debug_frame_begin (void)
83 void _gpgme_debug_frame_end (void)
92 /* Remove leading and trailing white spaces. */
94 trim_spaces (char *str)
96 char *string, *p, *mark;
99 /* Find first non space character. */
100 for (p = string; *p && isspace (*(unsigned char *) p); p++)
102 /* Move characters. */
103 for (mark = NULL; (*string = *p); string++, p++)
104 if (isspace (*(unsigned char *) p))
112 *mark = '\0'; /* Remove trailing spaces. */
118 /* This is an internal function to set debug info. The caller must
119 assure that this function is called only by one thread at a time.
120 The function may have no effect if called after the debug system
121 has been initialized. Returns 0 on success. */
123 _gpgme_debug_set_debug_envvar (const char *value)
125 free (envvar_override);
126 envvar_override = strdup (value);
127 return !envvar_override;
134 static int initialized;
141 const char *s1, *s2;;
145 e = strdup (envvar_override);
146 free (envvar_override);
147 envvar_override = NULL;
151 #ifdef HAVE_W32CE_SYSTEM
152 e = _gpgme_w32ce_get_debug_envvar ();
153 #else /*!HAVE_W32CE_SYSTEM*/
154 err = _gpgme_getenv ("GPGME_DEBUG", &e);
160 #endif /*!HAVE_W32CE_SYSTEM*/
167 debug_level = atoi (e);
168 s1 = strchr (e, PATHSEP_C);
171 #ifndef HAVE_DOSISH_SYSTEM
172 if (getuid () == geteuid ()
173 #if defined(HAVE_GETGID) && defined(HAVE_GETEGID)
174 && getgid () == getegid ()
183 if (!(s2 = strchr (s1, PATHSEP_C)))
184 s2 = s1 + strlen (s1);
185 p = malloc (s2 - s1 + 1);
188 memcpy (p, s1, s2 - s1);
194 setvbuf (fp, NULL, _IOLBF, 0);
199 #ifndef HAVE_DOSISH_SYSTEM
209 _gpgme_debug (DEBUG_INIT, "gpgme_debug: level=%d\n", debug_level);
214 /* This should be called as soon as the locks are intialized. It is
215 required so that the assuan logging gets conncted to the gpgme log
216 stream as early as possible. */
218 _gpgme_debug_subsystem_init (void)
226 /* Log the formatted string FORMAT at debug level LEVEL or higher. */
228 _gpgme_debug (int level, const char *format, ...)
234 if (debug_level < level)
237 va_start (arg_ptr, format);
240 #ifdef HAVE_W32CE_SYSTEM
244 fprintf (errfp, "GPGME %04d-%02d-%02d %02d:%02d:%02d <0x%04llx> ",
245 t.wYear, t.wMonth, t.wDay,
246 t.wHour, t.wMinute, t.wSecond,
247 (unsigned long long) ath_self ());
250 time_t atime = time (NULL);
252 tp = localtime (&atime);
253 fprintf (errfp, "GPGME %04d-%02d-%02d %02d:%02d:%02d <0x%04llx> ",
254 1900+tp->tm_year, tp->tm_mon+1, tp->tm_mday,
255 tp->tm_hour, tp->tm_min, tp->tm_sec,
256 (unsigned long long) ath_self ());
262 int nr_spaces = sizeof (spaces) - 1;
265 nr_columns = 2 * (frame_nr - 1);
266 if (nr_columns > nr_spaces)
267 nr_columns = nr_spaces;
270 spaces[nr_columns] = '\0';
271 fprintf (errfp, "%s", spaces);
275 vfprintf (errfp, format, arg_ptr);
277 if(format && *format && format[strlen (format) - 1] != '\n')
282 gpg_err_set_errno (saved_errno);
286 /* Start a new debug line in *LINE, logged at level LEVEL or higher,
287 and starting with the formatted string FORMAT. */
289 _gpgme_debug_begin (void **line, int level, const char *format, ...)
294 if (debug_level < level)
296 /* Disable logging of this line. */
301 va_start (arg_ptr, format);
302 res = vasprintf ((char **) line, format, arg_ptr);
309 /* Add the formatted string FORMAT to the debug line *LINE. */
311 _gpgme_debug_add (void **line, const char *format, ...)
321 va_start (arg_ptr, format);
322 res = vasprintf (&toadd, format, arg_ptr);
329 res = asprintf (&result, "%s%s", *(char **) line, toadd);
339 /* Finish construction of *LINE and send it to the debug output
342 _gpgme_debug_end (void **line)
347 /* The smallest possible level is 1, so force logging here by
349 _gpgme_debug (1, "%s", *line);
355 #define TOHEX(val) (((val) < 10) ? ((val) + '0') : ((val) - 10 + 'a'))
358 _gpgme_debug_buffer (int lvl, const char *const fmt,
359 const char *const func, const char *const buffer,
365 if (!_gpgme_debug_trace ())
372 char *strp2 = &str[34];
374 for (j = 0; j < 16; j++)
380 *(strp++) = TOHEX (val >> 4);
381 *(strp++) = TOHEX (val % 16);
382 *(strp2++) = isprint (val) ? val : '.';
395 _gpgme_debug (lvl, fmt, func, str);