]> ncurses.scripts.mit.edu Git - ncurses.git/blob - ncurses/trace/lib_trace.c
8a5d7e6a650a04c4b9363849103f206bd00c3ddc
[ncurses.git] / ncurses / trace / lib_trace.c
1 /****************************************************************************
2  * Copyright (c) 1998-2002,2003 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  ****************************************************************************/
33
34 /*
35  *      lib_trace.c - Tracing/Debugging routines
36  */
37
38 #include <curses.priv.h>
39 #include <tic.h>
40
41 #include <ctype.h>
42
43 MODULE_ID("$Id: lib_trace.c,v 1.53 2003/11/23 00:39:30 tom Exp $")
44
45 NCURSES_EXPORT_VAR(unsigned) _nc_tracing = 0;   /* always define this */
46
47 #ifdef TRACE
48 NCURSES_EXPORT_VAR(const char *) _nc_tputs_trace = "";
49 NCURSES_EXPORT_VAR(long) _nc_outchars = 0;
50
51 static FILE *tracefp = 0;       /* default to writing to stderr */
52
53 NCURSES_EXPORT(void)
54 trace(const unsigned int tracelevel)
55 {
56     static bool been_here = FALSE;
57     static char my_name[PATH_MAX];
58
59     if ((tracefp == 0) && tracelevel) {
60         const char *mode = been_here ? "ab" : "wb";
61
62         if (*my_name == '\0') {
63             if (getcwd(my_name, sizeof(my_name) - 10) == 0) {
64                 perror("curses: Can't get working directory");
65                 exit(EXIT_FAILURE);
66             }
67             strcat(my_name, "/trace");
68         }
69
70         been_here = TRUE;
71         _nc_tracing = tracelevel;
72         if (_nc_access(my_name, W_OK) < 0
73             || (tracefp = fopen(my_name, mode)) == 0) {
74             perror("curses: Can't open 'trace' file");
75             exit(EXIT_FAILURE);
76         }
77         /* Try to set line-buffered mode, or (failing that) unbuffered,
78          * so that the trace-output gets flushed automatically at the
79          * end of each line.  This is useful in case the program dies. 
80          */
81 #if HAVE_SETVBUF                /* ANSI */
82         (void) setvbuf(tracefp, (char *) 0, _IOLBF, 0);
83 #elif HAVE_SETBUF               /* POSIX */
84         (void) setbuffer(tracefp, (char *) 0);
85 #endif
86         _tracef("TRACING NCURSES version %s.%d (tracelevel=%#x)",
87                 NCURSES_VERSION,
88                 NCURSES_VERSION_PATCH,
89                 tracelevel);
90     } else if (tracelevel == 0) {
91         if (tracefp != 0) {
92             fclose(tracefp);
93             tracefp = 0;
94         }
95         _nc_tracing = tracelevel;
96     } else if (_nc_tracing != tracelevel) {
97         _nc_tracing = tracelevel;
98         _tracef("tracelevel=%#x", tracelevel);
99     }
100 }
101
102 NCURSES_EXPORT(void)
103 _tracef(const char *fmt,...)
104 {
105     static const char Called[] = T_CALLED("");
106     static const char Return[] = T_RETURN("");
107     static int level;
108     va_list ap;
109     bool before = FALSE;
110     bool after = FALSE;
111     int doit = _nc_tracing;
112     int save_err = errno;
113
114     if (strlen(fmt) >= sizeof(Called) - 1) {
115         if (!strncmp(fmt, Called, sizeof(Called) - 1)) {
116             before = TRUE;
117             level++;
118         } else if (!strncmp(fmt, Return, sizeof(Return) - 1)) {
119             after = TRUE;
120         }
121         if (before || after) {
122             if ((level <= 1)
123                 || (doit & TRACE_ICALLS) != 0)
124                 doit &= (TRACE_CALLS | TRACE_CCALLS);
125             else
126                 doit = 0;
127         }
128     }
129
130     if (doit != 0) {
131         if (tracefp == 0)
132             tracefp = stderr;
133         if (before || after) {
134             int n;
135             for (n = 1; n < level; n++)
136                 fputs("+ ", tracefp);
137         }
138         va_start(ap, fmt);
139         vfprintf(tracefp, fmt, ap);
140         fputc('\n', tracefp);
141         va_end(ap);
142         fflush(tracefp);
143     }
144
145     if (after && level)
146         level--;
147     errno = save_err;
148 }
149
150 /* Trace 'bool' return-values */
151 NCURSES_EXPORT(NCURSES_BOOL)
152 _nc_retrace_bool(NCURSES_BOOL code)
153 {
154     T((T_RETURN("%s"), code ? "TRUE" : "FALSE"));
155     return code;
156 }
157
158 /* Trace 'int' return-values */
159 NCURSES_EXPORT(int)
160 _nc_retrace_int(int code)
161 {
162     T((T_RETURN("%d"), code));
163     return code;
164 }
165
166 /* Trace 'unsigned' return-values */
167 NCURSES_EXPORT(unsigned)
168 _nc_retrace_unsigned(unsigned code)
169 {
170     T((T_RETURN("%#x"), code));
171     return code;
172 }
173
174 /* Trace 'char*' return-values */
175 NCURSES_EXPORT(char *)
176 _nc_retrace_ptr(char *code)
177 {
178     T((T_RETURN("%s"), _nc_visbuf(code)));
179     return code;
180 }
181
182 /* Trace 'SCREEN *' return-values */
183 NCURSES_EXPORT(SCREEN *)
184 _nc_retrace_sp(SCREEN * code)
185 {
186     T((T_RETURN("%p"), code));
187     return code;
188 }
189
190 /* Trace 'WINDOW *' return-values */
191 NCURSES_EXPORT(WINDOW *)
192 _nc_retrace_win(WINDOW *code)
193 {
194     T((T_RETURN("%p"), code));
195     return code;
196 }
197 #endif /* TRACE */