2 /***************************************************************************
4 ****************************************************************************
5 * ncurses is copyright (C) 1992-1995 *
7 * zmbenhal@netcom.com *
9 * esr@snark.thyrsus.com *
11 * Permission is hereby granted to reproduce and distribute ncurses *
12 * by any means and for any fee, whether alone or as part of a *
13 * larger distribution, in source or in binary form, PROVIDED *
14 * this notice is included with any such distribution, and is not *
15 * removed from any of its header files. Mention of ncurses in any *
16 * applications linked with it is highly appreciated. *
18 * ncurses comes AS IS with no warranty, implied or expressed. *
20 ***************************************************************************/
23 * lib_trace.c - Tracing/Debugging routines
27 #define TRACE /* turn on internal defs for this module */
30 #include <curses.priv.h>
32 MODULE_ID("$Id: lib_trace.c,v 1.23 1997/05/02 00:13:07 tom Exp $")
39 unsigned _nc_tracing = 0;
40 const char *_nc_tputs_trace = "";
42 int _nc_optimize_enable = OPTIMIZE_ALL;
44 static FILE * tracefp; /* default to writing to stderr */
46 void trace(const unsigned int tracelevel)
48 static bool been_here = FALSE;
50 _nc_tracing = tracelevel;
51 if (! been_here && tracelevel) {
54 if ((tracefp = fopen("trace", "w")) == 0) {
55 perror("curses: Can't open 'trace' file: ");
58 /* Try to set line-buffered mode, or (failing that) unbuffered,
59 * so that the trace-output gets flushed automatically at the
60 * end of each line. This is useful in case the program dies.
62 #if HAVE_SETVBUF /* ANSI */
63 (void) setvbuf(tracefp, (char *)0, _IOLBF, 0);
64 #elif HAVE_SETBUF /* POSIX */
65 (void) setbuffer(tracefp, (char *)0);
67 _tracef("TRACING NCURSES version %s (%d)",
68 NCURSES_VERSION, NCURSES_VERSION_PATCH);
72 const char *_nc_visbuf2(int bufnum, const char *buf)
73 /* visibilize a given string */
82 tp = vbuf = _nc_trace_buf(bufnum, (strlen(buf) * 4) + 5);
84 while ((c = *buf++) != '\0') {
86 *tp++ = '\\'; *tp++ = '"';
87 } else if (is7bits(c) && (isgraph(c) || c == ' ')) {
89 } else if (c == '\n') {
90 *tp++ = '\\'; *tp++ = 'n';
91 } else if (c == '\r') {
92 *tp++ = '\\'; *tp++ = 'r';
93 } else if (c == '\b') {
94 *tp++ = '\\'; *tp++ = 'b';
95 } else if (c == '\033') {
96 *tp++ = '\\'; *tp++ = 'e';
97 } else if (is7bits(c) && iscntrl(c)) {
98 *tp++ = '\\'; *tp++ = '^'; *tp++ = '@' + c;
100 sprintf(tp, "\\%03o", c & 0xff);
109 const char *_nc_visbuf(const char *buf)
111 return _nc_visbuf2(0, buf);
115 _tracef(const char *fmt, ...)
117 static const char Called[] = T_CALLED("");
118 static const char Return[] = T_RETURN("");
123 int doit = _nc_tracing;
125 if (strlen(fmt) >= sizeof(Called) - 1) {
126 if (!strncmp(fmt, Called, sizeof(Called)-1)) {
129 } else if (!strncmp(fmt, Return, sizeof(Return)-1)) {
132 if (before || after) {
134 || (doit & TRACE_ICALLS) != 0)
135 doit &= (TRACE_CALLS|TRACE_CCALLS);
144 if (before || after) {
146 for (n = 1; n < level; n++)
147 fputs("+ ", tracefp);
150 vfprintf(tracefp, fmt, ap);
151 fputc('\n', tracefp);
160 /* Trace 'int' return-values */
161 int _nc_retrace_int(int code)
163 T((T_RETURN("%d"), code));
167 /* Trace 'char*' return-values */
168 char * _nc_retrace_ptr(char * code)
170 T((T_RETURN("%s"), _nc_visbuf(code)));
174 /* Trace 'WINDOW *' return-values */
175 WINDOW *_nc_retrace_win(WINDOW *code)
177 T((T_RETURN("%p"), code));