xref: /titanic_50/usr/src/cmd/lp/cmd/lpsched/log.c (revision 372a60c34a6075464eaab2e7e079cbbc781f9215)
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 /*
23  * Copyright 2006 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 "stdarg.h"
34 #include "lpsched.h"
35 #include <syslog.h>
36 
37 static void log(char *, va_list);
38 
39 /**
40  ** open_logfile() - OPEN FILE FOR LOGGING MESSAGE
41  **/
42 
43 static int
44 open_logfile(char *name)
45 {
46 	char			path[80];
47 
48 	snprintf(path, sizeof (path), "%s/%s", Lp_Logs, name);
49 	return (open_locked(path, "a", 0640));
50 }
51 
52 
53 /**
54  ** fail() - LOG MESSAGE AND EXIT (ABORT IF DEBUGGING)
55  **/
56 
57 /*VARARGS1*/
58 void
59 fail(char *format, ...)
60 {
61 	va_list			ap;
62 
63 	va_start (ap, format);
64 	log (format, ap);
65 	va_end (ap);
66 
67 #if	defined(DEBUG)
68 	if (debug & DB_ABORT)
69 		abort ();
70 	else
71 #endif
72 		exit (1);
73 	/*NOTREACHED*/
74 }
75 
76 /**
77  ** note() - LOG MESSAGE
78  **/
79 
80 /*VARARGS1*/
81 void
82 note(char *format, ...)
83 {
84 	va_list			ap;
85 
86 	va_start (ap, format);
87 	log (format, ap);
88 	va_end (ap);
89 }
90 
91 
92 
93 /**
94  ** mallocfail() - COMPLAIN ABOUT MEMORY ALLOCATION FAILURE
95  **/
96 
97 void
98 mallocfail(void)
99 {
100 	fail ("Memory allocation failed!\n");
101 	/*NOTREACHED*/
102 }
103 
104 /**
105  ** log() - LOW LEVEL ROUTINE THAT LOGS MESSSAGES
106  **/
107 
108 static void
109 log(char *format, va_list ap)
110 {
111 	int			close_it;
112 	int			fd;
113 	static int		nodate	= 0;
114 	char buf[BUFSIZ];
115 
116 	vsyslog(LOG_DEBUG, format, ap);
117 
118 	if (!am_in_background) {
119 		fd = 1;
120 		close_it = 0;
121 	} else {
122 		if ((fd = open_logfile("lpsched")) < 0)
123 			return;
124 		close_it = 1;
125 	}
126 
127 	if (am_in_background && !nodate) {
128 		time_t curtime;
129 		struct tm *tm;
130 
131 		time(&curtime);
132 		if ((tm = localtime(&curtime)) != NULL)
133 			fdprintf (fd, "%.2d/%.2d %.2d:%.2d:%.2d: ",
134 			 	tm->tm_mon+1, tm->tm_mday, tm->tm_hour,
135 				tm->tm_min, tm->tm_sec);
136 		else
137 			fdprintf(fd, "bad date: ");
138 	}
139 	nodate = 0;
140 
141 	vsnprintf (buf, sizeof (buf),  format, ap);
142 	write(fd, buf, strlen(buf));
143 	if (format[strlen(format) - 1] != '\n')
144 		nodate = 1;
145 
146 	if (close_it)
147 		close(fd);
148 }
149 
150 /**
151  ** execlog()
152  **/
153 
154 /*VARARGS1*/
155 void
156 execlog(char *format, ...)
157 {
158 	va_list			ap;
159 
160 #if	defined(DEBUG)
161 	int			fd	= open_logfile("exec");
162 	char			buf[BUFSIZ];
163 	EXEC *			ep;
164 	static int		nodate	= 0;
165 
166 	va_start (ap, format);
167 	if (fd >= 0) {
168 		if (!nodate) {
169 			time_t now = time((time_t *)0);
170 
171 			fdprintf (fd, "%24.24s: ", ctime(&now));
172 		}
173 		nodate = 0;
174 		if (!STREQU(format, "%e")) {
175 			vsnprintf (buf, sizeof (buf), format, ap);
176 			write(fd, buf, strlen(buf));
177 			if (format[strlen(format) - 1] != '\n')
178 				nodate = 1;
179 		} else switch ((ep = va_arg(ap, EXEC *))->type) {
180 		case EX_INTERF:
181 			fdprintf(fd, "      EX_INTERF %s %s\n",
182 				ep->ex.printer->printer->name,
183 				ep->ex.printer->request->secure->req_id);
184 			break;
185 		case EX_SLOWF:
186 			fdprintf(fd, "      EX_SLOWF %s\n",
187 				ep->ex.request->secure->req_id);
188 			break;
189 		case EX_ALERT:
190 			fdprintf(fd, "      EX_ALERT %s\n",
191 				ep->ex.printer->printer->name);
192 			break;
193 		case EX_FAULT_MESSAGE:
194 			fdprintf(fd, "      EX_FAULT_MESSAGE %s\n",
195 				ep->ex.printer->printer->name);
196 			break;
197 		case EX_FORM_MESSAGE:
198 			fdprintf(fd, "      EX_FORM_MESSAGE %s\n",
199 				ep->ex.form->form->name);
200 			break;
201 		case EX_FALERT:
202 			fdprintf(fd, "      EX_FALERT %s\n",
203 				ep->ex.form->form->name);
204 			break;
205 		case EX_PALERT:
206 			fdprintf(fd, "      EX_PALERT %s\n",
207 				ep->ex.pwheel->pwheel->name);
208 			break;
209 		case EX_NOTIFY:
210 			fdprintf(fd, "      EX_NOTIFY %s\n",
211 				ep->ex.request->secure->req_id);
212 			break;
213 		default:
214 			fdprintf (fd, "      EX_???\n");
215 			break;
216 		}
217 		close(fd);
218 	}
219 #endif
220 }
221