Merge commit '00f1a4f432b3d8aad1aa270e91c44c57f03ef407'
[unleashed.git] / usr / src / cmd / saf / log.c
blobd80173e3ae27b4c929f0652aa70ca9b157ca885a
1 /*
2 * CDDL HEADER START
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.
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.
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]
19 * CDDL HEADER END
22 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
27 /* All Rights Reserved */
30 #pragma ident "%Z%%M% %I% %E% SMI"
32 #include <sys/types.h>
33 #include <sys/param.h>
34 #include <sys/fcntl.h>
35 #include <stdio.h>
36 #include <unistd.h>
37 #include <stdarg.h>
38 #include <strings.h>
39 #include <errno.h>
41 #include "extern.h"
42 #include "misc.h"
43 #include "msgs.h"
44 #include <sac.h>
45 #include "structs.h"
47 static FILE *Lfp; /* log file */
48 #ifdef DEBUG
49 static FILE *Dfp; /* debug file */
50 #endif
54 * cons_printf - emit a message to the system console
57 /*PRINTFLIKE1*/
58 static void
59 cons_printf(const char *fmt, ...)
61 char buf[MAXPATHLEN * 2]; /* enough space for msg including a path */
62 int fd;
63 va_list ap;
65 va_start(ap, fmt);
66 (void) vsnprintf(buf, sizeof (buf), fmt, ap);
67 va_end(ap);
69 if ((fd = open("/dev/console", O_WRONLY|O_NOCTTY)) != -1)
70 (void) write(fd, buf, strlen(buf) + 1);
71 (void) close(fd);
75 * openlog - open log file, sets global file pointer Lfp
78 void
79 openlog()
81 if ((Lfp = fopen(LOGFILE, "a+")) == NULL) {
82 cons_printf("SAC: could not open logfile %s: %s\n",
83 LOGFILE, strerror(errno));
84 exit(1);
88 * lock logfile to indicate presence
90 if (lockf(fileno(Lfp), F_LOCK, 0) < 0) {
91 cons_printf("SAC: could not lock logfile %s:%s\n",
92 LOGFILE, strerror(errno));
93 exit(1);
99 * log - put a message into the log file
101 * args: msg - message to be logged
103 void
104 log(char *msg)
106 char *timestamp; /* current time in readable form */
107 time_t clock; /* current time in seconds */
108 char buf[SIZE]; /* scratch buffer */
110 (void) time(&clock);
111 timestamp = ctime(&clock);
112 *(strchr(timestamp, '\n')) = '\0';
113 (void) snprintf(buf, sizeof (buf), "%s; %ld; %s\n",
114 timestamp, getpid(), msg);
115 (void) fprintf(Lfp, buf);
116 (void) fflush(Lfp);
121 * error - put an error message into the log file and exit if indicated
123 * args: msgid - id of message to be output
124 * action - action to be taken (EXIT or not)
128 void
129 error(int msgid, int action)
131 if (msgid < 0 || msgid > N_msgs)
132 return;
133 log(Msgs[msgid].e_str);
134 if (action == EXIT) {
135 log("*** SAC exiting ***");
136 exit(Msgs[msgid].e_exitcode);
141 #ifdef DEBUG
144 * opendebug - open debugging file, sets global file pointer Dfp
148 void
149 opendebug()
151 FILE *fp; /* scratch file pointer for problems */
153 if ((Dfp = fopen(DBGFILE, "a+")) == NULL) {
154 cons_printf("SAC: could not open debugfile %s: %s\n",
155 DBGFILE, strerror(errno));
156 exit(1);
162 * debug - put a message into debug file
164 * args: msg - message to be output
168 void
169 debug(char *msg)
171 char *timestamp; /* current time in readable form */
172 time_t clock; /* current time in seconds */
173 char buf[SIZE]; /* scratch buffer */
175 (void) time(&clock);
176 timestamp = ctime(&clock);
177 *(strchr(timestamp, '\n')) = '\0';
178 (void) sprintf(buf, "%s; %ld; %s\n", timestamp, getpid(), msg);
179 (void) fprintf(Dfp, buf);
180 (void) fflush(Dfp);
183 #endif /* DEBUG */