1 /****************************************************************************
2 * Copyright (c) 1998-2004,2005 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 * and: Thomas E. Dickey 1996-on *
33 ****************************************************************************/
36 * lib_trace.c - Tracing/Debugging routines
39 #include <curses.priv.h>
44 MODULE_ID("$Id: lib_trace.c,v 1.58 2005/04/16 16:15:24 tom Exp $")
46 NCURSES_EXPORT_VAR(unsigned) _nc_tracing = 0; /* always define this */
49 NCURSES_EXPORT_VAR(const char *) _nc_tputs_trace = "";
50 NCURSES_EXPORT_VAR(long) _nc_outchars = 0;
52 static FILE *tracefp = 0; /* default to writing to stderr */
55 trace(const unsigned int tracelevel)
57 static bool been_here = FALSE;
58 static char my_name[PATH_MAX];
60 if ((tracefp == 0) && tracelevel) {
61 const char *mode = been_here ? "ab" : "wb";
63 if (*my_name == '\0') {
64 if (getcwd(my_name, sizeof(my_name) - 10) == 0) {
65 perror("curses: Can't get working directory");
68 strcat(my_name, "/trace");
72 _nc_tracing = tracelevel;
73 if (_nc_access(my_name, W_OK) < 0
74 || (tracefp = fopen(my_name, mode)) == 0) {
75 perror("curses: Can't open 'trace' file");
78 /* Try to set line-buffered mode, or (failing that) unbuffered,
79 * so that the trace-output gets flushed automatically at the
80 * end of each line. This is useful in case the program dies.
82 #if HAVE_SETVBUF /* ANSI */
83 (void) setvbuf(tracefp, (char *) 0, _IOLBF, 0);
84 #elif HAVE_SETBUF /* POSIX */
85 (void) setbuffer(tracefp, (char *) 0);
87 _tracef("TRACING NCURSES version %s.%d (tracelevel=%#x)",
89 NCURSES_VERSION_PATCH,
91 } else if (tracelevel == 0) {
96 _nc_tracing = tracelevel;
97 } else if (_nc_tracing != tracelevel) {
98 _nc_tracing = tracelevel;
99 _tracef("tracelevel=%#x", tracelevel);
104 _tracef(const char *fmt,...)
106 static const char Called[] = T_CALLED("");
107 static const char Return[] = T_RETURN("");
112 unsigned doit = _nc_tracing;
113 int save_err = errno;
115 if (strlen(fmt) >= sizeof(Called) - 1) {
116 if (!strncmp(fmt, Called, sizeof(Called) - 1)) {
119 } else if (!strncmp(fmt, Return, sizeof(Return) - 1)) {
122 if (before || after) {
124 || (doit & TRACE_ICALLS) != 0)
125 doit &= (TRACE_CALLS | TRACE_CCALLS);
134 if (before || after) {
136 for (n = 1; n < level; n++)
137 fputs("+ ", tracefp);
140 vfprintf(tracefp, fmt, ap);
141 fputc('\n', tracefp);
151 /* Trace 'bool' return-values */
152 NCURSES_EXPORT(NCURSES_BOOL)
153 _nc_retrace_bool(NCURSES_BOOL code)
155 T((T_RETURN("%s"), code ? "TRUE" : "FALSE"));
159 /* Trace 'int' return-values */
161 _nc_retrace_int(int code)
163 T((T_RETURN("%d"), code));
167 /* Trace 'unsigned' return-values */
168 NCURSES_EXPORT(unsigned)
169 _nc_retrace_unsigned(unsigned code)
171 T((T_RETURN("%#x"), code));
175 /* Trace 'char*' return-values */
176 NCURSES_EXPORT(char *)
177 _nc_retrace_ptr(char *code)
179 T((T_RETURN("%s"), _nc_visbuf(code)));
183 /* Trace 'const char*' return-values */
184 NCURSES_EXPORT(const char *)
185 _nc_retrace_cptr(const char *code)
187 T((T_RETURN("%s"), _nc_visbuf(code)));
191 /* Trace 'NCURSES_CONST void*' return-values */
192 NCURSES_EXPORT(NCURSES_CONST void *)
193 _nc_retrace_cvoid_ptr(NCURSES_CONST void *code)
195 T((T_RETURN("%p"), code));
199 /* Trace 'void*' return-values */
200 NCURSES_EXPORT(void *)
201 _nc_retrace_void_ptr(void *code)
203 T((T_RETURN("%p"), code));
207 /* Trace 'SCREEN *' return-values */
208 NCURSES_EXPORT(SCREEN *)
209 _nc_retrace_sp(SCREEN *code)
211 T((T_RETURN("%p"), code));
215 /* Trace 'WINDOW *' return-values */
216 NCURSES_EXPORT(WINDOW *)
217 _nc_retrace_win(WINDOW *code)
219 T((T_RETURN("%p"), code));