dmake: do not set MAKEFLAGS=k
[unleashed/tickless.git] / usr / src / lib / efcode / engine / log.c
blobb17f38b8c4ab4671d56b975ce2ae9c49c3b56dff
1 /*
2 * CDDL HEADER START
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.
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.
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]
20 * CDDL HEADER END
23 * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 #pragma ident "%Z%%M% %I% %E% SMI"
30 * Daemon log message. This can direct log messages to either stdout,
31 * an error log file or syslog (or any combination).
34 #include <stdarg.h>
35 #include <stdio.h>
36 #include <syslog.h>
37 #include <errno.h>
38 #include <string.h>
39 #include <unistd.h>
40 #include <stdlib.h>
42 #include <fcode/private.h>
43 #include <fcode/log.h>
45 #define LOG_LINESIZE 256
46 #define LOG_EMIT_BUFSIZE LOG_LINESIZE
48 static FILE *error_log_fp = NULL;
49 static int syslog_opened = 0;
50 static int error_log_flags;
51 static int syslog_log_flags;
52 static int do_emit_flag = -1;
53 static int daemon_log_flag;
54 static int min_syslog_level = LOG_ERR;
56 static int log_to_stdout(int);
57 static int log_to_error_log(int);
58 static int log_to_syslog(int);
59 static int msg_level_to_syslog(int);
62 * Complicated by not wanting to do any emit processing if no one's actually
63 * going to see it.
65 void
66 log_emit(char c)
68 static char emit_buf[LOG_EMIT_BUFSIZE];
69 static char *emit_p = emit_buf;
70 static int lastnl = 1;
73 * No one has set the do_emit_flag, go ahead and figure it out.
75 if (do_emit_flag < 0) {
76 do_emit_flag = (log_to_stdout(MSG_EMIT) |
77 log_to_error_log(MSG_EMIT) | log_to_syslog(MSG_EMIT));
80 if (!do_emit_flag)
81 return;
84 * Check for buffer overflow.
86 if (emit_p >= &emit_buf[LOG_EMIT_BUFSIZE - 1]) {
87 *emit_p = '\0';
88 log_message(MSG_EMIT, "emit: %s\n", emit_buf);
89 emit_p = emit_buf;
90 lastnl = 1;
94 * Fcode emit's may output both CR/LF, we go ahead and eat multiple
95 * ones in succession.
97 if (c == '\n' || c == '\r') {
98 if (!lastnl) {
99 *emit_p = '\0';
100 log_message(MSG_EMIT, "emit: %s\n", emit_buf);
101 emit_p = emit_buf;
103 lastnl = 1;
104 } else {
105 lastnl = 0;
106 *emit_p++ = c;
111 * If stdout is a tty and this is MSG_EMIT, we should have alredy output it.
112 * If running as daemon, output to stdout is a waste of time.
114 static int
115 log_to_stdout(int msg_level)
117 if (isatty(fileno(stdin)) && (msg_level & MSG_EMIT) != 0)
118 return (0);
119 return (daemon_log_flag == 0);
123 * Can't turn off FATAL or ERROR messages to error log file.
125 static int
126 log_to_error_log(int msg_level)
128 if (!error_log_fp)
129 return (0);
130 if (msg_level & (MSG_FATAL|MSG_ERROR))
131 return (1);
132 return (msg_level & error_log_flags);
136 * Can't turn off FATAL or ERROR messages to syslog.
138 static int
139 log_to_syslog(int msg_level)
141 if (!syslog_opened)
142 return (0);
143 if (msg_level & (MSG_FATAL|MSG_ERROR))
144 return (1);
145 return (msg_level & syslog_log_flags);
149 * Turn internal MSG level to syslog msg level. Don't return a msg level
150 * lower priority than min_syslog_level.
152 static int
153 msg_level_to_syslog(int msg_level)
155 if (min_syslog_level <= LOG_ERR)
156 return (min_syslog_level);
157 if (msg_level & (MSG_FATAL|MSG_ERROR))
158 return (LOG_ERR);
159 if (min_syslog_level <= LOG_WARNING)
160 return (min_syslog_level);
161 if (msg_level & MSG_WARN)
162 return (LOG_WARNING);
163 if (min_syslog_level <= LOG_NOTICE)
164 return (min_syslog_level);
165 if (msg_level & MSG_NOTE)
166 return (LOG_NOTICE);
167 if (min_syslog_level <= LOG_INFO)
168 return (min_syslog_level);
169 if (msg_level & MSG_INFO)
170 return (LOG_INFO);
171 return (min(min_syslog_level, LOG_DEBUG));
175 * Log a message to the appropriate places.
177 void
178 log_message(int msg_level, char *fmt, ...)
180 va_list ap;
181 char msg[LOG_LINESIZE], *p;
182 static char log_msg[LOG_LINESIZE];
184 va_start(ap, fmt);
186 vsprintf(msg, fmt, ap);
188 if (log_to_stdout(msg_level)) {
189 printf(msg);
190 fflush(stdout);
192 if (log_to_error_log(msg_level)) {
193 fprintf(error_log_fp, msg);
194 fflush(error_log_fp);
196 if (log_to_syslog(msg_level)) {
197 if (strlen(log_msg) + strlen(msg) > LOG_LINESIZE - 1) {
198 syslog(msg_level_to_syslog(msg_level), log_msg);
199 log_msg[0] = '\0';
201 strcat(log_msg, msg);
202 if ((p = strchr(log_msg, '\n')) != NULL) {
203 *p = '\0';
204 syslog(msg_level_to_syslog(msg_level), log_msg);
205 log_msg[0] = '\0';
211 * Output debug message
213 void
214 debug_msg(int debug_level, char *fmt, ...)
216 va_list ap;
217 char msg[LOG_LINESIZE];
219 if ((debug_level & get_interpreter_debug_level()) == 0)
220 return;
222 va_start(ap, fmt);
224 vsprintf(msg, fmt, ap);
226 log_message(MSG_DEBUG, msg);
230 * Log a perror message to the appropriate places.
232 void
233 log_perror(int msg_level, char *fmt, ...)
235 va_list ap;
236 char msg[LOG_LINESIZE], tmp[LOG_LINESIZE];
238 va_start(ap, fmt);
240 vsprintf(msg, fmt, ap);
241 sprintf(tmp, "%s: %s\n", msg, strerror(errno));
242 log_message(msg_level, tmp);
245 void
246 set_min_syslog_level(int level)
248 min_syslog_level = level;
251 char *error_log_name;
253 void
254 open_error_log(char *fname, int errflags)
256 if ((error_log_fp = fopen(fname, "a")) == NULL) {
257 log_perror(MSG_FATAL, fname);
258 exit(1);
260 error_log_name = STRDUP(fname);
261 error_log_flags = errflags;
262 do_emit_flag = (log_to_stdout(MSG_EMIT) | log_to_error_log(MSG_EMIT) |
263 log_to_syslog(MSG_EMIT));
266 void
267 open_syslog_log(char *logname, int logflags)
269 openlog(logname, LOG_PID, LOG_DAEMON);
270 syslog_log_flags = logflags;
271 do_emit_flag = (log_to_stdout(MSG_EMIT) | log_to_error_log(MSG_EMIT) |
272 log_to_syslog(MSG_EMIT));
273 syslog_opened = 1;
277 * Turn on/off syslog LOG_DAEMON flag to syslog messages. Also controls
278 * outputting to stdout, which is a waste of time when we're running as a
279 * daemon.
281 void
282 set_daemon_log_flag(int flag)
284 if (flag)
285 daemon_log_flag = LOG_DAEMON;
286 else
287 daemon_log_flag = 0;
291 parse_msg_flags(char *flags)
293 int msgflags = 0;
294 char c;
296 while ((c = *flags++) != '\0') {
297 switch (c) {
298 case 'f': msgflags |= MSG_FATAL; break;
299 case 'e': msgflags |= MSG_ERROR; break;
300 case 'w': msgflags |= MSG_WARN; break;
301 case 'i': msgflags |= MSG_INFO; break;
302 case 'd': msgflags |= MSG_DEBUG; break;
303 case 'D': msgflags |= MSG_FC_DEBUG; break;
305 default:
306 log_message(MSG_ERROR, "Invalid msglvl flag: %c\n", c);
307 break;
310 return (msgflags);
313 #define MAXERRBUF 256
314 char error_buffer[MAXERRBUF];
316 static void
317 dot_error_buffer(fcode_env_t *env)
319 log_message(MSG_INFO, "%s\n", error_buffer);
322 static void
323 set_error_log(fcode_env_t *env)
325 char *fname;
326 FILE *fp;
328 parse_word(env);
329 fname = pop_a_string(env, NULL);
330 if (fname != NULL) {
331 if ((fp = fopen(fname, "a")) == NULL) {
332 log_perror(MSG_ERROR, "Can't open '%s'\n", fname);
333 return;
335 if (error_log_fp)
336 fclose(error_log_fp);
337 if (error_log_name)
338 FREE(error_log_name);
339 error_log_fp = fp;
340 error_log_name = STRDUP(fname);
341 error_log_flags = MSG_FATAL|MSG_ERROR|MSG_WARN|MSG_INFO|
342 MSG_DEBUG|MSG_FC_DEBUG;
343 } else if (error_log_name)
344 log_message(MSG_INFO, "%s\n", error_log_name);
345 else
346 log_message(MSG_INFO, "NULL\n");
349 #pragma init(_init)
351 static void
352 _init(void)
354 fcode_env_t *env = initial_env;
356 ASSERT(env);
357 NOTICE;
359 FORTH(0, ".error-buffer", dot_error_buffer);
360 FORTH(0, "set-error-log", set_error_log);