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 /*
27  * trace.c
28  *
29  * XCurses Library
30  *
31  * Copyright 1990, 1995 by Mortice Kern Systems Inc.  All right reserved.
32  *
33  */
34 
35 #ifdef M_RCSID
36 #ifndef lint
37 static char rcsID[] = "$Header: /rd/src/libc/xcurses/rcs/trace.c 1.3 1995/06/12 20:24:05 ant Exp $";
38 #endif
39 #endif
40 
41 #include <private.h>
42 #include <fcntl.h>
43 #include <string.h>
44 #include <stdarg.h>
45 #include <stdlib.h>
46 
47 static int __m_tracing = FALSE;
48 
49 /*f
50  *  Write a formatted string into a trace file.
51  */
52 void
__m_trace(const char * fmt,...)53 __m_trace(const char *fmt, ...)
54 {
55 	va_list vp;
56 	static FILE *fp;
57 	static int initialized = FALSE;
58 
59 	if (!__m_tracing)
60 		return;
61 
62 	if (!initialized) {
63 		fp = fopen("trace.out", "wF");
64 		if (fp == (FILE *) 0) {
65 			fprintf(stderr, "Program cannot open \"trace.out\".\n");
66 			exit(1);
67 		}
68 		initialized = TRUE;
69 	}
70 
71 	va_start(vp, fmt);
72 	(void) vfprintf(fp, fmt, vp);
73 	va_end(vp);
74 	fputc('\n', fp);
75 }
76 
77 int
78 (__m_return_code)(const char *s, int code)
79 {
80 	switch (code) {
81 	case OK:
82 		__m_trace("%s returned OK.", s);
83 		break;
84 	case ERR:
85 		__m_trace("%s returned ERR.", s);
86 		break;
87 	case KEY_CODE_YES:
88 		__m_trace("%s returned KEY_CODE_YES.", s);
89 		break;
90 	default:
91 		__m_trace("%s returned code %d", s, code);
92 	}
93 
94 	return code;
95 }
96 
97 int
98 (__m_return_int)(const char *s, int value)
99 {
100 	__m_trace("%s returned %d", s, value);
101 
102 	return value;
103 }
104 
chtype(__m_return_chtype)105 chtype
106 (__m_return_chtype)(const char *s, chtype ch)
107 {
108 	__m_trace("%s returned %lx", s, ch);
109 
110 	return ch;
111 }
112 
113 void *
114 (__m_return_pointer)(const char *s, const void *ptr)
115 {
116 	if (ptr == (void *) 0)
117 		__m_trace("%s returned NULL.", s);
118 	else
119 		__m_trace("%s returned %p.", s, ptr);
120 
121 	return (void *) ptr;
122 }
123 
124 #undef __m_return_void
125 
126 void
__m_return_void(const char * s)127 __m_return_void(const char *s)
128 {
129 	__m_trace("%s returns void.");
130 }
131 
132 /*f
133  *  Turn tracing on
134  */
135 void
traceon()136 traceon()
137 {
138     	__m_tracing = TRUE;
139 	__m_trace("traceon()\ntraceon() returns void.");
140 }
141 
142 /*f
143  *  Turn tracing off
144  */
145 void
traceoff()146 traceoff()
147 {
148 	__m_trace("traceoff()\ntraceoff() returns void.");
149     	__m_tracing = FALSE;
150 }
151 
152