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