]> ncurses.scripts.mit.edu Git - ncurses.git/blob - ncurses/trace/lib_trace.c
ncurses 5.6 - patch 20070421
[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.61 2007/04/21 23:06:07 tom Exp $")
50
51 NCURSES_EXPORT_VAR(unsigned) _nc_tracing = 0; /* always define this */
52
53 #ifdef TRACE
54 NCURSES_EXPORT_VAR(const char *) _nc_tputs_trace = "";
55 NCURSES_EXPORT_VAR(long) _nc_outchars = 0;
56
57 #define TraceFP         _nc_globals.trace_fp
58 #define TracePath       _nc_globals.trace_fname
59 #define TraceLevel      _nc_globals.trace_level
60
61 NCURSES_EXPORT(void)
62 trace(const unsigned int tracelevel)
63 {
64     if ((TraceFP == 0) && tracelevel) {
65         const char *mode = _nc_globals.init_trace ? "ab" : "wb";
66
67         if (TracePath[0] == '\0') {
68             if (getcwd(TracePath, sizeof(TracePath) - 10) == 0) {
69                 perror("curses: Can't get working directory");
70                 exit(EXIT_FAILURE);
71             }
72             strcat(TracePath, "/trace");
73         }
74
75         _nc_globals.init_trace = TRUE;
76         _nc_tracing = tracelevel;
77         if (_nc_access(TracePath, W_OK) < 0
78             || (TraceFP = fopen(TracePath, mode)) == 0) {
79             perror("curses: Can't open 'trace' file");
80             exit(EXIT_FAILURE);
81         }
82         /* Try to set line-buffered mode, or (failing that) unbuffered,
83          * so that the trace-output gets flushed automatically at the
84          * end of each line.  This is useful in case the program dies. 
85          */
86 #if HAVE_SETVBUF                /* ANSI */
87         (void) setvbuf(TraceFP, (char *) 0, _IOLBF, 0);
88 #elif HAVE_SETBUF               /* POSIX */
89         (void) setbuffer(TraceFP, (char *) 0);
90 #endif
91         _tracef("TRACING NCURSES version %s.%d (tracelevel=%#x)",
92                 NCURSES_VERSION,
93                 NCURSES_VERSION_PATCH,
94                 tracelevel);
95     } else if (tracelevel == 0) {
96         if (TraceFP != 0) {
97             fclose(TraceFP);
98             TraceFP = 0;
99         }
100         _nc_tracing = tracelevel;
101     } else if (_nc_tracing != tracelevel) {
102         _nc_tracing = tracelevel;
103         _tracef("tracelevel=%#x", tracelevel);
104     }
105 }
106
107 NCURSES_EXPORT(void)
108 _tracef(const char *fmt,...)
109 {
110     static const char Called[] = T_CALLED("");
111     static const char Return[] = T_RETURN("");
112     va_list ap;
113     bool before = FALSE;
114     bool after = FALSE;
115     unsigned doit = _nc_tracing;
116     int save_err = errno;
117
118     if (strlen(fmt) >= sizeof(Called) - 1) {
119         if (!strncmp(fmt, Called, sizeof(Called) - 1)) {
120             before = TRUE;
121             TraceLevel++;
122         } else if (!strncmp(fmt, Return, sizeof(Return) - 1)) {
123             after = TRUE;
124         }
125         if (before || after) {
126             if ((TraceLevel <= 1)
127                 || (doit & TRACE_ICALLS) != 0)
128                 doit &= (TRACE_CALLS | TRACE_CCALLS);
129             else
130                 doit = 0;
131         }
132     }
133
134     if (doit != 0) {
135         if (TraceFP == 0)
136             TraceFP = stderr;
137         if (before || after) {
138             int n;
139             for (n = 1; n < TraceLevel; n++)
140                 fputs("+ ", TraceFP);
141         }
142         va_start(ap, fmt);
143         vfprintf(TraceFP, fmt, ap);
144         fputc('\n', TraceFP);
145         va_end(ap);
146         fflush(TraceFP);
147     }
148
149     if (after && TraceLevel)
150         TraceLevel--;
151     errno = save_err;
152 }
153
154 /* Trace 'bool' return-values */
155 NCURSES_EXPORT(NCURSES_BOOL)
156 _nc_retrace_bool(NCURSES_BOOL code)
157 {
158     T((T_RETURN("%s"), code ? "TRUE" : "FALSE"));
159     return code;
160 }
161
162 /* Trace 'int' return-values */
163 NCURSES_EXPORT(int)
164 _nc_retrace_int(int code)
165 {
166     T((T_RETURN("%d"), code));
167     return code;
168 }
169
170 /* Trace 'unsigned' return-values */
171 NCURSES_EXPORT(unsigned)
172 _nc_retrace_unsigned(unsigned code)
173 {
174     T((T_RETURN("%#x"), code));
175     return code;
176 }
177
178 /* Trace 'char*' return-values */
179 NCURSES_EXPORT(char *)
180 _nc_retrace_ptr(char *code)
181 {
182     T((T_RETURN("%s"), _nc_visbuf(code)));
183     return code;
184 }
185
186 /* Trace 'const char*' return-values */
187 NCURSES_EXPORT(const char *)
188 _nc_retrace_cptr(const char *code)
189 {
190     T((T_RETURN("%s"), _nc_visbuf(code)));
191     return code;
192 }
193
194 /* Trace 'NCURSES_CONST void*' return-values */
195 NCURSES_EXPORT(NCURSES_CONST void *)
196 _nc_retrace_cvoid_ptr(NCURSES_CONST void *code)
197 {
198     T((T_RETURN("%p"), code));
199     return code;
200 }
201
202 /* Trace 'void*' return-values */
203 NCURSES_EXPORT(void *)
204 _nc_retrace_void_ptr(void *code)
205 {
206     T((T_RETURN("%p"), code));
207     return code;
208 }
209
210 /* Trace 'SCREEN *' return-values */
211 NCURSES_EXPORT(SCREEN *)
212 _nc_retrace_sp(SCREEN *code)
213 {
214     T((T_RETURN("%p"), code));
215     return code;
216 }
217
218 /* Trace 'WINDOW *' return-values */
219 NCURSES_EXPORT(WINDOW *)
220 _nc_retrace_win(WINDOW *code)
221 {
222     T((T_RETURN("%p"), code));
223     return code;
224 }
225 #endif /* TRACE */