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 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */ 23 /* All Rights Reserved */ 24 25 26 /* 27 * Copyright (c) 2001 by Sun Microsystems, Inc. 28 * All rights reserved. 29 */ 30 31 #pragma ident "%Z%%M% %I% %E% SMI" 32 33 #include "stdarg.h" 34 #include "lpsched.h" 35 36 static void log(char *, va_list); 37 38 /** 39 ** open_logfile() - OPEN FILE FOR LOGGING MESSAGE 40 **/ 41 42 static int 43 open_logfile(char *name) 44 { 45 char path[80]; 46 47 snprintf(path, sizeof (path), "%s/%s", Lp_Logs, name); 48 return (open_locked(path, "a", 0640)); 49 } 50 51 52 /** 53 ** fail() - LOG MESSAGE AND EXIT (ABORT IF DEBUGGING) 54 **/ 55 56 /*VARARGS1*/ 57 void 58 fail(char *format, ...) 59 { 60 va_list ap; 61 62 va_start (ap, format); 63 log (format, ap); 64 va_end (ap); 65 66 #if defined(DEBUG) 67 if (debug & DB_ABORT) 68 abort (); 69 else 70 #endif 71 exit (1); 72 /*NOTREACHED*/ 73 } 74 75 /** 76 ** note() - LOG MESSAGE 77 **/ 78 79 /*VARARGS1*/ 80 void 81 note(char *format, ...) 82 { 83 va_list ap; 84 85 va_start (ap, format); 86 log (format, ap); 87 va_end (ap); 88 } 89 90 91 92 /** 93 ** mallocfail() - COMPLAIN ABOUT MEMORY ALLOCATION FAILURE 94 **/ 95 96 void 97 mallocfail(void) 98 { 99 fail ("Memory allocation failed!\n"); 100 /*NOTREACHED*/ 101 } 102 103 /** 104 ** log() - LOW LEVEL ROUTINE THAT LOGS MESSSAGES 105 **/ 106 107 static void 108 log(char *format, va_list ap) 109 { 110 int close_it; 111 int fd; 112 static int nodate = 0; 113 char buf[BUFSIZ]; 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 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