gpgme-tool: Use membuf functions to build up strings.
[gpgme.git] / src / debug.h
1 /* debug.h - interface to debugging functions
2    Copyright (C) 2002, 2004, 2005, 2007 g10 Code GmbH
3
4    This file is part of GPGME.
5
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.
10
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.
15
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
19    02111-1307, USA.  */
20
21 #ifndef DEBUG_H
22 #define DEBUG_H
23
24 #include <string.h>
25 #ifdef HAVE_STDINT_H
26 #include <stdint.h>
27 #endif
28
29 /* Indirect stringification, requires __STDC__ to work.  */
30 #define STRINGIFY(v) #v
31 #define XSTRINGIFY(v) STRINGIFY(v)
32
33 \f
34 /* The debug levels.  */
35
36 #define DEBUG_INIT      1
37 #define DEBUG_GLOBAL    2
38 #define DEBUG_CTX       3
39 #define DEBUG_ENGINE    4
40 #define DEBUG_DATA      5
41 #define DEBUG_ASSUAN    6
42 #define DEBUG_SYSIO     7
43
44 \f
45 /* Remove path components from filenames (i.e. __FILE__) for cleaner
46    logs. */
47 static inline const char *_gpgme_debug_srcname (const char *file)
48                                                 GPGME_GCC_A_PURE;
49
50 static inline const char *
51 _gpgme_debug_srcname (const char *file)
52 {
53   const char *s = strrchr (file, '/');
54   return s? s+1:file;
55 }
56
57 /* Initialization helper function; see debug.c.  */
58 int _gpgme_debug_set_debug_envvar (const char *value);
59
60 /* Called early to initialize the logging.  */
61 void _gpgme_debug_subsystem_init (void);
62
63 /* Log the formatted string FORMAT at debug level LEVEL or higher.  */
64 void _gpgme_debug (int level, const char *format, ...);
65
66 /* Start a new debug line in *LINE, logged at level LEVEL or higher,
67    and starting with the formatted string FORMAT.  */
68 void _gpgme_debug_begin (void **helper, int level, const char *format, ...);
69
70 /* Add the formatted string FORMAT to the debug line *LINE.  */
71 void _gpgme_debug_add (void **helper, const char *format, ...);
72
73 /* Finish construction of *LINE and send it to the debug output
74    stream.  */
75 void _gpgme_debug_end (void **helper);
76
77 void _gpgme_debug_buffer (int lvl, const char *const fmt,
78                           const char *const func, const char *const buffer,
79                           size_t len);
80
81 void _gpgme_debug_frame_begin (void);
82 void _gpgme_debug_frame_end (void);
83
84
85 \f
86 /* Trace support.  */
87
88 /* FIXME: For now.  */
89 #define _gpgme_debug_trace() 1
90
91 #define _TRACE(lvl, name, tag)                                  \
92   int _gpgme_trace_level = lvl;                                 \
93   const char *const _gpgme_trace_func = name;                   \
94   const char *const _gpgme_trace_tagname = STRINGIFY (tag);     \
95   void *_gpgme_trace_tag = (void *) (uintptr_t) tag; \
96   _gpgme_debug_frame_begin ()
97
98 #define TRACE_BEG(lvl, name, tag)                          \
99   _TRACE (lvl, name, tag);                                 \
100   _gpgme_debug (_gpgme_trace_level, "%s: enter: %s=%p\n", \
101                 _gpgme_trace_func, _gpgme_trace_tagname, _gpgme_trace_tag), 0
102 #define TRACE_BEG0(lvl, name, tag, fmt)                                 \
103   _TRACE (lvl, name, tag);                                              \
104   _gpgme_debug (_gpgme_trace_level, "%s: enter: %s=%p, " fmt "\n",      \
105                 _gpgme_trace_func, _gpgme_trace_tagname, _gpgme_trace_tag), 0
106 #define TRACE_BEG1(lvl, name, tag, fmt, arg1)                           \
107   _TRACE (lvl, name, tag);                                              \
108   _gpgme_debug (_gpgme_trace_level, "%s: enter: %s=%p, " fmt "\n",      \
109                 _gpgme_trace_func, _gpgme_trace_tagname, _gpgme_trace_tag, \
110                 arg1), 0
111 #define TRACE_BEG2(lvl, name, tag, fmt, arg1, arg2)                 \
112   _TRACE (lvl, name, tag);                                          \
113   _gpgme_debug (_gpgme_trace_level, "%s: enter: %s=%p, " fmt "\n", \
114                 _gpgme_trace_func, _gpgme_trace_tagname, _gpgme_trace_tag, \
115                 arg1, arg2), 0
116 #define TRACE_BEG3(lvl, name, tag, fmt, arg1, arg2, arg3)           \
117   _TRACE (lvl, name, tag);                                          \
118   _gpgme_debug (_gpgme_trace_level, "%s: enter: %s=%p, " fmt "\n", \
119                 _gpgme_trace_func, _gpgme_trace_tagname, _gpgme_trace_tag, \
120                 arg1, arg2, arg3), 0
121 #define TRACE_BEG4(lvl, name, tag, fmt, arg1, arg2, arg3, arg4)     \
122   _TRACE (lvl, name, tag);                                          \
123   _gpgme_debug (_gpgme_trace_level, "%s: enter: %s=%p, " fmt "\n", \
124                 _gpgme_trace_func, _gpgme_trace_tagname, _gpgme_trace_tag, \
125                 arg1, arg2, arg3, arg4), 0
126 #define TRACE_BEG5(lvl, name, tag, fmt, arg1, arg2, arg3, arg4, arg5) \
127   _TRACE (lvl, name, tag);                                          \
128   _gpgme_debug (_gpgme_trace_level, "%s: enter: %s=%p, " fmt "\n", \
129                 _gpgme_trace_func, _gpgme_trace_tagname, _gpgme_trace_tag, \
130                 arg1, arg2, arg3, arg4, arg5), 0
131 #define TRACE_BEG7(lvl, name, tag, fmt, arg1, arg2, arg3, arg4,     \
132                    arg5, arg6, arg7)                                \
133   _TRACE (lvl, name, tag);                                          \
134   _gpgme_debug (_gpgme_trace_level, "%s: enter: %s=%p, " fmt "\n", \
135                 _gpgme_trace_func, _gpgme_trace_tagname, _gpgme_trace_tag, \
136                 arg1, arg2, arg3, arg4, arg5,       \
137                 arg6, arg7), 0
138 #define TRACE_BEG8(lvl, name, tag, fmt, arg1, arg2, arg3, arg4,     \
139                    arg5, arg6, arg7, arg8)                          \
140   _TRACE (lvl, name, tag);                                          \
141   _gpgme_debug (_gpgme_trace_level, "%s: enter: %s=%p, " fmt "\n", \
142                 _gpgme_trace_func, _gpgme_trace_tagname, _gpgme_trace_tag, \
143                 arg1, arg2, arg3, arg4, arg5,       \
144                 arg6, arg7, arg8), 0
145
146 #define TRACE(lvl, name, tag)                                           \
147   _gpgme_debug_frame_begin (),                                          \
148   _gpgme_debug (lvl, "%s: call: %s=%p\n",                               \
149                 name, STRINGIFY (tag), (void *) (uintptr_t) tag),       \
150   _gpgme_debug_frame_end (), 0
151 #define TRACE0(lvl, name, tag, fmt)                                     \
152   _gpgme_debug_frame_begin (),                                          \
153   _gpgme_debug (lvl, "%s: call: %s=%p, " fmt "\n",                      \
154                 name, STRINGIFY (tag), (void *) (uintptr_t) tag),       \
155   _gpgme_debug_frame_end (), 0
156 #define TRACE1(lvl, name, tag, fmt, arg1)                              \
157   _gpgme_debug_frame_begin (),                                          \
158   _gpgme_debug (lvl, "%s: call: %s=%p, " fmt "\n",                     \
159                 name, STRINGIFY (tag), (void *) (uintptr_t) tag, arg1), \
160   _gpgme_debug_frame_end (), 0
161 #define TRACE2(lvl, name, tag, fmt, arg1, arg2)                        \
162   _gpgme_debug_frame_begin (),                                          \
163   _gpgme_debug (lvl, "%s: call: %s=%p, " fmt "\n",                     \
164                 name, STRINGIFY (tag), (void *) (uintptr_t) tag, arg1, \
165                 arg2), _gpgme_debug_frame_end (), 0
166 #define TRACE3(lvl, name, tag, fmt, arg1, arg2, arg3)                  \
167   _gpgme_debug_frame_begin (),                                          \
168   _gpgme_debug (lvl, "%s: call: %s=%p, " fmt "\n",                     \
169                 name, STRINGIFY (tag), (void *) (uintptr_t) tag, arg1, \
170                 arg2, arg3), _gpgme_debug_frame_end (), 0
171 #define TRACE6(lvl, name, tag, fmt, arg1, arg2, arg3, arg4, arg5, arg6) \
172   _gpgme_debug_frame_begin (),                                          \
173   _gpgme_debug (lvl, "%s: call: %s=%p, " fmt "\n",                      \
174                 name, STRINGIFY (tag), (void *) (uintptr_t) tag, arg1, \
175                 arg2, arg3, arg4, arg5, arg6),                         \
176   _gpgme_debug_frame_end (), 0
177
178 #define TRACE_ERR(err)                                                  \
179   err == 0 ? (TRACE_SUC ()) :                                           \
180     (_gpgme_debug (_gpgme_trace_level, "%s: error: %s <%s>\n",          \
181                    _gpgme_trace_func, gpgme_strerror (err),             \
182                    gpgme_strsource (err)), _gpgme_debug_frame_end (), (err))
183 /* The cast to void suppresses GCC warnings.  */
184 #define TRACE_SYSRES(res)                                               \
185   res >= 0 ? ((void) (TRACE_SUC1 ("result=%i", res)), (res)) :          \
186     (_gpgme_debug (_gpgme_trace_level, "%s: error: %s\n",       \
187                    _gpgme_trace_func, strerror (errno)), _gpgme_debug_frame_end (), (res))
188 #define TRACE_SYSERR(res)                                               \
189   res == 0 ? ((void) (TRACE_SUC1 ("result=%i", res)), (res)) :          \
190     (_gpgme_debug (_gpgme_trace_level, "%s: error: %s\n",               \
191                    _gpgme_trace_func, strerror (res)),                  \
192      _gpgme_debug_frame_end (), (res))
193
194 #define TRACE_SUC()                                              \
195   _gpgme_debug (_gpgme_trace_level, "%s: leave\n",       \
196                 _gpgme_trace_func), _gpgme_debug_frame_end (), 0
197 #define TRACE_SUC0(fmt)                                                 \
198   _gpgme_debug (_gpgme_trace_level, "%s: leave: " fmt "\n",     \
199                 _gpgme_trace_func), _gpgme_debug_frame_end (), 0
200 #define TRACE_SUC1(fmt, arg1)                                           \
201   _gpgme_debug (_gpgme_trace_level, "%s: leave: " fmt "\n",     \
202                 _gpgme_trace_func, arg1), _gpgme_debug_frame_end (), 0
203 #define TRACE_SUC2(fmt, arg1, arg2)                                     \
204   _gpgme_debug (_gpgme_trace_level, "%s: leave: " fmt "\n",     \
205                 _gpgme_trace_func, arg1, arg2), _gpgme_debug_frame_end (), 0
206 #define TRACE_SUC5(fmt, arg1, arg2, arg3, arg4, arg5)                   \
207   _gpgme_debug (_gpgme_trace_level, "%s: leave: " fmt "\n",     \
208                 _gpgme_trace_func, arg1, arg2, arg3, arg4, arg5), \
209     _gpgme_debug_frame_end (), 0
210 #define TRACE_SUC6(fmt, arg1, arg2, arg3, arg4, arg5, arg6)     \
211   _gpgme_debug (_gpgme_trace_level, "%s: leave: " fmt "\n",     \
212                 _gpgme_trace_func, arg1, arg2, arg3, arg4, arg5, arg6), \
213     _gpgme_debug_frame_end (), 0
214
215 #define TRACE_LOG(fmt)                                                  \
216   _gpgme_debug (_gpgme_trace_level, "%s: check: %s=%p, " fmt "\n",      \
217                 _gpgme_trace_func, _gpgme_trace_tagname, _gpgme_trace_tag), 0
218 #define TRACE_LOG1(fmt, arg1)                                           \
219   _gpgme_debug (_gpgme_trace_level, "%s: check: %s=%p, " fmt "\n",      \
220                 _gpgme_trace_func, _gpgme_trace_tagname, _gpgme_trace_tag, \
221                 arg1), 0
222 #define TRACE_LOG2(fmt, arg1, arg2)                                 \
223   _gpgme_debug (_gpgme_trace_level, "%s: check: %s=%p, " fmt "\n", \
224                 _gpgme_trace_func, _gpgme_trace_tagname, _gpgme_trace_tag, \
225                 arg1, arg2), 0
226 #define TRACE_LOG3(fmt, arg1, arg2, arg3)                           \
227   _gpgme_debug (_gpgme_trace_level, "%s: check: %s=%p, " fmt "\n", \
228                 _gpgme_trace_func, _gpgme_trace_tagname, _gpgme_trace_tag, \
229                 arg1, arg2, arg3), 0
230 #define TRACE_LOG4(fmt, arg1, arg2, arg3, arg4)                     \
231   _gpgme_debug (_gpgme_trace_level, "%s: check: %s=%p, " fmt "\n", \
232                 _gpgme_trace_func, _gpgme_trace_tagname, _gpgme_trace_tag, \
233                 arg1, arg2, arg3, arg4), 0
234 #define TRACE_LOG5(fmt, arg1, arg2, arg3, arg4, arg5)               \
235   _gpgme_debug (_gpgme_trace_level, "%s: check: %s=%p, " fmt "\n", \
236                 _gpgme_trace_func, _gpgme_trace_tagname, _gpgme_trace_tag, \
237                 arg1, arg2, arg3, arg4, arg5), 0
238 #define TRACE_LOG6(fmt, arg1, arg2, arg3, arg4, arg5, arg6)         \
239   _gpgme_debug (_gpgme_trace_level, "%s: check: %s=%p, " fmt "\n", \
240                 _gpgme_trace_func, _gpgme_trace_tagname, _gpgme_trace_tag, \
241                 arg1, arg2, arg3, arg4, arg5,       \
242                 arg6), 0
243
244 #define TRACE_LOGBUF(buf, len)                                  \
245   _gpgme_debug_buffer (_gpgme_trace_level, "%s: check: %s",     \
246                        _gpgme_trace_func, buf, len)
247
248 #define TRACE_SEQ(hlp,fmt)                                              \
249   _gpgme_debug_begin (&(hlp), _gpgme_trace_level,                       \
250                       "%s: check: %s=%p, " fmt, _gpgme_trace_func,      \
251                       _gpgme_trace_tagname, _gpgme_trace_tag)
252 #define TRACE_ADD0(hlp,fmt) \
253   _gpgme_debug_add (&(hlp), fmt)
254 #define TRACE_ADD1(hlp,fmt,a) \
255   _gpgme_debug_add (&(hlp), fmt, (a))
256 #define TRACE_ADD2(hlp,fmt,a,b) \
257   _gpgme_debug_add (&(hlp), fmt, (a), (b))
258 #define TRACE_ADD3(hlp,fmt,a,b,c) \
259   _gpgme_debug_add (&(hlp), fmt, (a), (b), (c))
260 #define TRACE_END(hlp,fmt) \
261   _gpgme_debug_add (&(hlp), fmt); \
262   _gpgme_debug_end (&(hlp))
263 #define TRACE_ENABLED(hlp) (!!(hlp))
264
265 #endif  /* DEBUG_H */