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