]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/ncurses/ncurses/trace/lib_trace.c
This commit was generated by cvs2svn to compensate for changes in r161475,
[FreeBSD/FreeBSD.git] / contrib / ncurses / ncurses / trace / lib_trace.c
1 /****************************************************************************
2  * Copyright (c) 1998,1999,2000,2001 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.48 2001/10/20 20:35:25 tom Exp $")
44
45 NCURSES_EXPORT_VAR(unsigned)
46 _nc_tracing = 0;                /* always define this */
47
48 #ifdef TRACE
49 NCURSES_EXPORT_VAR(const char *)
50 _nc_tputs_trace = "";
51 NCURSES_EXPORT_VAR(long)
52 _nc_outchars = 0;
53
54      static FILE *tracefp;      /* default to writing to stderr */
55
56 NCURSES_EXPORT(void)
57 trace(const unsigned int tracelevel GCC_UNUSED)
58 {
59     static bool been_here = FALSE;
60     static char my_name[] = "trace";
61
62     if (!been_here && tracelevel) {
63         been_here = TRUE;
64
65         _nc_tracing = tracelevel;
66         if (_nc_access(my_name, W_OK) < 0
67             || (tracefp = fopen(my_name, "wb")) == 0) {
68             perror("curses: Can't open 'trace' file: ");
69             exit(EXIT_FAILURE);
70         }
71         /* Try to set line-buffered mode, or (failing that) unbuffered,
72          * so that the trace-output gets flushed automatically at the
73          * end of each line.  This is useful in case the program dies. 
74          */
75 #if HAVE_SETVBUF                /* ANSI */
76         (void) setvbuf(tracefp, (char *) 0, _IOLBF, 0);
77 #elif HAVE_SETBUF               /* POSIX */
78         (void) setbuffer(tracefp, (char *) 0);
79 #endif
80         _tracef("TRACING NCURSES version %s (tracelevel=%#x)",
81                 curses_version(), tracelevel);
82     } else if (_nc_tracing != tracelevel) {
83         _nc_tracing = tracelevel;
84         _tracef("tracelevel=%#x", tracelevel);
85     }
86 }
87
88 NCURSES_EXPORT(void)
89 _tracef(const char *fmt,...)
90 {
91     static const char Called[] = T_CALLED("");
92     static const char Return[] = T_RETURN("");
93     static int level;
94     va_list ap;
95     bool before = FALSE;
96     bool after = FALSE;
97     int doit = _nc_tracing;
98     int save_err = errno;
99
100     if (strlen(fmt) >= sizeof(Called) - 1) {
101         if (!strncmp(fmt, Called, sizeof(Called) - 1)) {
102             before = TRUE;
103             level++;
104         } else if (!strncmp(fmt, Return, sizeof(Return) - 1)) {
105             after = TRUE;
106         }
107         if (before || after) {
108             if ((level <= 1)
109                 || (doit & TRACE_ICALLS) != 0)
110                 doit &= (TRACE_CALLS | TRACE_CCALLS);
111             else
112                 doit = 0;
113         }
114     }
115
116     if (doit != 0) {
117         if (tracefp == 0)
118             tracefp = stderr;
119         if (before || after) {
120             int n;
121             for (n = 1; n < level; n++)
122                 fputs("+ ", tracefp);
123         }
124         va_start(ap, fmt);
125         vfprintf(tracefp, fmt, ap);
126         fputc('\n', tracefp);
127         va_end(ap);
128         fflush(tracefp);
129     }
130
131     if (after && level)
132         level--;
133     errno = save_err;
134 }
135
136 /* Trace 'int' return-values */
137 NCURSES_EXPORT(int)
138 _nc_retrace_int(int code)
139 {
140     T((T_RETURN("%d"), code));
141     return code;
142 }
143
144 /* Trace 'char*' return-values */
145 NCURSES_EXPORT(char *)
146 _nc_retrace_ptr(char *code)
147 {
148     T((T_RETURN("%s"), _nc_visbuf(code)));
149     return code;
150 }
151
152 /* Trace 'SCREEN *' return-values */
153 NCURSES_EXPORT(SCREEN *)
154 _nc_retrace_sp(SCREEN * code)
155 {
156     T((T_RETURN("%p"), code));
157     return code;
158 }
159
160 /* Trace 'WINDOW *' return-values */
161 NCURSES_EXPORT(WINDOW *)
162 _nc_retrace_win(WINDOW *code)
163 {
164     T((T_RETURN("%p"), code));
165     return code;
166 }
167 #endif /* TRACE */