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