xref: /titanic_51/usr/src/cmd/saf/log.c (revision 7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fe)
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 2005 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
28 /*	  All Rights Reserved  	*/
29 
30 
31 #pragma ident	"%Z%%M%	%I%	%E% SMI"
32 
33 #include <sys/types.h>
34 #include <sys/param.h>
35 #include <sys/fcntl.h>
36 #include <stdio.h>
37 #include <unistd.h>
38 #include <stdarg.h>
39 #include <strings.h>
40 #include <errno.h>
41 
42 #include "extern.h"
43 #include "misc.h"
44 #include "msgs.h"
45 #include <sac.h>
46 #include "structs.h"
47 
48 static	FILE	*Lfp;	/* log file */
49 #ifdef DEBUG
50 static	FILE	*Dfp;	/* debug file */
51 #endif
52 
53 
54 /*
55  * cons_printf - emit a message to the system console
56  */
57 
58 /*PRINTFLIKE1*/
59 static void
60 cons_printf(const char *fmt, ...)
61 {
62 	char buf[MAXPATHLEN * 2]; /* enough space for msg including a path */
63 	int fd;
64 	va_list ap;
65 
66 	va_start(ap, fmt);
67 	(void) vsnprintf(buf, sizeof (buf), fmt, ap);
68 	va_end(ap);
69 
70 	if ((fd = open("/dev/console", O_WRONLY|O_NOCTTY)) != -1)
71 		(void) write(fd, buf, strlen(buf) + 1);
72 	(void) close(fd);
73 }
74 
75 /*
76  * openlog - open log file, sets global file pointer Lfp
77  */
78 
79 void
80 openlog()
81 {
82 	if ((Lfp = fopen(LOGFILE, "a+")) == NULL) {
83 		cons_printf("SAC: could not open logfile %s: %s\n",
84 		    LOGFILE, strerror(errno));
85 		exit(1);
86 	}
87 
88 	/*
89 	 * lock logfile to indicate presence
90 	 */
91 	if (lockf(fileno(Lfp), F_LOCK, 0) < 0) {
92 		cons_printf("SAC: could not lock logfile %s:%s\n",
93 		    LOGFILE, strerror(errno));
94 		exit(1);
95 	}
96 }
97 
98 
99 /*
100  * log - put a message into the log file
101  *
102  *	args:	msg - message to be logged
103  */
104 void
105 log(char *msg)
106 {
107 	char *timestamp;	/* current time in readable form */
108 	time_t clock;		/* current time in seconds */
109 	char buf[SIZE];		/* scratch buffer */
110 
111 	(void) time(&clock);
112 	timestamp = ctime(&clock);
113 	*(strchr(timestamp, '\n')) = '\0';
114 	(void) sprintf(buf, "%s; %ld; %s\n", timestamp, getpid(), msg);
115 	(void) fprintf(Lfp, buf);
116 	(void) fflush(Lfp);
117 }
118 
119 
120 /*
121  * error - put an error message into the log file and exit if indicated
122  *
123  *	args:	msgid - id of message to be output
124  *		action - action to be taken (EXIT or not)
125  */
126 
127 
128 void
129 error(int msgid, int action)
130 {
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);
137 	}
138 }
139 
140 
141 #ifdef DEBUG
142 
143 /*
144  * opendebug - open debugging file, sets global file pointer Dfp
145  */
146 
147 
148 void
149 opendebug()
150 {
151 	FILE *fp;	/* scratch file pointer for problems */
152 
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);
157 	}
158 }
159 
160 
161 /*
162  * debug - put a message into debug file
163  *
164  *	args:	msg - message to be output
165  */
166 
167 
168 void
169 debug(char *msg)
170 {
171 	char *timestamp;	/* current time in readable form */
172 	time_t clock;		/* current time in seconds */
173 	char buf[SIZE];		/* scratch buffer */
174 
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);
181 }
182 
183 #endif /* DEBUG */
184