#include "stdarg.h"
#include "lpsched.h"
#include <syslog.h>
static void log(char *, va_list);
static int
open_logfile(char *name)
{
char path[80];
snprintf(path, sizeof (path), "%s/%s", Lp_Logs, name);
return (open_locked(path, "a", 0640));
}
void
fail(char *format, ...)
{
va_list ap;
va_start (ap, format);
log (format, ap);
va_end (ap);
#if defined(DEBUG)
if (debug & DB_ABORT)
abort ();
else
#endif
exit (1);
}
void
note(char *format, ...)
{
va_list ap;
va_start (ap, format);
log (format, ap);
va_end (ap);
}
void
mallocfail(void)
{
fail ("Memory allocation failed!\n");
}
static void
log(char *format, va_list ap)
{
int close_it;
int fd;
static int nodate = 0;
char buf[BUFSIZ];
vsyslog(LOG_DEBUG, format, ap);
if (!am_in_background) {
fd = 1;
close_it = 0;
} else {
if ((fd = open_logfile("lpsched")) < 0)
return;
close_it = 1;
}
if (am_in_background && !nodate) {
time_t curtime;
struct tm *tm;
time(&curtime);
if ((tm = localtime(&curtime)) != NULL)
fdprintf (fd, "%.2d/%.2d %.2d:%.2d:%.2d: ",
tm->tm_mon+1, tm->tm_mday, tm->tm_hour,
tm->tm_min, tm->tm_sec);
else
fdprintf(fd, "bad date: ");
}
nodate = 0;
vsnprintf (buf, sizeof (buf), format, ap);
write(fd, buf, strlen(buf));
if (format[strlen(format) - 1] != '\n')
nodate = 1;
if (close_it)
close(fd);
}
void
execlog(char *format, ...)
{
va_list ap;
#if defined(DEBUG)
int fd = open_logfile("exec");
char buf[BUFSIZ];
EXEC * ep;
static int nodate = 0;
va_start (ap, format);
if (fd >= 0) {
if (!nodate) {
time_t now = time((time_t *)0);
fdprintf (fd, "%24.24s: ", ctime(&now));
}
nodate = 0;
if (!STREQU(format, "%e")) {
vsnprintf (buf, sizeof (buf), format, ap);
write(fd, buf, strlen(buf));
if (format[strlen(format) - 1] != '\n')
nodate = 1;
} else switch ((ep = va_arg(ap, EXEC *))->type) {
case EX_INTERF:
fdprintf(fd, " EX_INTERF %s %s\n",
ep->ex.printer->printer->name,
ep->ex.printer->request->secure->req_id);
break;
case EX_SLOWF:
fdprintf(fd, " EX_SLOWF %s\n",
ep->ex.request->secure->req_id);
break;
case EX_ALERT:
fdprintf(fd, " EX_ALERT %s\n",
ep->ex.printer->printer->name);
break;
case EX_FAULT_MESSAGE:
fdprintf(fd, " EX_FAULT_MESSAGE %s\n",
ep->ex.printer->printer->name);
break;
case EX_FORM_MESSAGE:
fdprintf(fd, " EX_FORM_MESSAGE %s\n",
ep->ex.form->form->name);
break;
case EX_FALERT:
fdprintf(fd, " EX_FALERT %s\n",
ep->ex.form->form->name);
break;
case EX_PALERT:
fdprintf(fd, " EX_PALERT %s\n",
ep->ex.pwheel->pwheel->name);
break;
case EX_NOTIFY:
fdprintf(fd, " EX_NOTIFY %s\n",
ep->ex.request->secure->req_id);
break;
default:
fdprintf (fd, " EX_???\n");
break;
}
close(fd);
}
#endif
}