1 /****************************************************************************
2 * Copyright (c) 1998 Free Software Foundation, Inc. *
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: *
12 * The above copyright notice and this permission notice shall be included *
13 * in all copies or substantial portions of the Software. *
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. *
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 *
27 ****************************************************************************/
29 /****************************************************************************
30 * Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995 *
31 * and: Eric S. Raymond <esr@snark.thyrsus.com> *
32 ****************************************************************************/
35 * lib_trace.c - Tracing/Debugging routines
39 #define TRACE /* turn on internal defs for this module */
42 #include <curses.priv.h>
44 MODULE_ID("$Id: lib_trace.c,v 1.25 1998/02/11 12:13:55 tom Exp $")
51 unsigned _nc_tracing = 0;
52 const char *_nc_tputs_trace = "";
54 int _nc_optimize_enable = OPTIMIZE_ALL;
56 static FILE * tracefp; /* default to writing to stderr */
58 void trace(const unsigned int tracelevel)
60 static bool been_here = FALSE;
62 _nc_tracing = tracelevel;
63 if (! been_here && tracelevel) {
66 if ((tracefp = fopen("trace", "w")) == 0) {
67 perror("curses: Can't open 'trace' file: ");
70 /* Try to set line-buffered mode, or (failing that) unbuffered,
71 * so that the trace-output gets flushed automatically at the
72 * end of each line. This is useful in case the program dies.
74 #if HAVE_SETVBUF /* ANSI */
75 (void) setvbuf(tracefp, (char *)0, _IOLBF, 0);
76 #elif HAVE_SETBUF /* POSIX */
77 (void) setbuffer(tracefp, (char *)0);
79 _tracef("TRACING NCURSES version %s (%d)",
80 NCURSES_VERSION, NCURSES_VERSION_PATCH);
84 const char *_nc_visbuf2(int bufnum, const char *buf)
85 /* visibilize a given string */
94 tp = vbuf = _nc_trace_buf(bufnum, (strlen(buf) * 4) + 5);
96 while ((c = *buf++) != '\0') {
98 *tp++ = '\\'; *tp++ = '"';
99 } else if (is7bits(c) && (isgraph(c) || c == ' ')) {
101 } else if (c == '\n') {
102 *tp++ = '\\'; *tp++ = 'n';
103 } else if (c == '\r') {
104 *tp++ = '\\'; *tp++ = 'r';
105 } else if (c == '\b') {
106 *tp++ = '\\'; *tp++ = 'b';
107 } else if (c == '\033') {
108 *tp++ = '\\'; *tp++ = 'e';
109 } else if (is7bits(c) && iscntrl(c)) {
110 *tp++ = '\\'; *tp++ = '^'; *tp++ = '@' + c;
112 sprintf(tp, "\\%03o", c & 0xff);
121 const char *_nc_visbuf(const char *buf)
123 return _nc_visbuf2(0, buf);
127 _tracef(const char *fmt, ...)
129 static const char Called[] = T_CALLED("");
130 static const char Return[] = T_RETURN("");
135 int doit = _nc_tracing;
136 int save_err = errno;
138 if (strlen(fmt) >= sizeof(Called) - 1) {
139 if (!strncmp(fmt, Called, sizeof(Called)-1)) {
142 } else if (!strncmp(fmt, Return, sizeof(Return)-1)) {
145 if (before || after) {
147 || (doit & TRACE_ICALLS) != 0)
148 doit &= (TRACE_CALLS|TRACE_CCALLS);
157 if (before || after) {
159 for (n = 1; n < level; n++)
160 fputs("+ ", tracefp);
163 vfprintf(tracefp, fmt, ap);
164 fputc('\n', tracefp);
174 /* Trace 'int' return-values */
175 int _nc_retrace_int(int code)
177 T((T_RETURN("%d"), code));
181 /* Trace 'char*' return-values */
182 char * _nc_retrace_ptr(char * code)
184 T((T_RETURN("%s"), _nc_visbuf(code)));
188 /* Trace 'WINDOW *' return-values */
189 WINDOW *_nc_retrace_win(WINDOW *code)
191 T((T_RETURN("%p"), code));