1*51ffecc1SBen Gras /* $NetBSD: ctrace.c,v 1.20 2009/01/17 15:25:36 christos Exp $ */
2*51ffecc1SBen Gras
3*51ffecc1SBen Gras /*-
4*51ffecc1SBen Gras * Copyright (c) 1992, 1993
5*51ffecc1SBen Gras * The Regents of the University of California. All rights reserved.
6*51ffecc1SBen Gras *
7*51ffecc1SBen Gras * Redistribution and use in source and binary forms, with or without
8*51ffecc1SBen Gras * modification, are permitted provided that the following conditions
9*51ffecc1SBen Gras * are met:
10*51ffecc1SBen Gras * 1. Redistributions of source code must retain the above copyright
11*51ffecc1SBen Gras * notice, this list of conditions and the following disclaimer.
12*51ffecc1SBen Gras * 2. Redistributions in binary form must reproduce the above copyright
13*51ffecc1SBen Gras * notice, this list of conditions and the following disclaimer in the
14*51ffecc1SBen Gras * documentation and/or other materials provided with the distribution.
15*51ffecc1SBen Gras * 3. Neither the name of the University nor the names of its contributors
16*51ffecc1SBen Gras * may be used to endorse or promote products derived from this software
17*51ffecc1SBen Gras * without specific prior written permission.
18*51ffecc1SBen Gras *
19*51ffecc1SBen Gras * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20*51ffecc1SBen Gras * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21*51ffecc1SBen Gras * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22*51ffecc1SBen Gras * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23*51ffecc1SBen Gras * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24*51ffecc1SBen Gras * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25*51ffecc1SBen Gras * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26*51ffecc1SBen Gras * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27*51ffecc1SBen Gras * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28*51ffecc1SBen Gras * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29*51ffecc1SBen Gras * SUCH DAMAGE.
30*51ffecc1SBen Gras */
31*51ffecc1SBen Gras
32*51ffecc1SBen Gras #include <sys/cdefs.h>
33*51ffecc1SBen Gras #ifndef lint
34*51ffecc1SBen Gras #if 0
35*51ffecc1SBen Gras static char sccsid[] = "@(#)ctrace.c 8.2 (Berkeley) 10/5/93";
36*51ffecc1SBen Gras #else
37*51ffecc1SBen Gras __RCSID("$NetBSD: ctrace.c,v 1.20 2009/01/17 15:25:36 christos Exp $");
38*51ffecc1SBen Gras #endif
39*51ffecc1SBen Gras #endif /* not lint */
40*51ffecc1SBen Gras
41*51ffecc1SBen Gras #ifdef DEBUG
42*51ffecc1SBen Gras #include <stdarg.h>
43*51ffecc1SBen Gras #include <stdio.h>
44*51ffecc1SBen Gras #include <stdlib.h>
45*51ffecc1SBen Gras
46*51ffecc1SBen Gras #include <sys/time.h>
47*51ffecc1SBen Gras #include <string.h>
48*51ffecc1SBen Gras
49*51ffecc1SBen Gras #include "curses.h"
50*51ffecc1SBen Gras #include "curses_private.h"
51*51ffecc1SBen Gras
52*51ffecc1SBen Gras static FILE *tracefp = NULL; /* Curses debugging file descriptor. */
53*51ffecc1SBen Gras
54*51ffecc1SBen Gras static int tracemask; /* Areas of trace output we want. */
55*51ffecc1SBen Gras
56*51ffecc1SBen Gras static int init_done = 0;
57*51ffecc1SBen Gras
58*51ffecc1SBen Gras void
__CTRACE_init()59*51ffecc1SBen Gras __CTRACE_init()
60*51ffecc1SBen Gras {
61*51ffecc1SBen Gras char *tf, *tm;
62*51ffecc1SBen Gras
63*51ffecc1SBen Gras tm = getenv("CURSES_TRACE_MASK");
64*51ffecc1SBen Gras if (tm == NULL)
65*51ffecc1SBen Gras tracemask = __CTRACE_ALL;
66*51ffecc1SBen Gras else {
67*51ffecc1SBen Gras tracemask = (int) strtol(tm, NULL, 0);
68*51ffecc1SBen Gras }
69*51ffecc1SBen Gras if (tracemask < 0)
70*51ffecc1SBen Gras tracemask = (0 - tracemask) ^ __CTRACE_ALL;
71*51ffecc1SBen Gras if (tracemask == 0)
72*51ffecc1SBen Gras return;
73*51ffecc1SBen Gras
74*51ffecc1SBen Gras tf = getenv("CURSES_TRACE_FILE");
75*51ffecc1SBen Gras
76*51ffecc1SBen Gras if ((tf != NULL) && !strcmp( tf, "<none>"))
77*51ffecc1SBen Gras tf = NULL;
78*51ffecc1SBen Gras
79*51ffecc1SBen Gras if (tf != NULL)
80*51ffecc1SBen Gras tracefp = fopen(tf, "w");
81*51ffecc1SBen Gras
82*51ffecc1SBen Gras init_done = 1;
83*51ffecc1SBen Gras __CTRACE(__CTRACE_ALL, "Trace mask: 0x%08x\n", tracemask);
84*51ffecc1SBen Gras }
85*51ffecc1SBen Gras
86*51ffecc1SBen Gras void
__CTRACE(int area,const char * fmt,...)87*51ffecc1SBen Gras __CTRACE(int area, const char *fmt,...)
88*51ffecc1SBen Gras {
89*51ffecc1SBen Gras struct timeval tv;
90*51ffecc1SBen Gras static int seencr = 1;
91*51ffecc1SBen Gras va_list ap;
92*51ffecc1SBen Gras
93*51ffecc1SBen Gras if (!init_done)
94*51ffecc1SBen Gras __CTRACE_init();
95*51ffecc1SBen Gras if (tracefp == NULL || !(tracemask & area)) {
96*51ffecc1SBen Gras return;
97*51ffecc1SBen Gras }
98*51ffecc1SBen Gras gettimeofday(&tv, NULL);
99*51ffecc1SBen Gras if (seencr && (tracemask & __CTRACE_TSTAMP)) {
100*51ffecc1SBen Gras gettimeofday(&tv, NULL);
101*51ffecc1SBen Gras (void) fprintf(tracefp, "%llu.%06lu: ",
102*51ffecc1SBen Gras (long long)tv.tv_sec, (long)tv.tv_usec);
103*51ffecc1SBen Gras }
104*51ffecc1SBen Gras va_start(ap, fmt);
105*51ffecc1SBen Gras (void) vfprintf(tracefp, fmt, ap);
106*51ffecc1SBen Gras seencr = (strchr(fmt, '\n') != NULL);
107*51ffecc1SBen Gras va_end(ap);
108*51ffecc1SBen Gras (void) fflush(tracefp);
109*51ffecc1SBen Gras }
110*51ffecc1SBen Gras #else
111*51ffecc1SBen Gras /* this kills the empty translation unit message from lint... */
112*51ffecc1SBen Gras void
113*51ffecc1SBen Gras __cursesi_make_lint_shut_up_if_debug_not_defined(void);
114*51ffecc1SBen Gras
115*51ffecc1SBen Gras void
__cursesi_make_lint_shut_up_if_debug_not_defined(void)116*51ffecc1SBen Gras __cursesi_make_lint_shut_up_if_debug_not_defined(void)
117*51ffecc1SBen Gras {
118*51ffecc1SBen Gras return;
119*51ffecc1SBen Gras }
120*51ffecc1SBen Gras #endif
121