]> git.ipfire.org Git - thirdparty/cups.git/blame - scheduler/log.c
Merge changes from CUPS 1.5svn-r9717.
[thirdparty/cups.git] / scheduler / log.c
CommitLineData
ef416fc2 1/*
b19ccc9e 2 * "$Id: log.c 7918 2008-09-08 22:03:01Z mike $"
ef416fc2 3 *
10d09e33 4 * Log file routines for the CUPS scheduler.
ef416fc2 5 *
c8fef167 6 * Copyright 2007-2011 by Apple Inc.
b94498cf 7 * Copyright 1997-2007 by Easy Software Products, all rights reserved.
ef416fc2 8 *
9 * These coded instructions, statements, and computer programs are the
bc44d920 10 * property of Apple Inc. and are protected by Federal copyright
11 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
12 * which should have been included with this file. If this file is
13 * file is missing or damaged, see the license at "http://www.cups.org/".
ef416fc2 14 *
15 * Contents:
16 *
22c9029b 17 * cupsdCheckLogFile() - Open/rotate a log file if it needs it.
f7deaa1a 18 * cupsdGetDateTime() - Returns a pointer to a date/time string.
19 * cupsdLogGSSMessage() - Log a GSSAPI error...
75bd9771 20 * cupsdLogJob() - Log a job message.
f7deaa1a 21 * cupsdLogMessage() - Log a message to the error log file.
22 * cupsdLogPage() - Log a page to the page log file.
23 * cupsdLogRequest() - Log an HTTP request in Common Log Format.
75bd9771 24 * cupsdWriteErrorLog() - Write a line to the ErrorLog.
75bd9771 25 * format_log_line() - Format a line for a log file.
ef416fc2 26 */
27
28/*
29 * Include necessary headers...
30 */
31
32#include "cupsd.h"
33#include <stdarg.h>
b423cd4c 34#include <syslog.h>
ef416fc2 35
36
75bd9771
MS
37/*
38 * Local globals...
39 */
40
41static int log_linesize = 0; /* Size of line for output file */
42static char *log_line = NULL; /* Line for output file */
43
44
ef416fc2 45/*
46 * Local functions...
47 */
48
005dd1eb 49static int format_log_line(const char *message, va_list ap);
ef416fc2 50
51
22c9029b
MS
52/*
53 * 'cupsdCheckLogFile()' - Open/rotate a log file if it needs it.
54 */
55
56int /* O - 1 if log file open */
57cupsdCheckLogFile(cups_file_t **lf, /* IO - Log file */
58 const char *logname) /* I - Log filename */
59{
60 char backname[1024], /* Backup log filename */
61 filename[1024], /* Formatted log filename */
62 *ptr; /* Pointer into filename */
63 const char *logptr; /* Pointer into log filename */
64
65
66 /*
67 * See if we have a log file to check...
68 */
69
70 if (!lf || !logname || !logname[0])
71 return (1);
72
73 /*
74 * Format the filename as needed...
75 */
76
77 if (!*lf ||
78 (strncmp(logname, "/dev/", 5) && cupsFileTell(*lf) > MaxLogSize &&
79 MaxLogSize > 0))
80 {
81 /*
82 * Handle format strings...
83 */
84
85 filename[sizeof(filename) - 1] = '\0';
86
87 if (logname[0] != '/')
88 {
89 strlcpy(filename, ServerRoot, sizeof(filename));
90 strlcat(filename, "/", sizeof(filename));
91 }
92 else
93 filename[0] = '\0';
94
95 for (logptr = logname, ptr = filename + strlen(filename);
96 *logptr && ptr < (filename + sizeof(filename) - 1);
97 logptr ++)
98 if (*logptr == '%')
99 {
100 /*
101 * Format spec...
102 */
103
104 logptr ++;
105 if (*logptr == 's')
106 {
107 /*
108 * Insert the server name...
109 */
110
111 strlcpy(ptr, ServerName, sizeof(filename) - (ptr - filename));
112 ptr += strlen(ptr);
113 }
114 else
115 {
116 /*
117 * Otherwise just insert the character...
118 */
119
120 *ptr++ = *logptr;
121 }
122 }
123 else
124 *ptr++ = *logptr;
125
126 *ptr = '\0';
127 }
128
129 /*
130 * See if the log file is open...
131 */
132
133 if (!*lf)
134 {
135 /*
136 * Nope, open the log file...
137 */
138
139 if ((*lf = cupsFileOpen(filename, "a")) == NULL)
140 {
141 /*
142 * If the file is in CUPS_LOGDIR then try to create a missing directory...
143 */
144
145 if (!strncmp(filename, CUPS_LOGDIR, strlen(CUPS_LOGDIR)))
146 {
147 /*
148 * Try updating the permissions of the containing log directory, using
149 * the log file permissions as a basis...
150 */
151
152 int log_dir_perm = 0300 | LogFilePerm;
153 /* LogFilePerm + owner write/search */
154 if (log_dir_perm & 0040)
155 log_dir_perm |= 0010; /* Add group search */
156 if (log_dir_perm & 0004)
157 log_dir_perm |= 0001; /* Add other search */
158
159 cupsdCheckPermissions(CUPS_LOGDIR, NULL, log_dir_perm, RunUser, Group,
160 1, -1);
161
162 *lf = cupsFileOpen(filename, "a");
163 }
164
165 if (*lf == NULL)
166 {
167 syslog(LOG_ERR, "Unable to open log file \"%s\" - %s", filename,
168 strerror(errno));
169
170 if (FatalErrors & CUPSD_FATAL_LOG)
171 cupsdEndProcess(getpid(), 0);
172
173 return (0);
174 }
175 }
176
177 if (strncmp(filename, "/dev/", 5))
178 {
179 /*
180 * Change ownership and permissions of non-device logs...
181 */
182
183 fchown(cupsFileNumber(*lf), RunUser, Group);
184 fchmod(cupsFileNumber(*lf), LogFilePerm);
185 }
186 }
187
188 /*
189 * Do we need to rotate the log?
190 */
191
192 if (strncmp(logname, "/dev/", 5) && cupsFileTell(*lf) > MaxLogSize &&
193 MaxLogSize > 0)
194 {
195 /*
196 * Rotate log file...
197 */
198
199 cupsFileClose(*lf);
200
201 strcpy(backname, filename);
202 strlcat(backname, ".O", sizeof(backname));
203
204 unlink(backname);
205 rename(filename, backname);
206
207 if ((*lf = cupsFileOpen(filename, "a")) == NULL)
208 {
209 syslog(LOG_ERR, "Unable to open log file \"%s\" - %s", filename,
210 strerror(errno));
211
212 if (FatalErrors & CUPSD_FATAL_LOG)
213 cupsdEndProcess(getpid(), 0);
214
215 return (0);
216 }
217
218 /*
219 * Change ownership and permissions of non-device logs...
220 */
221
222 fchown(cupsFileNumber(*lf), RunUser, Group);
223 fchmod(cupsFileNumber(*lf), LogFilePerm);
224 }
225
226 return (1);
227}
228
229
ef416fc2 230/*
231 * 'cupsdGetDateTime()' - Returns a pointer to a date/time string.
232 */
233
234char * /* O - Date/time string */
dfd5680b
MS
235cupsdGetDateTime(struct timeval *t, /* I - Time value or NULL for current */
236 cupsd_time_t format) /* I - Format to use */
ef416fc2 237{
dfd5680b
MS
238 struct timeval curtime; /* Current time value */
239 struct tm *date; /* Date/time value */
240 static struct timeval last_time = { 0, 0 };
241 /* Last time we formatted */
242 static char s[1024]; /* Date/time string */
ef416fc2 243 static const char * const months[12] =/* Months */
244 {
245 "Jan",
246 "Feb",
247 "Mar",
248 "Apr",
249 "May",
250 "Jun",
251 "Jul",
252 "Aug",
253 "Sep",
254 "Oct",
255 "Nov",
256 "Dec"
257 };
258
259
839a51c8
MS
260 /*
261 * Make sure we have a valid time...
262 */
263
264 if (!t)
dfd5680b
MS
265 {
266 gettimeofday(&curtime, NULL);
267 t = &curtime;
268 }
839a51c8 269
dfd5680b
MS
270 if (t->tv_sec != last_time.tv_sec ||
271 (LogTimeFormat == CUPSD_TIME_USECS && t->tv_usec != last_time.tv_usec))
ef416fc2 272 {
dfd5680b 273 last_time = *t;
ef416fc2 274
275 /*
276 * Get the date and time from the UNIX time value, and then format it
277 * into a string. Note that we *can't* use the strftime() function since
278 * it is localized and will seriously confuse automatic programs if the
279 * month names are in the wrong language!
280 *
281 * Also, we use the "timezone" variable that contains the current timezone
282 * offset from GMT in seconds so that we are reporting local time in the
283 * log files. If you want GMT, set the TZ environment variable accordingly
284 * before starting the scheduler.
285 *
286 * (*BSD and Darwin store the timezone offset in the tm structure)
287 */
288
dfd5680b 289 date = localtime(&(t->tv_sec));
ef416fc2 290
dfd5680b
MS
291 if (format == CUPSD_TIME_STANDARD)
292 snprintf(s, sizeof(s), "[%02d/%s/%04d:%02d:%02d:%02d %+03ld%02ld]",
293 date->tm_mday, months[date->tm_mon], 1900 + date->tm_year,
294 date->tm_hour, date->tm_min, date->tm_sec,
295#ifdef HAVE_TM_GMTOFF
296 date->tm_gmtoff / 3600, (date->tm_gmtoff / 60) % 60);
297#else
298 timezone / 3600, (timezone / 60) % 60);
299#endif /* HAVE_TM_GMTOFF */
300 else
301 snprintf(s, sizeof(s), "[%02d/%s/%04d:%02d:%02d:%02d.%06d %+03ld%02ld]",
302 date->tm_mday, months[date->tm_mon], 1900 + date->tm_year,
bf3816c7 303 date->tm_hour, date->tm_min, date->tm_sec, (int)t->tv_usec,
ef416fc2 304#ifdef HAVE_TM_GMTOFF
dfd5680b 305 date->tm_gmtoff / 3600, (date->tm_gmtoff / 60) % 60);
ef416fc2 306#else
dfd5680b 307 timezone / 3600, (timezone / 60) % 60);
ef416fc2 308#endif /* HAVE_TM_GMTOFF */
309 }
310
311 return (s);
312}
313
314
22c9029b
MS
315/*
316 * 'cupsdLogFCMessage()' - Log a file checking message.
317 */
318
319void
320cupsdLogFCMessage(
321 void *context, /* I - Printer (if any) */
322 _cups_fc_result_t result, /* I - Check result */
323 const char *message) /* I - Message to log */
324{
325 cupsd_printer_t *p = (cupsd_printer_t *)context;
326 /* Printer */
327 cupsd_loglevel_t level; /* Log level */
328
329
330 if (result == _CUPS_FILE_CHECK_OK)
331 level = CUPSD_LOG_DEBUG2;
332 else
333 level = CUPSD_LOG_ERROR;
334
335 if (p)
336 {
337 cupsdLogMessage(level, "%s: %s", p->name, message);
338
339 if (result == _CUPS_FILE_CHECK_MISSING ||
340 result == _CUPS_FILE_CHECK_WRONG_TYPE)
341 {
342 strlcpy(p->state_message, message, sizeof(p->state_message));
343
344 if (cupsdSetPrinterReasons(p, "+cups-missing-filter-warning"))
345 cupsdAddEvent(CUPSD_EVENT_PRINTER_STATE, p, NULL, "%s", message);
346 }
347 else if (result == _CUPS_FILE_CHECK_PERMISSIONS)
348 {
349 strlcpy(p->state_message, message, sizeof(p->state_message));
350
351 if (cupsdSetPrinterReasons(p, "+cups-insecure-filter-warning"))
352 cupsdAddEvent(CUPSD_EVENT_PRINTER_STATE, p, NULL, "%s", message);
353 }
354 }
355 else
356 cupsdLogMessage(level, "%s", message);
357}
358
359
f7deaa1a 360#ifdef HAVE_GSSAPI
361/*
362 * 'cupsdLogGSSMessage()' - Log a GSSAPI error...
363 */
364
365int /* O - 1 on success, 0 on error */
366cupsdLogGSSMessage(
367 int level, /* I - Log level */
368 int major_status, /* I - Major GSSAPI status */
369 int minor_status, /* I - Minor GSSAPI status */
370 const char *message, /* I - printf-style message string */
371 ...) /* I - Additional args as needed */
372{
373 OM_uint32 err_major_status, /* Major status code for display */
374 err_minor_status; /* Minor status code for display */
375 OM_uint32 msg_ctx; /* Message context */
376 gss_buffer_desc major_status_string = GSS_C_EMPTY_BUFFER,
377 /* Major status message */
378 minor_status_string = GSS_C_EMPTY_BUFFER;
379 /* Minor status message */
380 int ret; /* Return value */
381
382
383 msg_ctx = 0;
384 err_major_status = gss_display_status(&err_minor_status,
385 major_status,
386 GSS_C_GSS_CODE,
387 GSS_C_NO_OID,
388 &msg_ctx,
389 &major_status_string);
390
391 if (!GSS_ERROR(err_major_status))
1f0275e3
MS
392 gss_display_status(&err_minor_status, minor_status, GSS_C_MECH_CODE,
393 GSS_C_NULL_OID, &msg_ctx, &minor_status_string);
f7deaa1a 394
395 ret = cupsdLogMessage(level, "%s: %s, %s", message,
396 (char *)major_status_string.value,
397 (char *)minor_status_string.value);
398 gss_release_buffer(&err_minor_status, &major_status_string);
399 gss_release_buffer(&err_minor_status, &minor_status_string);
400
401 return (ret);
402}
403#endif /* HAVE_GSSAPI */
404
405
ef416fc2 406/*
75bd9771 407 * 'cupsdLogJob()' - Log a job message.
ef416fc2 408 */
409
410int /* O - 1 on success, 0 on error */
75bd9771
MS
411cupsdLogJob(cupsd_job_t *job, /* I - Job */
412 int level, /* I - Log level */
413 const char *message, /* I - Printf-style message string */
414 ...) /* I - Additional arguments as needed */
ef416fc2 415{
ef416fc2 416 va_list ap; /* Argument pointer */
005dd1eb
MS
417 char jobmsg[1024]; /* Format string for job message */
418 int status; /* Formatting status */
ef416fc2 419
420
421 /*
422 * See if we want to log this message...
423 */
424
94da7e34
MS
425 if (TestConfigFile || !ErrorLog)
426 return (1);
427
178cb736
MS
428 if ((level > LogLevel ||
429 (level == CUPSD_LOG_INFO && LogLevel < CUPSD_LOG_DEBUG)) &&
430 LogDebugHistory <= 0)
2e4ff8af 431 return (1);
2e4ff8af 432
ef416fc2 433 /*
75bd9771 434 * Format and write the log message...
ef416fc2 435 */
436
75bd9771 437 snprintf(jobmsg, sizeof(jobmsg), "[Job %d] %s", job->id, message);
ef416fc2 438
005dd1eb
MS
439 do
440 {
441 va_start(ap, message);
442 status = format_log_line(jobmsg, ap);
443 va_end(ap);
444 }
445 while (status == 0);
178cb736 446
005dd1eb 447 if (status > 0)
178cb736
MS
448 {
449 if ((level > LogLevel ||
450 (level == CUPSD_LOG_INFO && LogLevel < CUPSD_LOG_DEBUG)) &&
451 LogDebugHistory > 0)
452 {
453 /*
454 * Add message to the job history...
455 */
456
457 cupsd_joblog_t *temp; /* Copy of log message */
458
459
460 if ((temp = malloc(sizeof(cupsd_joblog_t) + strlen(log_line))) != NULL)
461 {
462 temp->time = time(NULL);
463 strcpy(temp->message, log_line);
464 }
465
466 if (!job->history)
467 job->history = cupsArrayNew(NULL, NULL);
468
469 if (job->history && temp)
470 {
471 cupsArrayAdd(job->history, temp);
472
473 if (cupsArrayCount(job->history) > LogDebugHistory)
474 {
475 /*
476 * Remove excess messages...
477 */
478
479 temp = cupsArrayFirst(job->history);
480 cupsArrayRemove(job->history, temp);
481 free(temp);
482 }
483 }
484 else if (temp)
485 free(temp);
486
487 return (1);
488 }
489 else if (level <= LogLevel &&
490 (level != CUPSD_LOG_INFO || LogLevel >= CUPSD_LOG_DEBUG))
491 return (cupsdWriteErrorLog(level, log_line));
492 else
493 return (1);
494 }
75bd9771
MS
495 else
496 return (cupsdWriteErrorLog(CUPSD_LOG_ERROR,
497 "Unable to allocate memory for log line!"));
498}
ef416fc2 499
ef416fc2 500
75bd9771
MS
501/*
502 * 'cupsdLogMessage()' - Log a message to the error log file.
503 */
ef416fc2 504
75bd9771
MS
505int /* O - 1 on success, 0 on error */
506cupsdLogMessage(int level, /* I - Log level */
507 const char *message, /* I - printf-style message string */
508 ...) /* I - Additional args as needed */
509{
510 va_list ap; /* Argument pointer */
005dd1eb 511 int status; /* Formatting status */
ef416fc2 512
ef416fc2 513
514 /*
75bd9771 515 * See if we want to log this message...
ef416fc2 516 */
517
75bd9771 518 if (TestConfigFile)
ef416fc2 519 {
75bd9771 520 if (level <= CUPSD_LOG_WARN)
ef416fc2 521 {
75bd9771
MS
522 va_start(ap, message);
523 vfprintf(stderr, message, ap);
524 putc('\n', stderr);
525 va_end(ap);
ef416fc2 526 }
527
75bd9771 528 return (1);
ef416fc2 529 }
530
75bd9771
MS
531 if (level > LogLevel || !ErrorLog)
532 return (1);
ef416fc2 533
534 /*
75bd9771 535 * Format and write the log message...
ef416fc2 536 */
537
005dd1eb
MS
538 do
539 {
540 va_start(ap, message);
541 status = format_log_line(message, ap);
542 va_end(ap);
543 }
544 while (status == 0);
ef416fc2 545
005dd1eb
MS
546 if (status > 0)
547 return (cupsdWriteErrorLog(level, log_line));
75bd9771
MS
548 else
549 return (cupsdWriteErrorLog(CUPSD_LOG_ERROR,
550 "Unable to allocate memory for log line!"));
ef416fc2 551}
552
553
554/*
555 * 'cupsdLogPage()' - Log a page to the page log file.
556 */
557
558int /* O - 1 on success, 0 on error */
559cupsdLogPage(cupsd_job_t *job, /* I - Job being printed */
560 const char *page) /* I - Page being printed */
561{
01ce6322
MS
562 int i; /* Looping var */
563 char buffer[2048], /* Buffer for page log */
564 *bufptr, /* Pointer into buffer */
565 name[256]; /* Attribute name */
566 const char *format, /* Pointer into PageLogFormat */
567 *nameend; /* End of attribute name */
568 ipp_attribute_t *attr; /* Current attribute */
569 int number; /* Page number */
570 char copies[256]; /* Number of copies */
ef416fc2 571
572
01ce6322
MS
573 /*
574 * Format the line going into the page log...
575 */
576
577 if (!PageLogFormat)
578 return (1);
579
580 number = 1;
581 strcpy(copies, "1");
582 sscanf(page, "%d%255s", &number, copies);
583
584 for (format = PageLogFormat, bufptr = buffer; *format; format ++)
585 {
586 if (*format == '%')
587 {
588 format ++;
589
590 switch (*format)
591 {
592 case '%' : /* Literal % */
593 if (bufptr < (buffer + sizeof(buffer) - 1))
594 *bufptr++ = '%';
595 break;
596
597 case 'p' : /* Printer name */
598 strlcpy(bufptr, job->printer->name,
599 sizeof(buffer) - (bufptr - buffer));
600 bufptr += strlen(bufptr);
601 break;
602
603 case 'j' : /* Job ID */
604 snprintf(bufptr, sizeof(buffer) - (bufptr - buffer), "%d", job->id);
605 bufptr += strlen(bufptr);
606 break;
607
608 case 'u' : /* Username */
609 strlcpy(bufptr, job->username ? job->username : "-",
610 sizeof(buffer) - (bufptr - buffer));
611 bufptr += strlen(bufptr);
612 break;
613
614 case 'T' : /* Date and time */
dfd5680b 615 strlcpy(bufptr, cupsdGetDateTime(NULL, LogTimeFormat),
01ce6322
MS
616 sizeof(buffer) - (bufptr - buffer));
617 bufptr += strlen(bufptr);
618 break;
619
620 case 'P' : /* Page number */
621 snprintf(bufptr, sizeof(buffer) - (bufptr - buffer), "%d", number);
622 bufptr += strlen(bufptr);
623 break;
624
625 case 'C' : /* Number of copies */
626 strlcpy(bufptr, copies, sizeof(buffer) - (bufptr - buffer));
627 bufptr += strlen(bufptr);
628 break;
629
630 case '{' : /* {attribute} */
631 if ((nameend = strchr(format, '}')) != NULL &&
632 (nameend - format - 2) < (sizeof(name) - 1))
633 {
634 /*
635 * Pull the name from inside the brackets...
636 */
637
75bd9771
MS
638 memcpy(name, format + 1, nameend - format - 1);
639 name[nameend - format - 1] = '\0';
640
641 format = nameend;
01ce6322
MS
642
643 if ((attr = ippFindAttribute(job->attrs, name,
644 IPP_TAG_ZERO)) != NULL)
645 {
646 /*
647 * Add the attribute value...
648 */
649
01ce6322
MS
650 for (i = 0;
651 i < attr->num_values &&
652 bufptr < (buffer + sizeof(buffer) - 1);
653 i ++)
654 {
655 if (i)
656 *bufptr++ = ',';
657
658 switch (attr->value_tag)
659 {
660 case IPP_TAG_INTEGER :
661 case IPP_TAG_ENUM :
662 snprintf(bufptr, sizeof(buffer) - (bufptr - buffer),
663 "%d", attr->values[i].integer);
664 bufptr += strlen(bufptr);
665 break;
666
667 case IPP_TAG_BOOLEAN :
668 snprintf(bufptr, sizeof(buffer) - (bufptr - buffer),
669 "%d", attr->values[i].boolean);
670 bufptr += strlen(bufptr);
671 break;
672
673 case IPP_TAG_TEXTLANG :
674 case IPP_TAG_NAMELANG :
675 case IPP_TAG_TEXT :
676 case IPP_TAG_NAME :
677 case IPP_TAG_KEYWORD :
678 case IPP_TAG_URI :
679 case IPP_TAG_URISCHEME :
680 case IPP_TAG_CHARSET :
681 case IPP_TAG_LANGUAGE :
682 case IPP_TAG_MIMETYPE :
683 strlcpy(bufptr, attr->values[i].string.text,
684 sizeof(buffer) - (bufptr - buffer));
685 bufptr += strlen(bufptr);
686 break;
687
688 default :
689 strlcpy(bufptr, "???",
690 sizeof(buffer) - (bufptr - buffer));
691 bufptr += strlen(bufptr);
692 break;
693 }
694 }
695 }
696 else if (bufptr < (buffer + sizeof(buffer) - 1))
697 *bufptr++ = '-';
698 break;
699 }
700
701 default :
702 if (bufptr < (buffer + sizeof(buffer) - 2))
703 {
704 *bufptr++ = '%';
705 *bufptr++ = *format;
706 }
707 break;
708 }
709 }
710 else if (bufptr < (buffer + sizeof(buffer) - 1))
711 *bufptr++ = *format;
712 }
ef416fc2 713
01ce6322
MS
714 *bufptr = '\0';
715
ef416fc2 716#ifdef HAVE_VSYSLOG
717 /*
718 * See if we are logging pages via syslog...
719 */
720
721 if (!strcmp(PageLog, "syslog"))
722 {
01ce6322 723 syslog(LOG_INFO, "%s", buffer);
ef416fc2 724
725 return (1);
726 }
727#endif /* HAVE_VSYSLOG */
728
729 /*
730 * Not using syslog; check the log file...
731 */
732
22c9029b 733 if (!cupsdCheckLogFile(&PageFile, PageLog))
ef416fc2 734 return (0);
735
736 /*
737 * Print a page log entry of the form:
738 *
ac884b6a 739 * printer user job-id [DD/MON/YYYY:HH:MM:SS +TTTT] page num-copies \
ef416fc2 740 * billing hostname
741 */
742
01ce6322 743 cupsFilePrintf(PageFile, "%s\n", buffer);
ef416fc2 744 cupsFileFlush(PageFile);
745
746 return (1);
747}
748
749
750/*
751 * 'cupsdLogRequest()' - Log an HTTP request in Common Log Format.
752 */
753
754int /* O - 1 on success, 0 on error */
755cupsdLogRequest(cupsd_client_t *con, /* I - Request to log */
756 http_status_t code) /* I - Response code */
757{
839a51c8 758 char temp[2048]; /* Temporary string for URI */
ef416fc2 759 static const char * const states[] = /* HTTP client states... */
760 {
761 "WAITING",
762 "OPTIONS",
763 "GET",
764 "GET",
765 "HEAD",
766 "POST",
767 "POST",
768 "POST",
769 "PUT",
770 "PUT",
771 "DELETE",
772 "TRACE",
773 "CLOSE",
774 "STATUS"
775 };
776
777
1f0275e3
MS
778 /*
779 * Filter requests as needed...
780 */
781
782 if (AccessLogLevel < CUPSD_ACCESSLOG_ALL)
783 {
784 /*
ba55dc12 785 * Eliminate simple GET, POST, and PUT requests...
1f0275e3
MS
786 */
787
788 if ((con->operation == HTTP_GET &&
789 strncmp(con->uri, "/admin/conf", 11) &&
790 strncmp(con->uri, "/admin/log", 10)) ||
791 (con->operation == HTTP_POST && !con->request &&
792 strncmp(con->uri, "/admin", 6)) ||
ba55dc12
MS
793 (con->operation != HTTP_GET && con->operation != HTTP_POST &&
794 con->operation != HTTP_PUT))
1f0275e3
MS
795 return (1);
796
797 if (con->request && con->response &&
e07d4801
MS
798 (con->response->request.status.status_code < IPP_REDIRECTION_OTHER_SITE ||
799 con->response->request.status.status_code == IPP_NOT_FOUND))
1f0275e3
MS
800 {
801 /*
802 * Check successful requests...
803 */
804
805 ipp_op_t op = con->request->request.op.operation_id;
806 static cupsd_accesslog_t standard_ops[] =
807 {
808 CUPSD_ACCESSLOG_ALL, /* reserved */
809 CUPSD_ACCESSLOG_ALL, /* reserved */
810 CUPSD_ACCESSLOG_ACTIONS,/* Print-Job */
811 CUPSD_ACCESSLOG_ACTIONS,/* Print-URI */
812 CUPSD_ACCESSLOG_ACTIONS,/* Validate-Job */
813 CUPSD_ACCESSLOG_ACTIONS,/* Create-Job */
814 CUPSD_ACCESSLOG_ACTIONS,/* Send-Document */
815 CUPSD_ACCESSLOG_ACTIONS,/* Send-URI */
816 CUPSD_ACCESSLOG_ACTIONS,/* Cancel-Job */
817 CUPSD_ACCESSLOG_ALL, /* Get-Job-Attributes */
818 CUPSD_ACCESSLOG_ALL, /* Get-Jobs */
819 CUPSD_ACCESSLOG_ALL, /* Get-Printer-Attributes */
820 CUPSD_ACCESSLOG_ACTIONS,/* Hold-Job */
821 CUPSD_ACCESSLOG_ACTIONS,/* Release-Job */
822 CUPSD_ACCESSLOG_ACTIONS,/* Restart-Job */
823 CUPSD_ACCESSLOG_ALL, /* reserved */
824 CUPSD_ACCESSLOG_CONFIG, /* Pause-Printer */
825 CUPSD_ACCESSLOG_CONFIG, /* Resume-Printer */
826 CUPSD_ACCESSLOG_CONFIG, /* Purge-Jobs */
827 CUPSD_ACCESSLOG_CONFIG, /* Set-Printer-Attributes */
828 CUPSD_ACCESSLOG_ACTIONS,/* Set-Job-Attributes */
829 CUPSD_ACCESSLOG_CONFIG, /* Get-Printer-Supported-Values */
830 CUPSD_ACCESSLOG_ACTIONS,/* Create-Printer-Subscription */
831 CUPSD_ACCESSLOG_ACTIONS,/* Create-Job-Subscription */
832 CUPSD_ACCESSLOG_ALL, /* Get-Subscription-Attributes */
833 CUPSD_ACCESSLOG_ALL, /* Get-Subscriptions */
834 CUPSD_ACCESSLOG_ACTIONS,/* Renew-Subscription */
835 CUPSD_ACCESSLOG_ACTIONS,/* Cancel-Subscription */
836 CUPSD_ACCESSLOG_ALL, /* Get-Notifications */
837 CUPSD_ACCESSLOG_ACTIONS,/* Send-Notifications */
838 CUPSD_ACCESSLOG_ALL, /* reserved */
839 CUPSD_ACCESSLOG_ALL, /* reserved */
840 CUPSD_ACCESSLOG_ALL, /* reserved */
841 CUPSD_ACCESSLOG_ALL, /* Get-Print-Support-Files */
842 CUPSD_ACCESSLOG_CONFIG, /* Enable-Printer */
843 CUPSD_ACCESSLOG_CONFIG, /* Disable-Printer */
844 CUPSD_ACCESSLOG_CONFIG, /* Pause-Printer-After-Current-Job */
845 CUPSD_ACCESSLOG_ACTIONS,/* Hold-New-Jobs */
846 CUPSD_ACCESSLOG_ACTIONS,/* Release-Held-New-Jobs */
847 CUPSD_ACCESSLOG_CONFIG, /* Deactivate-Printer */
848 CUPSD_ACCESSLOG_CONFIG, /* Activate-Printer */
849 CUPSD_ACCESSLOG_CONFIG, /* Restart-Printer */
850 CUPSD_ACCESSLOG_CONFIG, /* Shutdown-Printer */
851 CUPSD_ACCESSLOG_CONFIG, /* Startup-Printer */
852 CUPSD_ACCESSLOG_ACTIONS,/* Reprocess-Job */
853 CUPSD_ACCESSLOG_ACTIONS,/* Cancel-Current-Job */
854 CUPSD_ACCESSLOG_ACTIONS,/* Suspend-Current-Job */
855 CUPSD_ACCESSLOG_ACTIONS,/* Resume-Job */
856 CUPSD_ACCESSLOG_ACTIONS,/* Promote-Job */
857 CUPSD_ACCESSLOG_ACTIONS /* Schedule-Job-After */
858 };
859 static cupsd_accesslog_t cups_ops[] =
860 {
861 CUPSD_ACCESSLOG_ALL, /* CUPS-Get-Default */
862 CUPSD_ACCESSLOG_ALL, /* CUPS-Get-Printers */
863 CUPSD_ACCESSLOG_CONFIG, /* CUPS-Add-Modify-Printer */
864 CUPSD_ACCESSLOG_CONFIG, /* CUPS-Delete-Printer */
865 CUPSD_ACCESSLOG_ALL, /* CUPS-Get-Classes */
866 CUPSD_ACCESSLOG_CONFIG, /* CUPS-Add-Modify-Class */
867 CUPSD_ACCESSLOG_CONFIG, /* CUPS-Delete-Class */
868 CUPSD_ACCESSLOG_CONFIG, /* CUPS-Accept-Jobs */
869 CUPSD_ACCESSLOG_CONFIG, /* CUPS-Reject-Jobs */
870 CUPSD_ACCESSLOG_CONFIG, /* CUPS-Set-Default */
871 CUPSD_ACCESSLOG_CONFIG, /* CUPS-Get-Devices */
872 CUPSD_ACCESSLOG_CONFIG, /* CUPS-Get-PPDs */
873 CUPSD_ACCESSLOG_ACTIONS,/* CUPS-Move-Job */
874 CUPSD_ACCESSLOG_ACTIONS,/* CUPS-Authenticate-Job */
875 CUPSD_ACCESSLOG_ALL /* CUPS-Get-PPD */
876 };
877
878
879 if ((op <= IPP_SCHEDULE_JOB_AFTER && standard_ops[op] > AccessLogLevel) ||
880 (op >= CUPS_GET_DEFAULT && op <= CUPS_GET_PPD &&
881 cups_ops[op - CUPS_GET_DEFAULT] > AccessLogLevel))
882 return (1);
883 }
884 }
885
ef416fc2 886#ifdef HAVE_VSYSLOG
887 /*
888 * See if we are logging accesses via syslog...
889 */
890
891 if (!strcmp(AccessLog, "syslog"))
892 {
2abf387c 893 syslog(LOG_INFO,
894 "REQUEST %s - %s \"%s %s HTTP/%d.%d\" %d " CUPS_LLFMT " %s %s\n",
ef416fc2 895 con->http.hostname, con->username[0] != '\0' ? con->username : "-",
839a51c8 896 states[con->operation], _httpEncodeURI(temp, con->uri, sizeof(temp)),
ef416fc2 897 con->http.version / 100, con->http.version % 100,
2abf387c 898 code, CUPS_LLCAST con->bytes,
899 con->request ?
900 ippOpString(con->request->request.op.operation_id) : "-",
901 con->response ?
902 ippErrorString(con->response->request.status.status_code) : "-");
ef416fc2 903
904 return (1);
905 }
906#endif /* HAVE_VSYSLOG */
907
908 /*
909 * Not using syslog; check the log file...
910 */
911
22c9029b 912 if (!cupsdCheckLogFile(&AccessFile, AccessLog))
ef416fc2 913 return (0);
914
915 /*
916 * Write a log of the request in "common log format"...
917 */
918
919 cupsFilePrintf(AccessFile,
920 "%s - %s %s \"%s %s HTTP/%d.%d\" %d " CUPS_LLFMT " %s %s\n",
c8fef167
MS
921 con->http.hostname,
922 con->username[0] != '\0' ? con->username : "-",
dfd5680b
MS
923 cupsdGetDateTime(&(con->start), LogTimeFormat),
924 states[con->operation],
839a51c8 925 _httpEncodeURI(temp, con->uri, sizeof(temp)),
ef416fc2 926 con->http.version / 100, con->http.version % 100,
927 code, CUPS_LLCAST con->bytes,
928 con->request ?
929 ippOpString(con->request->request.op.operation_id) : "-",
930 con->response ?
931 ippErrorString(con->response->request.status.status_code) :
932 "-");
933
934 cupsFileFlush(AccessFile);
935
936 return (1);
937}
938
939
75bd9771
MS
940/*
941 * 'cupsdWriteErrorLog()' - Write a line to the ErrorLog.
942 */
943
944int /* O - 1 on success, 0 on failure */
945cupsdWriteErrorLog(int level, /* I - Log level */
946 const char *message) /* I - Message string */
947{
948 static const char levels[] = /* Log levels... */
949 {
950 ' ',
951 'X',
952 'A',
953 'C',
954 'E',
955 'W',
956 'N',
957 'I',
958 'D',
959 'd'
960 };
961#ifdef HAVE_VSYSLOG
962 static const int syslevels[] = /* SYSLOG levels... */
963 {
964 0,
965 LOG_EMERG,
966 LOG_ALERT,
967 LOG_CRIT,
968 LOG_ERR,
969 LOG_WARNING,
970 LOG_NOTICE,
971 LOG_INFO,
972 LOG_DEBUG,
973 LOG_DEBUG
974 };
975#endif /* HAVE_VSYSLOG */
976
977
978#ifdef HAVE_VSYSLOG
979 /*
980 * See if we are logging errors via syslog...
981 */
982
983 if (!strcmp(ErrorLog, "syslog"))
984 {
985 syslog(syslevels[level], "%s", message);
986 return (1);
987 }
988#endif /* HAVE_VSYSLOG */
989
990 /*
991 * Not using syslog; check the log file...
992 */
993
22c9029b 994 if (!cupsdCheckLogFile(&ErrorFile, ErrorLog))
75bd9771
MS
995 return (0);
996
997 /*
998 * Write the log message...
999 */
1000
1001 cupsFilePrintf(ErrorFile, "%c %s %s\n", levels[level],
dfd5680b 1002 cupsdGetDateTime(NULL, LogTimeFormat), message);
75bd9771
MS
1003 cupsFileFlush(ErrorFile);
1004
1005 return (1);
1006}
1007
1008
ef416fc2 1009/*
75bd9771
MS
1010 * 'format_log_line()' - Format a line for a log file.
1011 *
1012 * This function resizes a global string buffer as needed. Each call returns
1013 * a pointer to this buffer, so the contents are only good until the next call
1014 * to format_log_line()...
1015 */
1016
005dd1eb 1017static int /* O - -1 for fatal, 0 for retry, 1 for success */
75bd9771
MS
1018format_log_line(const char *message, /* I - Printf-style format string */
1019 va_list ap) /* I - Argument list */
1020{
005dd1eb 1021 int len; /* Length of formatted line */
75bd9771
MS
1022
1023
1024 /*
1025 * Allocate the line buffer as needed...
1026 */
1027
1028 if (!log_linesize)
1029 {
1030 log_linesize = 8192;
1031 log_line = malloc(log_linesize);
1032
1033 if (!log_line)
005dd1eb 1034 return (-1);
75bd9771
MS
1035 }
1036
1037 /*
1038 * Format the log message...
1039 */
1040
1041 len = vsnprintf(log_line, log_linesize, message, ap);
1042
1043 /*
1044 * Resize the buffer as needed...
1045 */
1046
ed6e7faf 1047 if (len >= log_linesize && log_linesize < 65536)
75bd9771
MS
1048 {
1049 char *temp; /* Temporary string pointer */
1050
1051
1052 len ++;
1053
1054 if (len < 8192)
1055 len = 8192;
1056 else if (len > 65536)
1057 len = 65536;
1058
1059 temp = realloc(log_line, len);
1060
1061 if (temp)
1062 {
1063 log_line = temp;
1064 log_linesize = len;
75bd9771 1065
005dd1eb
MS
1066 return (0);
1067 }
75bd9771
MS
1068 }
1069
005dd1eb 1070 return (1);
75bd9771
MS
1071}
1072
1073
1074/*
b19ccc9e 1075 * End of "$Id: log.c 7918 2008-09-08 22:03:01Z mike $".
ef416fc2 1076 */