Add a git revision number
[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 /* Called early to initialize the logging.  */
58 void _gpgme_debug_subsystem_init (void);
59
60 /* Log the formatted string FORMAT at debug level LEVEL or higher.  */
61 void _gpgme_debug (int level, const char *format, ...);
62
63 /* Start a new debug line in *LINE, logged at level LEVEL or higher,
64    and starting with the formatted string FORMAT.  */
65 void _gpgme_debug_begin (void **helper, int level, const char *format, ...);
66
67 /* Add the formatted string FORMAT to the debug line *LINE.  */
68 void _gpgme_debug_add (void **helper, const char *format, ...);
69
70 /* Finish construction of *LINE and send it to the debug output
71    stream.  */
72 void _gpgme_debug_end (void **helper);
73
74 void _gpgme_debug_buffer (int lvl, const char *const fmt,
75                           const char *const func, const char *const buffer,
76                           size_t len);
77
78 void _gpgme_debug_frame_begin (void);
79 void _gpgme_debug_frame_end (void);
80
81
82 \f
83 /* Trace support.  */
84
85 /* FIXME: For now.  */
86 #define _gpgme_debug_trace() 1
87
88 #define _TRACE(lvl, name, tag)                                  \
89   int _gpgme_trace_level = lvl;                                 \
90   const char *const _gpgme_trace_func = name;                   \
91   const char *const _gpgme_trace_tagname = STRINGIFY (tag);     \
92   void *_gpgme_trace_tag = (void *) (uintptr_t) tag; \
93   _gpgme_debug_frame_begin ()
94
95 #define TRACE_BEG(lvl, name, tag)                          \
96   _TRACE (lvl, name, tag);                                 \
97   _gpgme_debug (_gpgme_trace_level, "%s: enter: %s=%p\n", \
98                 _gpgme_trace_func, _gpgme_trace_tagname, _gpgme_trace_tag), 0
99 #define TRACE_BEG0(lvl, name, tag, fmt)                                 \
100   _TRACE (lvl, name, tag);                                              \
101   _gpgme_debug (_gpgme_trace_level, "%s: enter: %s=%p, " fmt "\n",      \
102                 _gpgme_trace_func, _gpgme_trace_tagname, _gpgme_trace_tag), 0
103 #define TRACE_BEG1(lvl, name, tag, fmt, arg1)                           \
104   _TRACE (lvl, name, tag);                                              \
105   _gpgme_debug (_gpgme_trace_level, "%s: enter: %s=%p, " fmt "\n",      \
106                 _gpgme_trace_func, _gpgme_trace_tagname, _gpgme_trace_tag, \
107                 arg1), 0
108 #define TRACE_BEG2(lvl, name, tag, fmt, arg1, arg2)                 \
109   _TRACE (lvl, name, tag);                                          \
110   _gpgme_debug (_gpgme_trace_level, "%s: enter: %s=%p, " fmt "\n", \
111                 _gpgme_trace_func, _gpgme_trace_tagname, _gpgme_trace_tag, \
112                 arg1, arg2), 0
113 #define TRACE_BEG3(lvl, name, tag, fmt, arg1, arg2, arg3)           \
114   _TRACE (lvl, name, tag);                                          \
115   _gpgme_debug (_gpgme_trace_level, "%s: enter: %s=%p, " fmt "\n", \
116                 _gpgme_trace_func, _gpgme_trace_tagname, _gpgme_trace_tag, \
117                 arg1, arg2, arg3), 0
118 #define TRACE_BEG4(lvl, name, tag, fmt, arg1, arg2, arg3, arg4)     \
119   _TRACE (lvl, name, tag);                                          \
120   _gpgme_debug (_gpgme_trace_level, "%s: enter: %s=%p, " fmt "\n", \
121                 _gpgme_trace_func, _gpgme_trace_tagname, _gpgme_trace_tag, \
122                 arg1, arg2, arg3, arg4), 0
123 #define TRACE_BEG5(lvl, name, tag, fmt, arg1, arg2, arg3, arg4, arg5) \
124   _TRACE (lvl, name, tag);                                          \
125   _gpgme_debug (_gpgme_trace_level, "%s: enter: %s=%p, " fmt "\n", \
126                 _gpgme_trace_func, _gpgme_trace_tagname, _gpgme_trace_tag, \
127                 arg1, arg2, arg3, arg4, arg5), 0
128 #define TRACE_BEG7(lvl, name, tag, fmt, arg1, arg2, arg3, arg4,     \
129                    arg5, arg6, arg7)                                \
130   _TRACE (lvl, name, tag);                                          \
131   _gpgme_debug (_gpgme_trace_level, "%s: enter: %s=%p, " fmt "\n", \
132                 _gpgme_trace_func, _gpgme_trace_tagname, _gpgme_trace_tag, \
133                 arg1, arg2, arg3, arg4, arg5,       \
134                 arg6, arg7), 0
135 #define TRACE_BEG8(lvl, name, tag, fmt, arg1, arg2, arg3, arg4,     \
136                    arg5, arg6, arg7, arg8)                          \
137   _TRACE (lvl, name, tag);                                          \
138   _gpgme_debug (_gpgme_trace_level, "%s: enter: %s=%p, " fmt "\n", \
139                 _gpgme_trace_func, _gpgme_trace_tagname, _gpgme_trace_tag, \
140                 arg1, arg2, arg3, arg4, arg5,       \
141                 arg6, arg7, arg8), 0
142
143 #define TRACE(lvl, name, tag)                                           \
144   _gpgme_debug_frame_begin (),                                          \
145   _gpgme_debug (lvl, "%s: call: %s=%p\n",                               \
146                 name, STRINGIFY (tag), (void *) (uintptr_t) tag),       \
147   _gpgme_debug_frame_end (), 0
148 #define TRACE0(lvl, name, tag, fmt)                                     \
149   _gpgme_debug_frame_begin (),                                          \
150   _gpgme_debug (lvl, "%s: call: %s=%p, " fmt "\n",                      \
151                 name, STRINGIFY (tag), (void *) (uintptr_t) tag),       \
152   _gpgme_debug_frame_end (), 0
153 #define TRACE1(lvl, name, tag, fmt, arg1)                              \
154   _gpgme_debug_frame_begin (),                                          \
155   _gpgme_debug (lvl, "%s: call: %s=%p, " fmt "\n",                     \
156                 name, STRINGIFY (tag), (void *) (uintptr_t) tag, arg1), \
157   _gpgme_debug_frame_end (), 0
158 #define TRACE2(lvl, name, tag, fmt, arg1, arg2)                        \
159   _gpgme_debug_frame_begin (),                                          \
160   _gpgme_debug (lvl, "%s: call: %s=%p, " fmt "\n",                     \
161                 name, STRINGIFY (tag), (void *) (uintptr_t) tag, arg1, \
162                 arg2), _gpgme_debug_frame_end (), 0
163 #define TRACE3(lvl, name, tag, fmt, arg1, arg2, arg3)                  \
164   _gpgme_debug_frame_begin (),                                          \
165   _gpgme_debug (lvl, "%s: call: %s=%p, " fmt "\n",                     \
166                 name, STRINGIFY (tag), (void *) (uintptr_t) tag, arg1, \
167                 arg2, arg3), _gpgme_debug_frame_end (), 0
168 #define TRACE6(lvl, name, tag, fmt, arg1, arg2, arg3, arg4, arg5, arg6) \
169   _gpgme_debug_frame_begin (),                                          \
170   _gpgme_debug (lvl, "%s: call: %s=%p, " fmt "\n",                      \
171                 name, STRINGIFY (tag), (void *) (uintptr_t) tag, arg1, \
172                 arg2, arg3, arg4, arg5, arg6),                         \
173   _gpgme_debug_frame_end (), 0
174
175 #define TRACE_ERR(err)                                                  \
176   err == 0 ? (TRACE_SUC ()) :                                           \
177     (_gpgme_debug (_gpgme_trace_level, "%s: error: %s <%s>\n",          \
178                    _gpgme_trace_func, gpgme_strerror (err),             \
179                    gpgme_strsource (err)), _gpgme_debug_frame_end (), (err))
180 /* The cast to void suppresses GCC warnings.  */
181 #define TRACE_SYSRES(res)                                               \
182   res >= 0 ? ((void) (TRACE_SUC1 ("result=%i", res)), (res)) :          \
183     (_gpgme_debug (_gpgme_trace_level, "%s: error: %s\n",       \
184                    _gpgme_trace_func, strerror (errno)), _gpgme_debug_frame_end (), (res))
185 #define TRACE_SYSERR(res)                                               \
186   res == 0 ? ((void) (TRACE_SUC1 ("result=%i", res)), (res)) :          \
187     (_gpgme_debug (_gpgme_trace_level, "%s: error: %s\n",               \
188                    _gpgme_trace_func, strerror (res)),                  \
189      _gpgme_debug_frame_end (), (res))
190
191 #define TRACE_SUC()                                              \
192   _gpgme_debug (_gpgme_trace_level, "%s: leave\n",       \
193                 _gpgme_trace_func), _gpgme_debug_frame_end (), 0
194 #define TRACE_SUC0(fmt)                                                 \
195   _gpgme_debug (_gpgme_trace_level, "%s: leave: " fmt "\n",     \
196                 _gpgme_trace_func), _gpgme_debug_frame_end (), 0
197 #define TRACE_SUC1(fmt, arg1)                                           \
198   _gpgme_debug (_gpgme_trace_level, "%s: leave: " fmt "\n",     \
199                 _gpgme_trace_func, arg1), _gpgme_debug_frame_end (), 0
200 #define TRACE_SUC2(fmt, arg1, arg2)                                     \
201   _gpgme_debug (_gpgme_trace_level, "%s: leave: " fmt "\n",     \
202                 _gpgme_trace_func, arg1, arg2), _gpgme_debug_frame_end (), 0
203 #define TRACE_SUC5(fmt, arg1, arg2, arg3, arg4, arg5)                   \
204   _gpgme_debug (_gpgme_trace_level, "%s: leave: " fmt "\n",     \
205                 _gpgme_trace_func, arg1, arg2, arg3, arg4, arg5), \
206     _gpgme_debug_frame_end (), 0
207 #define TRACE_SUC6(fmt, arg1, arg2, arg3, arg4, arg5, arg6)     \
208   _gpgme_debug (_gpgme_trace_level, "%s: leave: " fmt "\n",     \
209                 _gpgme_trace_func, arg1, arg2, arg3, arg4, arg5, arg6), \
210     _gpgme_debug_frame_end (), 0
211
212 #define TRACE_LOG(fmt)                                                  \
213   _gpgme_debug (_gpgme_trace_level, "%s: check: %s=%p, " fmt "\n",      \
214                 _gpgme_trace_func, _gpgme_trace_tagname, _gpgme_trace_tag), 0
215 #define TRACE_LOG1(fmt, arg1)                                           \
216   _gpgme_debug (_gpgme_trace_level, "%s: check: %s=%p, " fmt "\n",      \
217                 _gpgme_trace_func, _gpgme_trace_tagname, _gpgme_trace_tag, \
218                 arg1), 0
219 #define TRACE_LOG2(fmt, arg1, arg2)                                 \
220   _gpgme_debug (_gpgme_trace_level, "%s: check: %s=%p, " fmt "\n", \
221                 _gpgme_trace_func, _gpgme_trace_tagname, _gpgme_trace_tag, \
222                 arg1, arg2), 0
223 #define TRACE_LOG3(fmt, arg1, arg2, arg3)                           \
224   _gpgme_debug (_gpgme_trace_level, "%s: check: %s=%p, " fmt "\n", \
225                 _gpgme_trace_func, _gpgme_trace_tagname, _gpgme_trace_tag, \
226                 arg1, arg2, arg3), 0
227 #define TRACE_LOG4(fmt, arg1, arg2, arg3, arg4)                     \
228   _gpgme_debug (_gpgme_trace_level, "%s: check: %s=%p, " fmt "\n", \
229                 _gpgme_trace_func, _gpgme_trace_tagname, _gpgme_trace_tag, \
230                 arg1, arg2, arg3, arg4), 0
231 #define TRACE_LOG5(fmt, arg1, arg2, arg3, arg4, arg5)               \
232   _gpgme_debug (_gpgme_trace_level, "%s: check: %s=%p, " fmt "\n", \
233                 _gpgme_trace_func, _gpgme_trace_tagname, _gpgme_trace_tag, \
234                 arg1, arg2, arg3, arg4, arg5), 0
235 #define TRACE_LOG6(fmt, arg1, arg2, arg3, arg4, arg5, arg6)         \
236   _gpgme_debug (_gpgme_trace_level, "%s: check: %s=%p, " fmt "\n", \
237                 _gpgme_trace_func, _gpgme_trace_tagname, _gpgme_trace_tag, \
238                 arg1, arg2, arg3, arg4, arg5,       \
239                 arg6), 0
240
241 #define TRACE_LOGBUF(buf, len)                                  \
242   _gpgme_debug_buffer (_gpgme_trace_level, "%s: check: %s",     \
243                        _gpgme_trace_func, buf, len)
244
245 #define TRACE_SEQ(hlp,fmt)                                              \
246   _gpgme_debug_begin (&(hlp), _gpgme_trace_level,                       \
247                       "%s: check: %s=%p, " fmt, _gpgme_trace_func,      \
248                       _gpgme_trace_tagname, _gpgme_trace_tag)           
249 #define TRACE_ADD0(hlp,fmt) \
250   _gpgme_debug_add (&(hlp), fmt)
251 #define TRACE_ADD1(hlp,fmt,a) \
252   _gpgme_debug_add (&(hlp), fmt, (a))
253 #define TRACE_ADD2(hlp,fmt,a,b) \
254   _gpgme_debug_add (&(hlp), fmt, (a), (b))
255 #define TRACE_ADD3(hlp,fmt,a,b,c) \
256   _gpgme_debug_add (&(hlp), fmt, (a), (b), (c))
257 #define TRACE_END(hlp,fmt) \
258   _gpgme_debug_add (&(hlp), fmt); \
259   _gpgme_debug_end (&(hlp))
260 #define TRACE_ENABLED(hlp) (!!(hlp))
261
262 #endif  /* DEBUG_H */