1 /****************************************************************************
2 * Copyright (c) 1998-2006,2007 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
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.
44 #include <curses.priv.h>
49 MODULE_ID("$Id: lib_trace.c,v 1.61 2007/04/21 23:06:07 tom Exp $")
51 NCURSES_EXPORT_VAR(unsigned) _nc_tracing = 0; /* always define this */
54 NCURSES_EXPORT_VAR(const char *) _nc_tputs_trace = "";
55 NCURSES_EXPORT_VAR(long) _nc_outchars = 0;
57 #define TraceFP _nc_globals.trace_fp
58 #define TracePath _nc_globals.trace_fname
59 #define TraceLevel _nc_globals.trace_level
62 trace(const unsigned int tracelevel)
64 if ((TraceFP == 0) && tracelevel) {
65 const char *mode = _nc_globals.init_trace ? "ab" : "wb";
67 if (TracePath[0] == '\0') {
68 if (getcwd(TracePath, sizeof(TracePath) - 10) == 0) {
69 perror("curses: Can't get working directory");
72 strcat(TracePath, "/trace");
75 _nc_globals.init_trace = TRUE;
76 _nc_tracing = tracelevel;
77 if (_nc_access(TracePath, W_OK) < 0
78 || (TraceFP = fopen(TracePath, mode)) == 0) {
79 perror("curses: Can't open 'trace' file");
82 /* Try to set line-buffered mode, or (failing that) unbuffered,
83 * so that the trace-output gets flushed automatically at the
84 * end of each line. This is useful in case the program dies.
86 #if HAVE_SETVBUF /* ANSI */
87 (void) setvbuf(TraceFP, (char *) 0, _IOLBF, 0);
88 #elif HAVE_SETBUF /* POSIX */
89 (void) setbuffer(TraceFP, (char *) 0);
91 _tracef("TRACING NCURSES version %s.%d (tracelevel=%#x)",
93 NCURSES_VERSION_PATCH,
95 } else if (tracelevel == 0) {
100 _nc_tracing = tracelevel;
101 } else if (_nc_tracing != tracelevel) {
102 _nc_tracing = tracelevel;
103 _tracef("tracelevel=%#x", tracelevel);
108 _tracef(const char *fmt,...)
110 static const char Called[] = T_CALLED("");
111 static const char Return[] = T_RETURN("");
115 unsigned doit = _nc_tracing;
116 int save_err = errno;
118 if (strlen(fmt) >= sizeof(Called) - 1) {
119 if (!strncmp(fmt, Called, sizeof(Called) - 1)) {
122 } else if (!strncmp(fmt, Return, sizeof(Return) - 1)) {
125 if (before || after) {
126 if ((TraceLevel <= 1)
127 || (doit & TRACE_ICALLS) != 0)
128 doit &= (TRACE_CALLS | TRACE_CCALLS);
137 if (before || after) {
139 for (n = 1; n < TraceLevel; n++)
140 fputs("+ ", TraceFP);
143 vfprintf(TraceFP, fmt, ap);
144 fputc('\n', TraceFP);
149 if (after && TraceLevel)
154 /* Trace 'bool' return-values */
155 NCURSES_EXPORT(NCURSES_BOOL)
156 _nc_retrace_bool(NCURSES_BOOL code)
158 T((T_RETURN("%s"), code ? "TRUE" : "FALSE"));
162 /* Trace 'int' return-values */
164 _nc_retrace_int(int code)
166 T((T_RETURN("%d"), code));
170 /* Trace 'unsigned' return-values */
171 NCURSES_EXPORT(unsigned)
172 _nc_retrace_unsigned(unsigned code)
174 T((T_RETURN("%#x"), code));
178 /* Trace 'char*' return-values */
179 NCURSES_EXPORT(char *)
180 _nc_retrace_ptr(char *code)
182 T((T_RETURN("%s"), _nc_visbuf(code)));
186 /* Trace 'const char*' return-values */
187 NCURSES_EXPORT(const char *)
188 _nc_retrace_cptr(const char *code)
190 T((T_RETURN("%s"), _nc_visbuf(code)));
194 /* Trace 'NCURSES_CONST void*' return-values */
195 NCURSES_EXPORT(NCURSES_CONST void *)
196 _nc_retrace_cvoid_ptr(NCURSES_CONST void *code)
198 T((T_RETURN("%p"), code));
202 /* Trace 'void*' return-values */
203 NCURSES_EXPORT(void *)
204 _nc_retrace_void_ptr(void *code)
206 T((T_RETURN("%p"), code));
210 /* Trace 'SCREEN *' return-values */
211 NCURSES_EXPORT(SCREEN *)
212 _nc_retrace_sp(SCREEN *code)
214 T((T_RETURN("%p"), code));
218 /* Trace 'WINDOW *' return-values */
219 NCURSES_EXPORT(WINDOW *)
220 _nc_retrace_win(WINDOW *code)
222 T((T_RETURN("%p"), code));