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.62 2007/07/14 19:32:54 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) - 12) == 0) {
69 perror("curses: Can't get working directory");
72 strcat(TracePath, "/trace");
73 if (_nc_is_dir_path(TracePath)) {
74 strcat(TracePath, ".log");
78 _nc_globals.init_trace = TRUE;
79 _nc_tracing = tracelevel;
80 if (_nc_access(TracePath, W_OK) < 0
81 || (TraceFP = fopen(TracePath, mode)) == 0) {
82 perror("curses: Can't open 'trace' file");
85 /* Try to set line-buffered mode, or (failing that) unbuffered,
86 * so that the trace-output gets flushed automatically at the
87 * end of each line. This is useful in case the program dies.
89 #if HAVE_SETVBUF /* ANSI */
90 (void) setvbuf(TraceFP, (char *) 0, _IOLBF, 0);
91 #elif HAVE_SETBUF /* POSIX */
92 (void) setbuffer(TraceFP, (char *) 0);
94 _tracef("TRACING NCURSES version %s.%d (tracelevel=%#x)",
96 NCURSES_VERSION_PATCH,
98 } else if (tracelevel == 0) {
103 _nc_tracing = tracelevel;
104 } else if (_nc_tracing != tracelevel) {
105 _nc_tracing = tracelevel;
106 _tracef("tracelevel=%#x", tracelevel);
111 _tracef(const char *fmt,...)
113 static const char Called[] = T_CALLED("");
114 static const char Return[] = T_RETURN("");
118 unsigned doit = _nc_tracing;
119 int save_err = errno;
121 if (strlen(fmt) >= sizeof(Called) - 1) {
122 if (!strncmp(fmt, Called, sizeof(Called) - 1)) {
125 } else if (!strncmp(fmt, Return, sizeof(Return) - 1)) {
128 if (before || after) {
129 if ((TraceLevel <= 1)
130 || (doit & TRACE_ICALLS) != 0)
131 doit &= (TRACE_CALLS | TRACE_CCALLS);
140 if (before || after) {
142 for (n = 1; n < TraceLevel; n++)
143 fputs("+ ", TraceFP);
146 vfprintf(TraceFP, fmt, ap);
147 fputc('\n', TraceFP);
152 if (after && TraceLevel)
157 /* Trace 'bool' return-values */
158 NCURSES_EXPORT(NCURSES_BOOL)
159 _nc_retrace_bool(NCURSES_BOOL code)
161 T((T_RETURN("%s"), code ? "TRUE" : "FALSE"));
165 /* Trace 'int' return-values */
167 _nc_retrace_int(int code)
169 T((T_RETURN("%d"), code));
173 /* Trace 'unsigned' return-values */
174 NCURSES_EXPORT(unsigned)
175 _nc_retrace_unsigned(unsigned code)
177 T((T_RETURN("%#x"), code));
181 /* Trace 'char*' return-values */
182 NCURSES_EXPORT(char *)
183 _nc_retrace_ptr(char *code)
185 T((T_RETURN("%s"), _nc_visbuf(code)));
189 /* Trace 'const char*' return-values */
190 NCURSES_EXPORT(const char *)
191 _nc_retrace_cptr(const char *code)
193 T((T_RETURN("%s"), _nc_visbuf(code)));
197 /* Trace 'NCURSES_CONST void*' return-values */
198 NCURSES_EXPORT(NCURSES_CONST void *)
199 _nc_retrace_cvoid_ptr(NCURSES_CONST void *code)
201 T((T_RETURN("%p"), code));
205 /* Trace 'void*' return-values */
206 NCURSES_EXPORT(void *)
207 _nc_retrace_void_ptr(void *code)
209 T((T_RETURN("%p"), code));
213 /* Trace 'SCREEN *' return-values */
214 NCURSES_EXPORT(SCREEN *)
215 _nc_retrace_sp(SCREEN *code)
217 T((T_RETURN("%p"), code));
221 /* Trace 'WINDOW *' return-values */
222 NCURSES_EXPORT(WINDOW *)
223 _nc_retrace_win(WINDOW *code)
225 T((T_RETURN("%p"), code));