xref: /titanic_52/usr/src/cmd/saf/log.c (revision af3025fd013117424096c94bd9e116ecc538cc6b)
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 (the "License").
6  * You may not use this file except in compliance with the License.
7  *
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.
12  *
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]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
27 /*	  All Rights Reserved  	*/
28 
29 
30 #pragma ident	"%Z%%M%	%I%	%E% SMI"
31 
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>
40 
41 #include "extern.h"
42 #include "misc.h"
43 #include "msgs.h"
44 #include <sac.h>
45 #include "structs.h"
46 
47 static	FILE	*Lfp;	/* log file */
48 #ifdef DEBUG
49 static	FILE	*Dfp;	/* debug file */
50 #endif
51 
52 
53 /*
54  * cons_printf - emit a message to the system console
55  */
56 
57 /*PRINTFLIKE1*/
58 static void
59 cons_printf(const char *fmt, ...)
60 {
61 	char buf[MAXPATHLEN * 2]; /* enough space for msg including a path */
62 	int fd;
63 	va_list ap;
64 
65 	va_start(ap, fmt);
66 	(void) vsnprintf(buf, sizeof (buf), fmt, ap);
67 	va_end(ap);
68 
69 	if ((fd = open("/dev/console", O_WRONLY|O_NOCTTY)) != -1)
70 		(void) write(fd, buf, strlen(buf) + 1);
71 	(void) close(fd);
72 }
73 
74 /*
75  * openlog - open log file, sets global file pointer Lfp
76  */
77 
78 void
79 openlog()
80 {
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);
85 	}
86 
87 	/*
88 	 * lock logfile to indicate presence
89 	 */
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);
94 	}
95 }
96 
97 
98 /*
99  * log - put a message into the log file
100  *
101  *	args:	msg - message to be logged
102  */
103 void
104 log(char *msg)
105 {
106 	char *timestamp;	/* current time in readable form */
107 	time_t clock;		/* current time in seconds */
108 	char buf[SIZE];		/* scratch buffer */
109 
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);
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