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