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