xref: /titanic_50/usr/src/lib/efcode/engine/log.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate /*
30*7c478bd9Sstevel@tonic-gate  * Daemon log message.  This can direct log messages to either stdout,
31*7c478bd9Sstevel@tonic-gate  * an error log file or syslog (or any combination).
32*7c478bd9Sstevel@tonic-gate  */
33*7c478bd9Sstevel@tonic-gate 
34*7c478bd9Sstevel@tonic-gate #include <stdarg.h>
35*7c478bd9Sstevel@tonic-gate #include <stdio.h>
36*7c478bd9Sstevel@tonic-gate #include <syslog.h>
37*7c478bd9Sstevel@tonic-gate #include <errno.h>
38*7c478bd9Sstevel@tonic-gate #include <string.h>
39*7c478bd9Sstevel@tonic-gate #include <unistd.h>
40*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
41*7c478bd9Sstevel@tonic-gate 
42*7c478bd9Sstevel@tonic-gate #include <fcode/private.h>
43*7c478bd9Sstevel@tonic-gate #include <fcode/log.h>
44*7c478bd9Sstevel@tonic-gate 
45*7c478bd9Sstevel@tonic-gate #define	LOG_LINESIZE		256
46*7c478bd9Sstevel@tonic-gate #define	LOG_EMIT_BUFSIZE	LOG_LINESIZE
47*7c478bd9Sstevel@tonic-gate 
48*7c478bd9Sstevel@tonic-gate static FILE *error_log_fp = NULL;
49*7c478bd9Sstevel@tonic-gate static int syslog_opened = 0;
50*7c478bd9Sstevel@tonic-gate static int error_log_flags;
51*7c478bd9Sstevel@tonic-gate static int syslog_log_flags;
52*7c478bd9Sstevel@tonic-gate static int do_emit_flag = -1;
53*7c478bd9Sstevel@tonic-gate static int daemon_log_flag;
54*7c478bd9Sstevel@tonic-gate static int min_syslog_level = LOG_ERR;
55*7c478bd9Sstevel@tonic-gate 
56*7c478bd9Sstevel@tonic-gate static int log_to_stdout(int);
57*7c478bd9Sstevel@tonic-gate static int log_to_error_log(int);
58*7c478bd9Sstevel@tonic-gate static int log_to_syslog(int);
59*7c478bd9Sstevel@tonic-gate static int msg_level_to_syslog(int);
60*7c478bd9Sstevel@tonic-gate 
61*7c478bd9Sstevel@tonic-gate /*
62*7c478bd9Sstevel@tonic-gate  * Complicated by not wanting to do any emit processing if no one's actually
63*7c478bd9Sstevel@tonic-gate  * going to see it.
64*7c478bd9Sstevel@tonic-gate  */
65*7c478bd9Sstevel@tonic-gate void
log_emit(char c)66*7c478bd9Sstevel@tonic-gate log_emit(char c)
67*7c478bd9Sstevel@tonic-gate {
68*7c478bd9Sstevel@tonic-gate 	static char emit_buf[LOG_EMIT_BUFSIZE];
69*7c478bd9Sstevel@tonic-gate 	static char *emit_p = emit_buf;
70*7c478bd9Sstevel@tonic-gate 	static int lastnl = 1;
71*7c478bd9Sstevel@tonic-gate 
72*7c478bd9Sstevel@tonic-gate 	/*
73*7c478bd9Sstevel@tonic-gate 	 * No one has set the do_emit_flag, go ahead and figure it out.
74*7c478bd9Sstevel@tonic-gate 	 */
75*7c478bd9Sstevel@tonic-gate 	if (do_emit_flag < 0) {
76*7c478bd9Sstevel@tonic-gate 		do_emit_flag = (log_to_stdout(MSG_EMIT) |
77*7c478bd9Sstevel@tonic-gate 		    log_to_error_log(MSG_EMIT) | log_to_syslog(MSG_EMIT));
78*7c478bd9Sstevel@tonic-gate 	}
79*7c478bd9Sstevel@tonic-gate 
80*7c478bd9Sstevel@tonic-gate 	if (!do_emit_flag)
81*7c478bd9Sstevel@tonic-gate 		return;
82*7c478bd9Sstevel@tonic-gate 
83*7c478bd9Sstevel@tonic-gate 	/*
84*7c478bd9Sstevel@tonic-gate 	 * Check for buffer overflow.
85*7c478bd9Sstevel@tonic-gate 	 */
86*7c478bd9Sstevel@tonic-gate 	if (emit_p >= &emit_buf[LOG_EMIT_BUFSIZE - 1]) {
87*7c478bd9Sstevel@tonic-gate 		*emit_p = '\0';
88*7c478bd9Sstevel@tonic-gate 		log_message(MSG_EMIT, "emit: %s\n", emit_buf);
89*7c478bd9Sstevel@tonic-gate 		emit_p = emit_buf;
90*7c478bd9Sstevel@tonic-gate 		lastnl = 1;
91*7c478bd9Sstevel@tonic-gate 	}
92*7c478bd9Sstevel@tonic-gate 
93*7c478bd9Sstevel@tonic-gate 	/*
94*7c478bd9Sstevel@tonic-gate 	 * Fcode emit's may output both CR/LF, we go ahead and eat multiple
95*7c478bd9Sstevel@tonic-gate 	 * ones in succession.
96*7c478bd9Sstevel@tonic-gate 	 */
97*7c478bd9Sstevel@tonic-gate 	if (c == '\n' || c == '\r') {
98*7c478bd9Sstevel@tonic-gate 		if (!lastnl) {
99*7c478bd9Sstevel@tonic-gate 			*emit_p = '\0';
100*7c478bd9Sstevel@tonic-gate 			log_message(MSG_EMIT, "emit: %s\n", emit_buf);
101*7c478bd9Sstevel@tonic-gate 			emit_p = emit_buf;
102*7c478bd9Sstevel@tonic-gate 		}
103*7c478bd9Sstevel@tonic-gate 		lastnl = 1;
104*7c478bd9Sstevel@tonic-gate 	} else {
105*7c478bd9Sstevel@tonic-gate 		lastnl = 0;
106*7c478bd9Sstevel@tonic-gate 		*emit_p++ = c;
107*7c478bd9Sstevel@tonic-gate 	}
108*7c478bd9Sstevel@tonic-gate }
109*7c478bd9Sstevel@tonic-gate 
110*7c478bd9Sstevel@tonic-gate /*
111*7c478bd9Sstevel@tonic-gate  * If stdout is a tty and this is MSG_EMIT, we should have alredy output it.
112*7c478bd9Sstevel@tonic-gate  * If running as daemon, output to stdout is a waste of time.
113*7c478bd9Sstevel@tonic-gate  */
114*7c478bd9Sstevel@tonic-gate static int
log_to_stdout(int msg_level)115*7c478bd9Sstevel@tonic-gate log_to_stdout(int msg_level)
116*7c478bd9Sstevel@tonic-gate {
117*7c478bd9Sstevel@tonic-gate 	if (isatty(fileno(stdin)) && (msg_level & MSG_EMIT) != 0)
118*7c478bd9Sstevel@tonic-gate 		return (0);
119*7c478bd9Sstevel@tonic-gate 	return (daemon_log_flag == 0);
120*7c478bd9Sstevel@tonic-gate }
121*7c478bd9Sstevel@tonic-gate 
122*7c478bd9Sstevel@tonic-gate /*
123*7c478bd9Sstevel@tonic-gate  * Can't turn off FATAL or ERROR messages to error log file.
124*7c478bd9Sstevel@tonic-gate  */
125*7c478bd9Sstevel@tonic-gate static int
log_to_error_log(int msg_level)126*7c478bd9Sstevel@tonic-gate log_to_error_log(int msg_level)
127*7c478bd9Sstevel@tonic-gate {
128*7c478bd9Sstevel@tonic-gate 	if (!error_log_fp)
129*7c478bd9Sstevel@tonic-gate 		return (0);
130*7c478bd9Sstevel@tonic-gate 	if (msg_level & (MSG_FATAL|MSG_ERROR))
131*7c478bd9Sstevel@tonic-gate 		return (1);
132*7c478bd9Sstevel@tonic-gate 	return (msg_level & error_log_flags);
133*7c478bd9Sstevel@tonic-gate }
134*7c478bd9Sstevel@tonic-gate 
135*7c478bd9Sstevel@tonic-gate /*
136*7c478bd9Sstevel@tonic-gate  * Can't turn off FATAL or ERROR messages to syslog.
137*7c478bd9Sstevel@tonic-gate  */
138*7c478bd9Sstevel@tonic-gate static int
log_to_syslog(int msg_level)139*7c478bd9Sstevel@tonic-gate log_to_syslog(int msg_level)
140*7c478bd9Sstevel@tonic-gate {
141*7c478bd9Sstevel@tonic-gate 	if (!syslog_opened)
142*7c478bd9Sstevel@tonic-gate 		return (0);
143*7c478bd9Sstevel@tonic-gate 	if (msg_level & (MSG_FATAL|MSG_ERROR))
144*7c478bd9Sstevel@tonic-gate 		return (1);
145*7c478bd9Sstevel@tonic-gate 	return (msg_level & syslog_log_flags);
146*7c478bd9Sstevel@tonic-gate }
147*7c478bd9Sstevel@tonic-gate 
148*7c478bd9Sstevel@tonic-gate /*
149*7c478bd9Sstevel@tonic-gate  * Turn internal MSG level to syslog msg level.  Don't return a msg level
150*7c478bd9Sstevel@tonic-gate  * lower priority than min_syslog_level.
151*7c478bd9Sstevel@tonic-gate  */
152*7c478bd9Sstevel@tonic-gate static int
msg_level_to_syslog(int msg_level)153*7c478bd9Sstevel@tonic-gate msg_level_to_syslog(int msg_level)
154*7c478bd9Sstevel@tonic-gate {
155*7c478bd9Sstevel@tonic-gate 	if (min_syslog_level <= LOG_ERR)
156*7c478bd9Sstevel@tonic-gate 		return (min_syslog_level);
157*7c478bd9Sstevel@tonic-gate 	if (msg_level & (MSG_FATAL|MSG_ERROR))
158*7c478bd9Sstevel@tonic-gate 		return (LOG_ERR);
159*7c478bd9Sstevel@tonic-gate 	if (min_syslog_level <= LOG_WARNING)
160*7c478bd9Sstevel@tonic-gate 		return (min_syslog_level);
161*7c478bd9Sstevel@tonic-gate 	if (msg_level & MSG_WARN)
162*7c478bd9Sstevel@tonic-gate 		return (LOG_WARNING);
163*7c478bd9Sstevel@tonic-gate 	if (min_syslog_level <= LOG_NOTICE)
164*7c478bd9Sstevel@tonic-gate 		return (min_syslog_level);
165*7c478bd9Sstevel@tonic-gate 	if (msg_level & MSG_NOTE)
166*7c478bd9Sstevel@tonic-gate 		return (LOG_NOTICE);
167*7c478bd9Sstevel@tonic-gate 	if (min_syslog_level <= LOG_INFO)
168*7c478bd9Sstevel@tonic-gate 		return (min_syslog_level);
169*7c478bd9Sstevel@tonic-gate 	if (msg_level & MSG_INFO)
170*7c478bd9Sstevel@tonic-gate 		return (LOG_INFO);
171*7c478bd9Sstevel@tonic-gate 	return (min(min_syslog_level, LOG_DEBUG));
172*7c478bd9Sstevel@tonic-gate }
173*7c478bd9Sstevel@tonic-gate 
174*7c478bd9Sstevel@tonic-gate /*
175*7c478bd9Sstevel@tonic-gate  * Log a message to the appropriate places.
176*7c478bd9Sstevel@tonic-gate  */
177*7c478bd9Sstevel@tonic-gate void
log_message(int msg_level,char * fmt,...)178*7c478bd9Sstevel@tonic-gate log_message(int msg_level, char *fmt, ...)
179*7c478bd9Sstevel@tonic-gate {
180*7c478bd9Sstevel@tonic-gate 	va_list ap;
181*7c478bd9Sstevel@tonic-gate 	char msg[LOG_LINESIZE], *p;
182*7c478bd9Sstevel@tonic-gate 	static char log_msg[LOG_LINESIZE];
183*7c478bd9Sstevel@tonic-gate 
184*7c478bd9Sstevel@tonic-gate 	va_start(ap, fmt);
185*7c478bd9Sstevel@tonic-gate 
186*7c478bd9Sstevel@tonic-gate 	vsprintf(msg, fmt, ap);
187*7c478bd9Sstevel@tonic-gate 
188*7c478bd9Sstevel@tonic-gate 	if (log_to_stdout(msg_level)) {
189*7c478bd9Sstevel@tonic-gate 		printf(msg);
190*7c478bd9Sstevel@tonic-gate 		fflush(stdout);
191*7c478bd9Sstevel@tonic-gate 	}
192*7c478bd9Sstevel@tonic-gate 	if (log_to_error_log(msg_level)) {
193*7c478bd9Sstevel@tonic-gate 		fprintf(error_log_fp, msg);
194*7c478bd9Sstevel@tonic-gate 		fflush(error_log_fp);
195*7c478bd9Sstevel@tonic-gate 	}
196*7c478bd9Sstevel@tonic-gate 	if (log_to_syslog(msg_level)) {
197*7c478bd9Sstevel@tonic-gate 		if (strlen(log_msg) + strlen(msg) > LOG_LINESIZE - 1) {
198*7c478bd9Sstevel@tonic-gate 			syslog(msg_level_to_syslog(msg_level), log_msg);
199*7c478bd9Sstevel@tonic-gate 			log_msg[0] = '\0';
200*7c478bd9Sstevel@tonic-gate 		}
201*7c478bd9Sstevel@tonic-gate 		strcat(log_msg, msg);
202*7c478bd9Sstevel@tonic-gate 		if ((p = strchr(log_msg, '\n')) != NULL) {
203*7c478bd9Sstevel@tonic-gate 			*p = '\0';
204*7c478bd9Sstevel@tonic-gate 			syslog(msg_level_to_syslog(msg_level), log_msg);
205*7c478bd9Sstevel@tonic-gate 			log_msg[0] = '\0';
206*7c478bd9Sstevel@tonic-gate 		}
207*7c478bd9Sstevel@tonic-gate 	}
208*7c478bd9Sstevel@tonic-gate }
209*7c478bd9Sstevel@tonic-gate 
210*7c478bd9Sstevel@tonic-gate /*
211*7c478bd9Sstevel@tonic-gate  * Output debug message
212*7c478bd9Sstevel@tonic-gate  */
213*7c478bd9Sstevel@tonic-gate void
debug_msg(int debug_level,char * fmt,...)214*7c478bd9Sstevel@tonic-gate debug_msg(int debug_level, char *fmt, ...)
215*7c478bd9Sstevel@tonic-gate {
216*7c478bd9Sstevel@tonic-gate 	va_list ap;
217*7c478bd9Sstevel@tonic-gate 	char msg[LOG_LINESIZE];
218*7c478bd9Sstevel@tonic-gate 
219*7c478bd9Sstevel@tonic-gate 	if ((debug_level & get_interpreter_debug_level()) == 0)
220*7c478bd9Sstevel@tonic-gate 		return;
221*7c478bd9Sstevel@tonic-gate 
222*7c478bd9Sstevel@tonic-gate 	va_start(ap, fmt);
223*7c478bd9Sstevel@tonic-gate 
224*7c478bd9Sstevel@tonic-gate 	vsprintf(msg, fmt, ap);
225*7c478bd9Sstevel@tonic-gate 
226*7c478bd9Sstevel@tonic-gate 	log_message(MSG_DEBUG, msg);
227*7c478bd9Sstevel@tonic-gate }
228*7c478bd9Sstevel@tonic-gate 
229*7c478bd9Sstevel@tonic-gate /*
230*7c478bd9Sstevel@tonic-gate  * Log a perror message to the appropriate places.
231*7c478bd9Sstevel@tonic-gate  */
232*7c478bd9Sstevel@tonic-gate void
log_perror(int msg_level,char * fmt,...)233*7c478bd9Sstevel@tonic-gate log_perror(int msg_level, char *fmt, ...)
234*7c478bd9Sstevel@tonic-gate {
235*7c478bd9Sstevel@tonic-gate 	va_list ap;
236*7c478bd9Sstevel@tonic-gate 	char msg[LOG_LINESIZE], tmp[LOG_LINESIZE];
237*7c478bd9Sstevel@tonic-gate 
238*7c478bd9Sstevel@tonic-gate 	va_start(ap, fmt);
239*7c478bd9Sstevel@tonic-gate 
240*7c478bd9Sstevel@tonic-gate 	vsprintf(msg, fmt, ap);
241*7c478bd9Sstevel@tonic-gate 	sprintf(tmp, "%s: %s\n", msg, strerror(errno));
242*7c478bd9Sstevel@tonic-gate 	log_message(msg_level, tmp);
243*7c478bd9Sstevel@tonic-gate }
244*7c478bd9Sstevel@tonic-gate 
245*7c478bd9Sstevel@tonic-gate void
set_min_syslog_level(int level)246*7c478bd9Sstevel@tonic-gate set_min_syslog_level(int level)
247*7c478bd9Sstevel@tonic-gate {
248*7c478bd9Sstevel@tonic-gate 	min_syslog_level = level;
249*7c478bd9Sstevel@tonic-gate }
250*7c478bd9Sstevel@tonic-gate 
251*7c478bd9Sstevel@tonic-gate char *error_log_name;
252*7c478bd9Sstevel@tonic-gate 
253*7c478bd9Sstevel@tonic-gate void
open_error_log(char * fname,int errflags)254*7c478bd9Sstevel@tonic-gate open_error_log(char *fname, int errflags)
255*7c478bd9Sstevel@tonic-gate {
256*7c478bd9Sstevel@tonic-gate 	if ((error_log_fp = fopen(fname, "a")) == NULL) {
257*7c478bd9Sstevel@tonic-gate 		log_perror(MSG_FATAL, fname);
258*7c478bd9Sstevel@tonic-gate 		exit(1);
259*7c478bd9Sstevel@tonic-gate 	}
260*7c478bd9Sstevel@tonic-gate 	error_log_name = STRDUP(fname);
261*7c478bd9Sstevel@tonic-gate 	error_log_flags = errflags;
262*7c478bd9Sstevel@tonic-gate 	do_emit_flag = (log_to_stdout(MSG_EMIT) | log_to_error_log(MSG_EMIT) |
263*7c478bd9Sstevel@tonic-gate 	    log_to_syslog(MSG_EMIT));
264*7c478bd9Sstevel@tonic-gate }
265*7c478bd9Sstevel@tonic-gate 
266*7c478bd9Sstevel@tonic-gate void
open_syslog_log(char * logname,int logflags)267*7c478bd9Sstevel@tonic-gate open_syslog_log(char *logname, int logflags)
268*7c478bd9Sstevel@tonic-gate {
269*7c478bd9Sstevel@tonic-gate 	openlog(logname, LOG_PID, LOG_DAEMON);
270*7c478bd9Sstevel@tonic-gate 	syslog_log_flags = logflags;
271*7c478bd9Sstevel@tonic-gate 	do_emit_flag = (log_to_stdout(MSG_EMIT) | log_to_error_log(MSG_EMIT) |
272*7c478bd9Sstevel@tonic-gate 	    log_to_syslog(MSG_EMIT));
273*7c478bd9Sstevel@tonic-gate 	syslog_opened = 1;
274*7c478bd9Sstevel@tonic-gate }
275*7c478bd9Sstevel@tonic-gate 
276*7c478bd9Sstevel@tonic-gate /*
277*7c478bd9Sstevel@tonic-gate  * Turn on/off syslog LOG_DAEMON flag to syslog messages.  Also controls
278*7c478bd9Sstevel@tonic-gate  * outputting to stdout, which is a waste of time when we're running as a
279*7c478bd9Sstevel@tonic-gate  * daemon.
280*7c478bd9Sstevel@tonic-gate  */
281*7c478bd9Sstevel@tonic-gate void
set_daemon_log_flag(int flag)282*7c478bd9Sstevel@tonic-gate set_daemon_log_flag(int flag)
283*7c478bd9Sstevel@tonic-gate {
284*7c478bd9Sstevel@tonic-gate 	if (flag)
285*7c478bd9Sstevel@tonic-gate 		daemon_log_flag = LOG_DAEMON;
286*7c478bd9Sstevel@tonic-gate 	else
287*7c478bd9Sstevel@tonic-gate 		daemon_log_flag = 0;
288*7c478bd9Sstevel@tonic-gate }
289*7c478bd9Sstevel@tonic-gate 
290*7c478bd9Sstevel@tonic-gate int
parse_msg_flags(char * flags)291*7c478bd9Sstevel@tonic-gate parse_msg_flags(char *flags)
292*7c478bd9Sstevel@tonic-gate {
293*7c478bd9Sstevel@tonic-gate 	int msgflags = 0;
294*7c478bd9Sstevel@tonic-gate 	char c;
295*7c478bd9Sstevel@tonic-gate 
296*7c478bd9Sstevel@tonic-gate 	while ((c = *flags++) != '\0') {
297*7c478bd9Sstevel@tonic-gate 		switch (c) {
298*7c478bd9Sstevel@tonic-gate 		case 'f': msgflags |= MSG_FATAL; break;
299*7c478bd9Sstevel@tonic-gate 		case 'e': msgflags |= MSG_ERROR; break;
300*7c478bd9Sstevel@tonic-gate 		case 'w': msgflags |= MSG_WARN;  break;
301*7c478bd9Sstevel@tonic-gate 		case 'i': msgflags |= MSG_INFO;  break;
302*7c478bd9Sstevel@tonic-gate 		case 'd': msgflags |= MSG_DEBUG; break;
303*7c478bd9Sstevel@tonic-gate 		case 'D': msgflags |= MSG_FC_DEBUG; break;
304*7c478bd9Sstevel@tonic-gate 
305*7c478bd9Sstevel@tonic-gate 		default:
306*7c478bd9Sstevel@tonic-gate 			log_message(MSG_ERROR, "Invalid msglvl flag: %c\n", c);
307*7c478bd9Sstevel@tonic-gate 			break;
308*7c478bd9Sstevel@tonic-gate 		}
309*7c478bd9Sstevel@tonic-gate 	}
310*7c478bd9Sstevel@tonic-gate 	return (msgflags);
311*7c478bd9Sstevel@tonic-gate }
312*7c478bd9Sstevel@tonic-gate 
313*7c478bd9Sstevel@tonic-gate #define	MAXERRBUF	256
314*7c478bd9Sstevel@tonic-gate char error_buffer[MAXERRBUF];
315*7c478bd9Sstevel@tonic-gate 
316*7c478bd9Sstevel@tonic-gate static void
dot_error_buffer(fcode_env_t * env)317*7c478bd9Sstevel@tonic-gate dot_error_buffer(fcode_env_t *env)
318*7c478bd9Sstevel@tonic-gate {
319*7c478bd9Sstevel@tonic-gate 	log_message(MSG_INFO, "%s\n", error_buffer);
320*7c478bd9Sstevel@tonic-gate }
321*7c478bd9Sstevel@tonic-gate 
322*7c478bd9Sstevel@tonic-gate static void
set_error_log(fcode_env_t * env)323*7c478bd9Sstevel@tonic-gate set_error_log(fcode_env_t *env)
324*7c478bd9Sstevel@tonic-gate {
325*7c478bd9Sstevel@tonic-gate 	char *fname;
326*7c478bd9Sstevel@tonic-gate 	FILE *fp;
327*7c478bd9Sstevel@tonic-gate 
328*7c478bd9Sstevel@tonic-gate 	parse_word(env);
329*7c478bd9Sstevel@tonic-gate 	fname = pop_a_string(env, NULL);
330*7c478bd9Sstevel@tonic-gate 	if (fname != NULL) {
331*7c478bd9Sstevel@tonic-gate 		if ((fp = fopen(fname, "a")) == NULL) {
332*7c478bd9Sstevel@tonic-gate 			log_perror(MSG_ERROR, "Can't open '%s'\n", fname);
333*7c478bd9Sstevel@tonic-gate 			return;
334*7c478bd9Sstevel@tonic-gate 		}
335*7c478bd9Sstevel@tonic-gate 		if (error_log_fp)
336*7c478bd9Sstevel@tonic-gate 			fclose(error_log_fp);
337*7c478bd9Sstevel@tonic-gate 		if (error_log_name)
338*7c478bd9Sstevel@tonic-gate 			FREE(error_log_name);
339*7c478bd9Sstevel@tonic-gate 		error_log_fp = fp;
340*7c478bd9Sstevel@tonic-gate 		error_log_name = STRDUP(fname);
341*7c478bd9Sstevel@tonic-gate 		error_log_flags = MSG_FATAL|MSG_ERROR|MSG_WARN|MSG_INFO|
342*7c478bd9Sstevel@tonic-gate 		    MSG_DEBUG|MSG_FC_DEBUG;
343*7c478bd9Sstevel@tonic-gate 	} else if (error_log_name)
344*7c478bd9Sstevel@tonic-gate 		log_message(MSG_INFO, "%s\n", error_log_name);
345*7c478bd9Sstevel@tonic-gate 	else
346*7c478bd9Sstevel@tonic-gate 		log_message(MSG_INFO, "NULL\n");
347*7c478bd9Sstevel@tonic-gate }
348*7c478bd9Sstevel@tonic-gate 
349*7c478bd9Sstevel@tonic-gate #pragma init(_init)
350*7c478bd9Sstevel@tonic-gate 
351*7c478bd9Sstevel@tonic-gate static void
_init(void)352*7c478bd9Sstevel@tonic-gate _init(void)
353*7c478bd9Sstevel@tonic-gate {
354*7c478bd9Sstevel@tonic-gate 	fcode_env_t *env = initial_env;
355*7c478bd9Sstevel@tonic-gate 
356*7c478bd9Sstevel@tonic-gate 	ASSERT(env);
357*7c478bd9Sstevel@tonic-gate 	NOTICE;
358*7c478bd9Sstevel@tonic-gate 
359*7c478bd9Sstevel@tonic-gate 	FORTH(0,	".error-buffer",	dot_error_buffer);
360*7c478bd9Sstevel@tonic-gate 	FORTH(0,	"set-error-log",	set_error_log);
361*7c478bd9Sstevel@tonic-gate }
362