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