xref: /titanic_52/usr/src/lib/libxcurses/src/libc/xcurses/trace.c (revision 004388ebfdfe2ed7dfd2d153a876dfcc22d2c006)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 /*
29  * trace.c
30  *
31  * XCurses Library
32  *
33  * Copyright 1990, 1995 by Mortice Kern Systems Inc.  All right reserved.
34  *
35  */
36 
37 #ifdef M_RCSID
38 #ifndef lint
39 static char rcsID[] = "$Header: /rd/src/libc/xcurses/rcs/trace.c 1.3 1995/06/12 20:24:05 ant Exp $";
40 #endif
41 #endif
42 
43 #include <private.h>
44 #include <fcntl.h>
45 #include <string.h>
46 #include <stdarg.h>
47 #include <stdlib.h>
48 
49 static int __m_tracing = FALSE;
50 
51 /*f
52  *  Write a formatted string into a trace file.
53  */
54 void
55 __m_trace(const char *fmt, ...)
56 {
57 	va_list vp;
58 	static FILE *fp;
59 	static int initialized = FALSE;
60 
61 	if (!__m_tracing)
62 		return;
63 
64 	if (!initialized) {
65 		fp = fopen("trace.out", "wF");
66 		if (fp == (FILE *) 0) {
67 			fprintf(stderr, "Program cannot open \"trace.out\".\n");
68 			exit(1);
69 		}
70 		initialized = TRUE;
71 	}
72 
73 	va_start(vp, fmt);
74 	(void) vfprintf(fp, fmt, vp);
75 	va_end(vp);
76 	fputc('\n', fp);
77 }
78 
79 int
80 (__m_return_code)(const char *s, int code)
81 {
82 	switch (code) {
83 	case OK:
84 		__m_trace("%s returned OK.", s);
85 		break;
86 	case ERR:
87 		__m_trace("%s returned ERR.", s);
88 		break;
89 	case KEY_CODE_YES:
90 		__m_trace("%s returned KEY_CODE_YES.", s);
91 		break;
92 	default:
93 		__m_trace("%s returned code %d", s, code);
94 	}
95 
96 	return code;
97 }
98 
99 int
100 (__m_return_int)(const char *s, int value)
101 {
102 	__m_trace("%s returned %d", s, value);
103 
104 	return value;
105 }
106 
107 chtype
108 (__m_return_chtype)(const char *s, chtype ch)
109 {
110 	__m_trace("%s returned %lx", s, ch);
111 
112 	return ch;
113 }
114 
115 void *
116 (__m_return_pointer)(const char *s, const void *ptr)
117 {
118 	if (ptr == (void *) 0)
119 		__m_trace("%s returned NULL.", s);
120 	else
121 		__m_trace("%s returned %p.", s, ptr);
122 
123 	return (void *) ptr;
124 }
125 
126 #undef __m_return_void
127 
128 void
129 __m_return_void(const char *s)
130 {
131 	__m_trace("%s returns void.");
132 }
133 
134 /*f
135  *  Turn tracing on
136  */
137 void
138 traceon()
139 {
140     	__m_tracing = TRUE;
141 	__m_trace("traceon()\ntraceon() returns void.");
142 }
143 
144 /*f
145  *  Turn tracing off
146  */
147 void
148 traceoff()
149 {
150 	__m_trace("traceoff()\ntraceoff() returns void.");
151     	__m_tracing = FALSE;
152 }
153 
154