1 /* debug.c - helpful output in desperate situations
2 Copyright (C) 2000 Werner Koch (dd9jn)
3 Copyright (C) 2001, 2002, 2003 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 General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (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 General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GPGME; if not, write to the Free Software Foundation,
19 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
30 #ifndef HAVE_DOSISH_SYSTEM
31 #include <sys/types.h>
41 /* Lock to serialize initialization of the debug output subsystem and
42 output of actual debug messages. */
43 DEFINE_STATIC_LOCK (debug_lock);
45 /* The amount of detail requested by the user, per environment
46 variable GPGME_DEBUG. */
47 static int debug_level;
49 /* The output stream for the debug messages. */
53 /* Remove leading and trailing white spaces. */
55 trim_spaces (char *str)
57 char *string, *p, *mark;
60 /* Find first non space character. */
61 for (p = string; *p && isspace (*(unsigned char *) p); p++)
63 /* Move characters. */
64 for (mark = NULL; (*string = *p); string++, p++)
65 if (isspace (*(unsigned char *) p))
73 *mark = '\0'; /* Remove trailing spaces. */
82 static int initialized;
91 err = _gpgme_getenv ("GPGME_DEBUG", &e);
102 debug_level = atoi (e);
103 s1 = strchr (e, ':');
106 #ifndef HAVE_DOSISH_SYSTEM
107 if (getuid () == geteuid ())
114 if (!(s2 = strchr (s1, ':')))
115 s2 = s1 + strlen (s1);
116 p = malloc (s2 - s1 + 1);
119 memcpy (p, s1, s2 - s1);
125 setvbuf (fp, NULL, _IOLBF, 0);
130 #ifndef HAVE_DOSISH_SYSTEM
138 fprintf (errfp, "gpgme_debug: level=%d\n", debug_level);
144 /* Log the formatted string FORMAT at debug level LEVEL or higher. */
146 _gpgme_debug (int level, const char *format, ...)
151 if (debug_level < level)
154 va_start (arg_ptr, format);
156 vfprintf (errfp, format, arg_ptr);
158 if(format && *format && format[strlen (format) - 1] != '\n')
165 /* Start a new debug line in *LINE, logged at level LEVEL or higher,
166 and starting with the formatted string FORMAT. */
168 _gpgme_debug_begin (void **line, int level, const char *format, ...)
173 if (debug_level < level)
175 /* Disable logging of this line. */
180 va_start (arg_ptr, format);
181 vasprintf ((char **) line, format, arg_ptr);
186 /* Add the formatted string FORMAT to the debug line *LINE. */
188 _gpgme_debug_add (void **line, const char *format, ...)
197 va_start (arg_ptr, format);
198 vasprintf (&toadd, format, arg_ptr);
200 asprintf (&result, "%s%s", *(char **) line, toadd);
207 /* Finish construction of *LINE and send it to the debug output
210 _gpgme_debug_end (void **line)
215 /* The smallest possible level is 1, so force logging here by
217 _gpgme_debug (1, "%s", *line);