/[winpt]/trunk/MyGPGME/debug.c
ViewVC logotype

Contents of /trunk/MyGPGME/debug.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 7 - (show annotations)
Mon Apr 4 07:01:43 2005 UTC (19 years, 10 months ago) by twoaday
File MIME type: text/plain
File size: 5774 byte(s)
2005-03-22  Timo Schulz  <twoaday@freakmail.de>
                                                                                
        * editcard.c: Support new status-fd entries SC_OP_SUCCESS
        and SC_OP_FAILURE.
        * editkey.c (cmd_addrev_handler): Check if context != NULL.
        * import.c (import_command_handler): Wrong function signature.
        Noted by Kurt Fitzner.
        * types.h: Fixed encrypt_result_s. Noted by Kurt.
        * gpgme.h (gpgme_editkey_addrev_set): Changed return type.
        Kudos to Kurt.
        * key.c: Removed some unreachable code. By Kurt.
                                                                                


1 /* debug.c
2 * Copyright (C) 2001 Werner Koch (dd9jn)
3 *
4 * This file is part of MyGPGME.
5 *
6 * MyGPGME is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * MyGPGME is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
19 */
20
21 #include <ctype.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <stdarg.h>
26 #include <assert.h>
27 #include <windows.h>
28 #include <process.h>
29 #include <io.h>
30
31 #include "util.h"
32 #include "sema.h"
33
34
35 /* XXX find a way to cleanup all static vars at the end */
36 DEFINE_STATIC_LOCK (debug_lock);
37
38 struct debug_control_s {
39 FILE * fp;
40 char fname[100];
41 };
42
43 static int debug_level = 0;
44 static FILE * errfp = NULL;
45
46
47 void
48 debug_cleanup (void)
49 {
50 DESTROY_LOCK (debug_lock);
51 }
52
53 /****************
54 * remove leading and trailing white spaces
55 */
56 static char *
57 trim_spaces (char *str)
58 {
59 char *string, *p, *mark;
60
61 string = str;
62 /* find first non space character */
63 for( p=string; *p && isspace( *(byte*)p ) ; p++ )
64 ;
65 /* move characters */
66 for( (mark = NULL); (*string = *p) != 0; string++, p++ )
67 if( isspace( *(byte*)p ) ) {
68 if( !mark )
69 mark = string ;
70 }
71 else
72 mark = NULL ;
73 if( mark )
74 *mark = '\0' ; /* remove trailing spaces */
75
76 return str ;
77 }
78
79 static const char*
80 extract_fname( const char *file )
81 {
82 const char *p;
83
84 do {
85 p = strchr( file, '\\' );
86 file += (p-file+1);
87 if ( !strchr( file, '\\' ) )
88 break;
89 } while ( p );
90 return file;
91 }
92
93 static void
94 debug_init (void)
95 {
96 static volatile int initialized = 0;
97
98 if (initialized)
99 return;
100 LOCK (debug_lock);
101 if (!initialized) {
102 const char *e = getenv ("GPGME_DEBUG");
103 const char *s1, *s2;
104
105 initialized = 1;
106 debug_level = 0;
107 errfp = stderr;
108 if (e) {
109 debug_level = atoi (e);
110 s1 = strchr (e, ':');
111 if (s1) {
112 char *p;
113 FILE *fp;
114
115 s1++;
116 if ( (s2 = strchr (s1, ':')) == NULL )
117 s2 = s1 + strlen(s1);
118 p = malloc (s2-s1+1);
119 if (p) {
120 memcpy (p, s1, s2-s1);
121 p[s2-s1] = 0;
122 trim_spaces (p);
123 fp = fopen (p,"a");
124 if (fp) {
125 setvbuf (fp, NULL, _IOLBF, 0);
126 errfp = fp;
127 }
128 safe_free (p);
129 }
130 }
131 }
132
133 if (debug_level > 0)
134 fprintf (errfp,"gpgme_debug: level=%d\n", debug_level);
135 }
136 UNLOCK (debug_lock);
137 }
138
139 int
140 _gpgme_debug_level ()
141 {
142 return debug_level;
143 }
144
145 void
146 _gpgme_debug (int level, int line, const char *fname, const char *format, ...)
147 {
148 va_list arg_ptr ;
149
150 debug_init ();
151 if ( debug_level < level )
152 return;
153
154 LOCK (debug_lock);
155 fprintf(errfp, "%s:%d ", extract_fname( fname ), line );
156 va_start ( arg_ptr, format );
157 vfprintf (errfp, format, arg_ptr);
158 va_end ( arg_ptr ) ;
159 if( format && *format && format[strlen(format)-1] != '\n' )
160 putc ('\n', errfp);
161 UNLOCK (debug_lock);
162 fflush (errfp);
163 }
164
165
166
167 void
168 _gpgme_debug_begin ( void **helper, int level, const char *text)
169 {
170 struct debug_control_s *ctl;
171
172 debug_init ();
173
174 *helper = NULL;
175 if ( debug_level < level )
176 return;
177 ctl = calloc (1, sizeof *ctl );
178 if (!ctl) {
179 _gpgme_debug (255, __LINE__, extract_fname( __FILE__ ), "out of core");
180 return;
181 }
182
183 /* Oh what a pitty that we don't have a asprintf or snprintf under
184 * Windows. We definitely should write our own clib for W32! */
185 sprintf ( ctl->fname, "/tmp/gpgme_debug.%d.%p", getpid (), ctl );
186 ctl->fp = fopen (ctl->fname, "w+");
187 if (!ctl->fp) {
188 _gpgme_debug (255, __LINE__, extract_fname( __FILE__),
189 "failed to create `%s'", ctl->fname );
190 safe_free (ctl);
191 return;
192 }
193 *helper = ctl;
194 _gpgme_debug_add (helper, "%s", text );
195 }
196
197 int
198 _gpgme_debug_enabled (void **helper)
199 {
200 return helper && *helper;
201 }
202
203
204 void
205 _gpgme_debug_add (void **helper, const char *format, ...)
206 {
207 struct debug_control_s *ctl = *helper;
208 va_list arg_ptr ;
209
210 if ( !*helper )
211 return;
212
213 va_start ( arg_ptr, format ) ;
214 vfprintf (ctl->fp, format, arg_ptr) ;
215 va_end ( arg_ptr ) ;
216 }
217
218 void
219 _gpgme_debug_end (void **helper, const char *text)
220 {
221 struct debug_control_s *ctl = *helper;
222 int c, last_c = EOF;
223
224 if (!*helper)
225 return;
226
227 _gpgme_debug_add (helper, "%s", text );
228 fflush (ctl->fp); /* we need this for the buggy Windows libc */
229 rewind (ctl->fp);
230 LOCK (debug_lock);
231 while ( (c=getc (ctl->fp)) != EOF ) {
232 putc (c, stderr);
233 last_c = c;
234 }
235 if (last_c != '\n')
236 putc ('\n', errfp);
237 UNLOCK (debug_lock);
238
239 fclose (ctl->fp);
240 remove (ctl->fname);
241 safe_free (ctl);
242 *helper = NULL;
243 }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26