]> ncurses.scripts.mit.edu Git - ncurses.git/blob - ncurses/trace/lib_trace.c
ncurses 5.6 - patch 20071117
[ncurses.git] / ncurses / trace / lib_trace.c
1 /****************************************************************************
2  * Copyright (c) 1998-2006,2007 Free Software Foundation, Inc.              *
3  *                                                                          *
4  * Permission is hereby granted, free of charge, to any person obtaining a  *
5  * copy of this software and associated documentation files (the            *
6  * "Software"), to deal in the Software without restriction, including      *
7  * without limitation the rights to use, copy, modify, merge, publish,      *
8  * distribute, distribute with modifications, sublicense, and/or sell       *
9  * copies of the Software, and to permit persons to whom the Software is    *
10  * furnished to do so, subject to the following conditions:                 *
11  *                                                                          *
12  * The above copyright notice and this permission notice shall be included  *
13  * in all copies or substantial portions of the Software.                   *
14  *                                                                          *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
16  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
17  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
18  * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
19  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
20  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
21  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
22  *                                                                          *
23  * Except as contained in this notice, the name(s) of the above copyright   *
24  * holders shall not be used in advertising or otherwise to promote the     *
25  * sale, use or other dealings in this Software without prior written       *
26  * authorization.                                                           *
27  ****************************************************************************/
28
29 /****************************************************************************
30  *  Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995               *
31  *     and: Eric S. Raymond <esr@snark.thyrsus.com>                         *
32  *     and: Thomas E. Dickey                        1996-on                 *
33  ****************************************************************************/
34
35 /*
36  *      lib_trace.c - Tracing/Debugging routines
37  *
38  * The _tracef() function is originally from pcurses (by Pavel Curtis) in 1982. 
39  * pcurses allowed one to enable/disable tracing using traceon() and traceoff()
40  * functions.  ncurses provides a trace() function which allows one to
41  * selectively enable or disable several tracing features.
42  */
43
44 #include <curses.priv.h>
45 #include <tic.h>
46
47 #include <ctype.h>
48
49 MODULE_ID("$Id: lib_trace.c,v 1.65 2007/09/29 21:47:46 tom Exp $")
50
51 NCURSES_EXPORT_VAR(unsigned) _nc_tracing = 0; /* always define this */
52
53 #ifdef TRACE
54
55 #if USE_REENTRANT
56 NCURSES_EXPORT(const char *)
57 NCURSES_PUBLIC_VAR(_nc_tputs_trace) (void)
58 {
59     return SP ? SP->_tputs_trace : _nc_prescreen._tputs_trace;
60 }
61 NCURSES_EXPORT(long)
62 NCURSES_PUBLIC_VAR(_nc_outchars) (void)
63 {
64     return SP ? SP->_outchars : _nc_prescreen._outchars;
65 }
66 NCURSES_EXPORT(void)
67 _nc_set_tputs_trace(const char *s)
68 {
69     if (SP)
70         SP->_tputs_trace = s;
71     else
72         _nc_prescreen._tputs_trace = s;
73 }
74 NCURSES_EXPORT(void)
75 _nc_count_outchars(long increment)
76 {
77     if (SP)
78         SP->_outchars += increment;
79     else
80         _nc_prescreen._outchars += increment;
81 }
82 #else
83 NCURSES_EXPORT_VAR(const char *) _nc_tputs_trace = "";
84 NCURSES_EXPORT_VAR(long) _nc_outchars = 0;
85 #endif
86
87 #define TraceFP         _nc_globals.trace_fp
88 #define TracePath       _nc_globals.trace_fname
89 #define TraceLevel      _nc_globals.trace_level
90
91 NCURSES_EXPORT(void)
92 trace(const unsigned int tracelevel)
93 {
94     if ((TraceFP == 0) && tracelevel) {
95         const char *mode = _nc_globals.init_trace ? "ab" : "wb";
96
97         if (TracePath[0] == '\0') {
98             if (getcwd(TracePath, sizeof(TracePath) - 12) == 0) {
99                 perror("curses: Can't get working directory");
100                 exit(EXIT_FAILURE);
101             }
102             strcat(TracePath, "/trace");
103             if (_nc_is_dir_path(TracePath)) {
104                 strcat(TracePath, ".log");
105             }
106         }
107
108         _nc_globals.init_trace = TRUE;
109         _nc_tracing = tracelevel;
110         if (_nc_access(TracePath, W_OK) < 0
111             || (TraceFP = fopen(TracePath, mode)) == 0) {
112             perror("curses: Can't open 'trace' file");
113             exit(EXIT_FAILURE);
114         }
115         /* Try to set line-buffered mode, or (failing that) unbuffered,
116          * so that the trace-output gets flushed automatically at the
117          * end of each line.  This is useful in case the program dies. 
118          */
119 #if HAVE_SETVBUF                /* ANSI */
120         (void) setvbuf(TraceFP, (char *) 0, _IOLBF, 0);
121 #elif HAVE_SETBUF               /* POSIX */
122         (void) setbuffer(TraceFP, (char *) 0);
123 #endif
124         _tracef("TRACING NCURSES version %s.%d (tracelevel=%#x)",
125                 NCURSES_VERSION,
126                 NCURSES_VERSION_PATCH,
127                 tracelevel);
128     } else if (tracelevel == 0) {
129         if (TraceFP != 0) {
130             fclose(TraceFP);
131             TraceFP = 0;
132         }
133         _nc_tracing = tracelevel;
134     } else if (_nc_tracing != tracelevel) {
135         _nc_tracing = tracelevel;
136         _tracef("tracelevel=%#x", tracelevel);
137     }
138 }
139
140 static void
141 _nc_va_tracef(const char *fmt, va_list ap)
142 {
143     static const char Called[] = T_CALLED("");
144     static const char Return[] = T_RETURN("");
145
146     bool before = FALSE;
147     bool after = FALSE;
148     unsigned doit = _nc_tracing;
149     int save_err = errno;
150
151     if (strlen(fmt) >= sizeof(Called) - 1) {
152         if (!strncmp(fmt, Called, sizeof(Called) - 1)) {
153             before = TRUE;
154             TraceLevel++;
155         } else if (!strncmp(fmt, Return, sizeof(Return) - 1)) {
156             after = TRUE;
157         }
158         if (before || after) {
159             if ((TraceLevel <= 1)
160                 || (doit & TRACE_ICALLS) != 0)
161                 doit &= (TRACE_CALLS | TRACE_CCALLS);
162             else
163                 doit = 0;
164         }
165     }
166
167     if (doit != 0) {
168         if (TraceFP == 0)
169             TraceFP = stderr;
170         if (before || after) {
171             int n;
172             for (n = 1; n < TraceLevel; n++)
173                 fputs("+ ", TraceFP);
174         }
175         vfprintf(TraceFP, fmt, ap);
176         fputc('\n', TraceFP);
177         fflush(TraceFP);
178     }
179
180     if (after && TraceLevel)
181         TraceLevel--;
182
183     errno = save_err;
184 }
185
186 NCURSES_EXPORT(void)
187 _tracef(const char *fmt,...)
188 {
189     va_list ap;
190
191     va_start(ap, fmt);
192     _nc_va_tracef(fmt, ap);
193     va_end(ap);
194 }
195
196 /* Trace 'bool' return-values */
197 NCURSES_EXPORT(NCURSES_BOOL)
198 _nc_retrace_bool(NCURSES_BOOL code)
199 {
200     T((T_RETURN("%s"), code ? "TRUE" : "FALSE"));
201     return code;
202 }
203
204 /* Trace 'int' return-values */
205 NCURSES_EXPORT(int)
206 _nc_retrace_int(int code)
207 {
208     T((T_RETURN("%d"), code));
209     return code;
210 }
211
212 /* Trace 'unsigned' return-values */
213 NCURSES_EXPORT(unsigned)
214 _nc_retrace_unsigned(unsigned code)
215 {
216     T((T_RETURN("%#x"), code));
217     return code;
218 }
219
220 /* Trace 'char*' return-values */
221 NCURSES_EXPORT(char *)
222 _nc_retrace_ptr(char *code)
223 {
224     T((T_RETURN("%s"), _nc_visbuf(code)));
225     return code;
226 }
227
228 /* Trace 'const char*' return-values */
229 NCURSES_EXPORT(const char *)
230 _nc_retrace_cptr(const char *code)
231 {
232     T((T_RETURN("%s"), _nc_visbuf(code)));
233     return code;
234 }
235
236 /* Trace 'NCURSES_CONST void*' return-values */
237 NCURSES_EXPORT(NCURSES_CONST void *)
238 _nc_retrace_cvoid_ptr(NCURSES_CONST void *code)
239 {
240     T((T_RETURN("%p"), code));
241     return code;
242 }
243
244 /* Trace 'void*' return-values */
245 NCURSES_EXPORT(void *)
246 _nc_retrace_void_ptr(void *code)
247 {
248     T((T_RETURN("%p"), code));
249     return code;
250 }
251
252 /* Trace 'SCREEN *' return-values */
253 NCURSES_EXPORT(SCREEN *)
254 _nc_retrace_sp(SCREEN *code)
255 {
256     T((T_RETURN("%p"), code));
257     return code;
258 }
259
260 /* Trace 'WINDOW *' return-values */
261 NCURSES_EXPORT(WINDOW *)
262 _nc_retrace_win(WINDOW *code)
263 {
264     T((T_RETURN("%p"), code));
265     return code;
266 }
267
268 #if USE_REENTRANT
269 /*
270  * Check if the given trace-mask is enabled.
271  *
272  * This function may be called from within one of the functions that fills
273  * in parameters for _tracef(), but in that case we do not want to lock the
274  * mutex, since it is already locked.
275  */
276 NCURSES_EXPORT(int)
277 _nc_use_tracef(unsigned mask)
278 {
279     bool result = FALSE;
280
281     _nc_lock_global(tst_tracef);
282     if (!_nc_globals.nested_tracef++) {
283         if ((result = (_nc_tracing & (mask))) != 0) {
284             /* we will call _nc_locked_tracef(), no nesting so far */
285             _nc_lock_global(tracef);
286         } else {
287             /* we will not call _nc_locked_tracef() */
288             _nc_globals.nested_tracef = 0;
289         }
290     } else {
291         /* we may call _nc_locked_tracef(), but with nested_tracef > 0 */
292         result = (_nc_tracing & (mask));
293     }
294     _nc_unlock_global(tst_tracef);
295     return result;
296 }
297
298 /*
299  * We call this if _nc_use_tracef() returns true, which means we must unlock
300  * the tracef mutex.
301  */
302 NCURSES_EXPORT(void)
303 _nc_locked_tracef(const char *fmt,...)
304 {
305     va_list ap;
306
307     va_start(ap, fmt);
308     _nc_va_tracef(fmt, ap);
309     va_end(ap);
310
311     if (--(_nc_globals.nested_tracef) == 0)
312         _nc_unlock_global(tracef);
313 }
314 #endif /* USE_REENTRANT */
315
316 #endif /* TRACE */