1
/* debug.h - interface to debugging functions
2
Copyright (C) 2002, 2004, 2005, 2007 g10 Code GmbH
4
This file is part of GPGME.
6
GPGME is free software; you can redistribute it and/or modify it
7
under the terms of the GNU Lesser General Public License as
8
published by the Free Software Foundation; either version 2.1 of
9
the License, or (at your option) any later version.
11
GPGME is distributed in the hope that it will be useful, but
12
WITHOUT ANY WARRANTY; without even the implied warranty of
13
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14
Lesser General Public License for more details.
16
You should have received a copy of the GNU Lesser General Public
17
License along with this program; if not, write to the Free Software
18
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
26
/* Indirect stringification, requires __STDC__ to work. */
27
#define STRINGIFY(v) #v
28
#define XSTRINGIFY(v) STRINGIFY(v)
31
/* The debug levels. */
35
#define DEBUG_ENGINE 3
37
#define DEBUG_ASSUAN 5
41
/* Remove path components from filenames (i.e. __FILE__) for cleaner
43
static inline const char *_gpgme_debug_srcname (const char *file)
46
static inline const char *
47
_gpgme_debug_srcname (const char *file)
49
const char *s = strrchr (file, '/');
53
/* Called early to initialize the logging. */
54
void _gpgme_debug_subsystem_init (void);
56
/* Log the formatted string FORMAT at debug level LEVEL or higher. */
57
void _gpgme_debug (int level, const char *format, ...);
59
/* Start a new debug line in *LINE, logged at level LEVEL or higher,
60
and starting with the formatted string FORMAT. */
61
void _gpgme_debug_begin (void **helper, int level, const char *format, ...);
63
/* Add the formatted string FORMAT to the debug line *LINE. */
64
void _gpgme_debug_add (void **helper, const char *format, ...);
66
/* Finish construction of *LINE and send it to the debug output
68
void _gpgme_debug_end (void **helper);
70
void _gpgme_debug_buffer (int lvl, const char *const fmt,
71
const char *const func, const char *const tagname,
72
void *tag, const char *const buffer, size_t len);
78
#define _gpgme_debug_trace() 1
80
#define _TRACE(lvl, name, tag) \
81
int _gpgme_trace_level = lvl; \
82
const char *const _gpgme_trace_func = name; \
83
const char *const _gpgme_trace_tagname = STRINGIFY (tag); \
84
void *_gpgme_trace_tag = (void *) tag
86
#define TRACE_BEG(lvl, name, tag) \
87
_TRACE (lvl, name, tag); \
88
_gpgme_debug (_gpgme_trace_level, "%s (%s=0x%x): enter\n", \
89
_gpgme_trace_func, _gpgme_trace_tagname, \
91
#define TRACE_BEG0(lvl, name, tag, fmt) \
92
_TRACE (lvl, name, tag); \
93
_gpgme_debug (_gpgme_trace_level, "%s (%s=0x%x): enter: " fmt "\n", \
94
_gpgme_trace_func, _gpgme_trace_tagname, \
96
#define TRACE_BEG1(lvl, name, tag, fmt, arg1) \
97
_TRACE (lvl, name, tag); \
98
_gpgme_debug (_gpgme_trace_level, "%s (%s=0x%x): enter: " fmt "\n", \
99
_gpgme_trace_func, _gpgme_trace_tagname, \
100
_gpgme_trace_tag, arg1), 0
101
#define TRACE_BEG2(lvl, name, tag, fmt, arg1, arg2) \
102
_TRACE (lvl, name, tag); \
103
_gpgme_debug (_gpgme_trace_level, "%s (%s=0x%x): enter: " fmt "\n", \
104
_gpgme_trace_func, _gpgme_trace_tagname, \
105
_gpgme_trace_tag, arg1, arg2), 0
106
#define TRACE_BEG3(lvl, name, tag, fmt, arg1, arg2, arg3) \
107
_TRACE (lvl, name, tag); \
108
_gpgme_debug (_gpgme_trace_level, "%s (%s=0x%x): enter: " fmt "\n", \
109
_gpgme_trace_func, _gpgme_trace_tagname, \
110
_gpgme_trace_tag, arg1, arg2, arg3), 0
111
#define TRACE_BEG4(lvl, name, tag, fmt, arg1, arg2, arg3, arg4) \
112
_TRACE (lvl, name, tag); \
113
_gpgme_debug (_gpgme_trace_level, "%s (%s=0x%x): enter: " fmt "\n", \
114
_gpgme_trace_func, _gpgme_trace_tagname, \
115
_gpgme_trace_tag, arg1, arg2, arg3, arg4), 0
117
#define TRACE(lvl, name, tag) \
118
_gpgme_debug (lvl, "%s (%s=0x%x): call\n", \
119
name, STRINGIFY (tag), (void *) tag), 0
120
#define TRACE0(lvl, name, tag, fmt) \
121
_gpgme_debug (lvl, "%s (%s=0x%x): call: " fmt "\n", \
122
name, STRINGIFY (tag), (void *) tag), 0
123
#define TRACE1(lvl, name, tag, fmt, arg1) \
124
_gpgme_debug (lvl, "%s (%s=0x%x): call: " fmt "\n", \
125
name, STRINGIFY (tag), (void *) tag, arg1), 0
126
#define TRACE2(lvl, name, tag, fmt, arg1, arg2) \
127
_gpgme_debug (lvl, "%s (%s=0x%x): call: " fmt "\n", \
128
name, STRINGIFY (tag), (void *) tag, arg1, arg2), 0
129
#define TRACE3(lvl, name, tag, fmt, arg1, arg2, arg3) \
130
_gpgme_debug (lvl, "%s (%s=0x%x): call: " fmt "\n", \
131
name, STRINGIFY (tag), (void *) tag, arg1, arg2, \
133
#define TRACE6(lvl, name, tag, fmt, arg1, arg2, arg3, arg4, arg5, arg6) \
134
_gpgme_debug (lvl, "%s (%s=0x%x): call: " fmt "\n", \
135
name, STRINGIFY (tag), (void *) tag, arg1, arg2, arg3, \
138
#define TRACE_ERR(err) \
139
err == 0 ? (TRACE_SUC ()) : \
140
(_gpgme_debug (_gpgme_trace_level, "%s (%s=0x%x): error: %s <%s>\n", \
141
_gpgme_trace_func, _gpgme_trace_tagname, \
142
_gpgme_trace_tag, gpgme_strerror (err), \
143
gpgme_strsource (err)), (err))
144
/* The cast to void suppresses GCC warnings. */
145
#define TRACE_SYSRES(res) \
146
res >= 0 ? ((void) (TRACE_SUC1 ("result=%i", res)), (res)) : \
147
(_gpgme_debug (_gpgme_trace_level, "%s (%s=0x%x): error: %s\n", \
148
_gpgme_trace_func, _gpgme_trace_tagname, \
149
_gpgme_trace_tag, strerror (errno)), (res))
150
#define TRACE_SYSERR(res) \
151
res == 0 ? ((void) (TRACE_SUC1 ("result=%i", res)), (res)) : \
152
(_gpgme_debug (_gpgme_trace_level, "%s (%s=0x%x): error: %s\n", \
153
_gpgme_trace_func, _gpgme_trace_tagname, \
154
_gpgme_trace_tag, strerror (res)), (res))
156
#define TRACE_SUC() \
157
_gpgme_debug (_gpgme_trace_level, "%s (%s=0x%x): leave\n", \
158
_gpgme_trace_func, _gpgme_trace_tagname, \
160
#define TRACE_SUC0(fmt) \
161
_gpgme_debug (_gpgme_trace_level, "%s (%s=0x%x): leave: " fmt "\n", \
162
_gpgme_trace_func, _gpgme_trace_tagname, \
164
#define TRACE_SUC1(fmt, arg1) \
165
_gpgme_debug (_gpgme_trace_level, "%s (%s=0x%x): leave: " fmt "\n", \
166
_gpgme_trace_func, _gpgme_trace_tagname, \
167
_gpgme_trace_tag, arg1), 0
168
#define TRACE_SUC2(fmt, arg1, arg2) \
169
_gpgme_debug (_gpgme_trace_level, "%s (%s=0x%x): leave: " fmt "\n", \
170
_gpgme_trace_func, _gpgme_trace_tagname, \
171
_gpgme_trace_tag, arg1, arg2), 0
172
#define TRACE_SUC5(fmt, arg1, arg2, arg3, arg4, arg5) \
173
_gpgme_debug (_gpgme_trace_level, "%s (%s=0x%x): leave: " fmt "\n", \
174
_gpgme_trace_func, _gpgme_trace_tagname, \
175
_gpgme_trace_tag, arg1, arg2, arg3, arg4, arg5), 0
177
#define TRACE_LOG(fmt) \
178
_gpgme_debug (_gpgme_trace_level, "%s (%s=0x%x): check: " fmt "\n", \
179
_gpgme_trace_func, _gpgme_trace_tagname, \
181
#define TRACE_LOG1(fmt, arg1) \
182
_gpgme_debug (_gpgme_trace_level, "%s (%s=0x%x): check: " fmt "\n", \
183
_gpgme_trace_func, _gpgme_trace_tagname, \
184
_gpgme_trace_tag, arg1), 0
185
#define TRACE_LOG2(fmt, arg1, arg2) \
186
_gpgme_debug (_gpgme_trace_level, "%s (%s=0x%x): check: " fmt "\n", \
187
_gpgme_trace_func, _gpgme_trace_tagname, \
188
_gpgme_trace_tag, arg1, arg2), 0
189
#define TRACE_LOG3(fmt, arg1, arg2, arg3) \
190
_gpgme_debug (_gpgme_trace_level, "%s (%s=0x%x): check: " fmt "\n", \
191
_gpgme_trace_func, _gpgme_trace_tagname, \
192
_gpgme_trace_tag, arg1, arg2, arg3), 0
193
#define TRACE_LOG4(fmt, arg1, arg2, arg3, arg4) \
194
_gpgme_debug (_gpgme_trace_level, "%s (%s=0x%x): check: " fmt "\n", \
195
_gpgme_trace_func, _gpgme_trace_tagname, \
196
_gpgme_trace_tag, arg1, arg2, arg3, arg4), 0
197
#define TRACE_LOG6(fmt, arg1, arg2, arg3, arg4, arg5, arg6) \
198
_gpgme_debug (_gpgme_trace_level, "%s (%s=0x%x): check: " fmt "\n", \
199
_gpgme_trace_func, _gpgme_trace_tagname, \
200
_gpgme_trace_tag, arg1, arg2, arg3, arg4, arg5, \
203
#define TRACE_LOGBUF(buf, len) \
204
_gpgme_debug_buffer (_gpgme_trace_level, "%s (%s=0x%x): check: %s", \
205
_gpgme_trace_func, _gpgme_trace_tagname, \
206
_gpgme_trace_tag, buf, len)
208
#define TRACE_SEQ(hlp,fmt) \
209
_gpgme_debug_begin (&(hlp), _gpgme_trace_level, \
210
"%s (%s=0x%x): check: " fmt, \
211
_gpgme_trace_func, _gpgme_trace_tagname, \
213
#define TRACE_ADD0(hlp,fmt) \
214
_gpgme_debug_add (&(hlp), fmt)
215
#define TRACE_ADD1(hlp,fmt,a) \
216
_gpgme_debug_add (&(hlp), fmt, (a))
217
#define TRACE_ADD2(hlp,fmt,a,b) \
218
_gpgme_debug_add (&(hlp), fmt, (a), (b))
219
#define TRACE_ADD3(hlp,fmt,a,b,c) \
220
_gpgme_debug_add (&(hlp), fmt, (a), (b), (c))
221
#define TRACE_END(hlp,fmt) \
222
_gpgme_debug_add (&(hlp), fmt); \
223
_gpgme_debug_end (&(hlp))
224
#define TRACE_ENABLED(hlp) (!!(hlp))