2 * "$Id: log.c 7918 2008-09-08 22:03:01Z mike $"
4 * Log file routines for the CUPS scheduler.
6 * Copyright 2007-2011 by Apple Inc.
7 * Copyright 1997-2007 by Easy Software Products, all rights reserved.
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/".
17 * cupsdCheckLogFile() - Open/rotate a log file if it needs it.
18 * cupsdGetDateTime() - Returns a pointer to a date/time string.
19 * cupsdLogGSSMessage() - Log a GSSAPI error...
20 * cupsdLogJob() - Log a job message.
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.
24 * cupsdWriteErrorLog() - Write a line to the ErrorLog.
25 * format_log_line() - Format a line for a log file.
29 * Include necessary headers...
41 static int log_linesize
= 0; /* Size of line for output file */
42 static char *log_line
= NULL
; /* Line for output file */
49 static int format_log_line(const char *message
, va_list ap
);
53 * 'cupsdCheckLogFile()' - Open/rotate a log file if it needs it.
56 int /* O - 1 if log file open */
57 cupsdCheckLogFile(cups_file_t
**lf
, /* IO - Log file */
58 const char *logname
) /* I - Log filename */
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 */
67 * See if we have a log file to check...
70 if (!lf
|| !logname
|| !logname
[0])
74 * Format the filename as needed...
78 (strncmp(logname
, "/dev/", 5) && cupsFileTell(*lf
) > MaxLogSize
&&
82 * Handle format strings...
85 filename
[sizeof(filename
) - 1] = '\0';
87 if (logname
[0] != '/')
89 strlcpy(filename
, ServerRoot
, sizeof(filename
));
90 strlcat(filename
, "/", sizeof(filename
));
95 for (logptr
= logname
, ptr
= filename
+ strlen(filename
);
96 *logptr
&& ptr
< (filename
+ sizeof(filename
) - 1);
108 * Insert the server name...
111 strlcpy(ptr
, ServerName
, sizeof(filename
) - (ptr
- filename
));
117 * Otherwise just insert the character...
130 * See if the log file is open...
136 * Nope, open the log file...
139 if ((*lf
= cupsFileOpen(filename
, "a")) == NULL
)
142 * If the file is in CUPS_LOGDIR then try to create a missing directory...
145 if (!strncmp(filename
, CUPS_LOGDIR
, strlen(CUPS_LOGDIR
)))
148 * Try updating the permissions of the containing log directory, using
149 * the log file permissions as a basis...
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 */
159 cupsdCheckPermissions(CUPS_LOGDIR
, NULL
, log_dir_perm
, RunUser
, Group
,
162 *lf
= cupsFileOpen(filename
, "a");
167 syslog(LOG_ERR
, "Unable to open log file \"%s\" - %s", filename
,
170 if (FatalErrors
& CUPSD_FATAL_LOG
)
171 cupsdEndProcess(getpid(), 0);
177 if (strncmp(filename
, "/dev/", 5))
180 * Change ownership and permissions of non-device logs...
183 fchown(cupsFileNumber(*lf
), RunUser
, Group
);
184 fchmod(cupsFileNumber(*lf
), LogFilePerm
);
189 * Do we need to rotate the log?
192 if (strncmp(logname
, "/dev/", 5) && cupsFileTell(*lf
) > MaxLogSize
&&
201 strcpy(backname
, filename
);
202 strlcat(backname
, ".O", sizeof(backname
));
205 rename(filename
, backname
);
207 if ((*lf
= cupsFileOpen(filename
, "a")) == NULL
)
209 syslog(LOG_ERR
, "Unable to open log file \"%s\" - %s", filename
,
212 if (FatalErrors
& CUPSD_FATAL_LOG
)
213 cupsdEndProcess(getpid(), 0);
219 * Change ownership and permissions of non-device logs...
222 fchown(cupsFileNumber(*lf
), RunUser
, Group
);
223 fchmod(cupsFileNumber(*lf
), LogFilePerm
);
231 * 'cupsdGetDateTime()' - Returns a pointer to a date/time string.
234 char * /* O - Date/time string */
235 cupsdGetDateTime(struct timeval
*t
, /* I - Time value or NULL for current */
236 cupsd_time_t format
) /* I - Format to use */
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 */
243 static const char * const months
[12] =/* Months */
261 * Make sure we have a valid time...
266 gettimeofday(&curtime
, NULL
);
270 if (t
->tv_sec
!= last_time
.tv_sec
||
271 (LogTimeFormat
== CUPSD_TIME_USECS
&& t
->tv_usec
!= last_time
.tv_usec
))
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!
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.
286 * (*BSD and Darwin store the timezone offset in the tm structure)
289 date
= localtime(&(t
->tv_sec
));
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);
298 timezone
/ 3600, (timezone
/ 60) % 60);
299 #endif /* HAVE_TM_GMTOFF */
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
,
303 date
->tm_hour
, date
->tm_min
, date
->tm_sec
, (int)t
->tv_usec
,
304 #ifdef HAVE_TM_GMTOFF
305 date
->tm_gmtoff
/ 3600, (date
->tm_gmtoff
/ 60) % 60);
307 timezone
/ 3600, (timezone
/ 60) % 60);
308 #endif /* HAVE_TM_GMTOFF */
316 * 'cupsdLogFCMessage()' - Log a file checking message.
321 void *context
, /* I - Printer (if any) */
322 _cups_fc_result_t result
, /* I - Check result */
323 const char *message
) /* I - Message to log */
325 cupsd_printer_t
*p
= (cupsd_printer_t
*)context
;
327 cupsd_loglevel_t level
; /* Log level */
330 if (result
== _CUPS_FILE_CHECK_OK
)
331 level
= CUPSD_LOG_DEBUG2
;
333 level
= CUPSD_LOG_ERROR
;
337 cupsdLogMessage(level
, "%s: %s", p
->name
, message
);
339 if (result
== _CUPS_FILE_CHECK_MISSING
||
340 result
== _CUPS_FILE_CHECK_WRONG_TYPE
)
342 strlcpy(p
->state_message
, message
, sizeof(p
->state_message
));
344 if (cupsdSetPrinterReasons(p
, "+cups-missing-filter-warning"))
345 cupsdAddEvent(CUPSD_EVENT_PRINTER_STATE
, p
, NULL
, "%s", message
);
347 else if (result
== _CUPS_FILE_CHECK_PERMISSIONS
||
348 result
== _CUPS_FILE_CHECK_RELATIVE_PATH
)
350 strlcpy(p
->state_message
, message
, sizeof(p
->state_message
));
352 if (cupsdSetPrinterReasons(p
, "+cups-insecure-filter-warning"))
353 cupsdAddEvent(CUPSD_EVENT_PRINTER_STATE
, p
, NULL
, "%s", message
);
357 cupsdLogMessage(level
, "%s", message
);
363 * 'cupsdLogGSSMessage()' - Log a GSSAPI error...
366 int /* O - 1 on success, 0 on error */
368 int level
, /* I - Log level */
369 int major_status
, /* I - Major GSSAPI status */
370 int minor_status
, /* I - Minor GSSAPI status */
371 const char *message
, /* I - printf-style message string */
372 ...) /* I - Additional args as needed */
374 OM_uint32 err_major_status
, /* Major status code for display */
375 err_minor_status
; /* Minor status code for display */
376 OM_uint32 msg_ctx
; /* Message context */
377 gss_buffer_desc major_status_string
= GSS_C_EMPTY_BUFFER
,
378 /* Major status message */
379 minor_status_string
= GSS_C_EMPTY_BUFFER
;
380 /* Minor status message */
381 int ret
; /* Return value */
385 err_major_status
= gss_display_status(&err_minor_status
,
390 &major_status_string
);
392 if (!GSS_ERROR(err_major_status
))
393 gss_display_status(&err_minor_status
, minor_status
, GSS_C_MECH_CODE
,
394 GSS_C_NULL_OID
, &msg_ctx
, &minor_status_string
);
396 ret
= cupsdLogMessage(level
, "%s: %s, %s", message
,
397 (char *)major_status_string
.value
,
398 (char *)minor_status_string
.value
);
399 gss_release_buffer(&err_minor_status
, &major_status_string
);
400 gss_release_buffer(&err_minor_status
, &minor_status_string
);
404 #endif /* HAVE_GSSAPI */
408 * 'cupsdLogJob()' - Log a job message.
411 int /* O - 1 on success, 0 on error */
412 cupsdLogJob(cupsd_job_t
*job
, /* I - Job */
413 int level
, /* I - Log level */
414 const char *message
, /* I - Printf-style message string */
415 ...) /* I - Additional arguments as needed */
417 va_list ap
; /* Argument pointer */
418 char jobmsg
[1024]; /* Format string for job message */
419 int status
; /* Formatting status */
423 * See if we want to log this message...
426 if (TestConfigFile
|| !ErrorLog
)
429 if ((level
> LogLevel
||
430 (level
== CUPSD_LOG_INFO
&& LogLevel
< CUPSD_LOG_DEBUG
)) &&
431 LogDebugHistory
<= 0)
435 * Format and write the log message...
438 snprintf(jobmsg
, sizeof(jobmsg
), "[Job %d] %s", job
->id
, message
);
442 va_start(ap
, message
);
443 status
= format_log_line(jobmsg
, ap
);
450 if ((level
> LogLevel
||
451 (level
== CUPSD_LOG_INFO
&& LogLevel
< CUPSD_LOG_DEBUG
)) &&
455 * Add message to the job history...
458 cupsd_joblog_t
*temp
; /* Copy of log message */
461 if ((temp
= malloc(sizeof(cupsd_joblog_t
) + strlen(log_line
))) != NULL
)
463 temp
->time
= time(NULL
);
464 strcpy(temp
->message
, log_line
);
468 job
->history
= cupsArrayNew(NULL
, NULL
);
470 if (job
->history
&& temp
)
472 cupsArrayAdd(job
->history
, temp
);
474 if (cupsArrayCount(job
->history
) > LogDebugHistory
)
477 * Remove excess messages...
480 temp
= cupsArrayFirst(job
->history
);
481 cupsArrayRemove(job
->history
, temp
);
490 else if (level
<= LogLevel
&&
491 (level
!= CUPSD_LOG_INFO
|| LogLevel
>= CUPSD_LOG_DEBUG
))
492 return (cupsdWriteErrorLog(level
, log_line
));
497 return (cupsdWriteErrorLog(CUPSD_LOG_ERROR
,
498 "Unable to allocate memory for log line!"));
503 * 'cupsdLogMessage()' - Log a message to the error log file.
506 int /* O - 1 on success, 0 on error */
507 cupsdLogMessage(int level
, /* I - Log level */
508 const char *message
, /* I - printf-style message string */
509 ...) /* I - Additional args as needed */
511 va_list ap
; /* Argument pointer */
512 int status
; /* Formatting status */
516 * See if we want to log this message...
519 if ((TestConfigFile
|| !ErrorLog
) && level
<= CUPSD_LOG_WARN
)
521 va_start(ap
, message
);
522 vfprintf(stderr
, message
, ap
);
529 if (level
> LogLevel
|| !ErrorLog
)
533 * Format and write the log message...
538 va_start(ap
, message
);
539 status
= format_log_line(message
, ap
);
545 return (cupsdWriteErrorLog(level
, log_line
));
547 return (cupsdWriteErrorLog(CUPSD_LOG_ERROR
,
548 "Unable to allocate memory for log line!"));
553 * 'cupsdLogPage()' - Log a page to the page log file.
556 int /* O - 1 on success, 0 on error */
557 cupsdLogPage(cupsd_job_t
*job
, /* I - Job being printed */
558 const char *page
) /* I - Page being printed */
560 int i
; /* Looping var */
561 char buffer
[2048], /* Buffer for page log */
562 *bufptr
, /* Pointer into buffer */
563 name
[256]; /* Attribute name */
564 const char *format
, /* Pointer into PageLogFormat */
565 *nameend
; /* End of attribute name */
566 ipp_attribute_t
*attr
; /* Current attribute */
567 char number
[256]; /* Page number */
568 int copies
; /* Number of copies */
572 * Format the line going into the page log...
580 sscanf(page
, "%255s%d", number
, &copies
);
582 for (format
= PageLogFormat
, bufptr
= buffer
; *format
; format
++)
590 case '%' : /* Literal % */
591 if (bufptr
< (buffer
+ sizeof(buffer
) - 1))
595 case 'p' : /* Printer name */
596 strlcpy(bufptr
, job
->printer
->name
,
597 sizeof(buffer
) - (bufptr
- buffer
));
598 bufptr
+= strlen(bufptr
);
601 case 'j' : /* Job ID */
602 snprintf(bufptr
, sizeof(buffer
) - (bufptr
- buffer
), "%d", job
->id
);
603 bufptr
+= strlen(bufptr
);
606 case 'u' : /* Username */
607 strlcpy(bufptr
, job
->username
? job
->username
: "-",
608 sizeof(buffer
) - (bufptr
- buffer
));
609 bufptr
+= strlen(bufptr
);
612 case 'T' : /* Date and time */
613 strlcpy(bufptr
, cupsdGetDateTime(NULL
, LogTimeFormat
),
614 sizeof(buffer
) - (bufptr
- buffer
));
615 bufptr
+= strlen(bufptr
);
618 case 'P' : /* Page number */
619 strlcpy(bufptr
, number
, sizeof(buffer
) - (bufptr
- buffer
));
620 bufptr
+= strlen(bufptr
);
623 case 'C' : /* Number of copies */
624 snprintf(bufptr
, sizeof(buffer
) - (bufptr
- buffer
), "%d", copies
);
625 bufptr
+= strlen(bufptr
);
628 case '{' : /* {attribute} */
629 if ((nameend
= strchr(format
, '}')) != NULL
&&
630 (nameend
- format
- 2) < (sizeof(name
) - 1))
633 * Pull the name from inside the brackets...
636 memcpy(name
, format
+ 1, nameend
- format
- 1);
637 name
[nameend
- format
- 1] = '\0';
641 if ((attr
= ippFindAttribute(job
->attrs
, name
,
642 IPP_TAG_ZERO
)) != NULL
)
645 * Add the attribute value...
649 i
< attr
->num_values
&&
650 bufptr
< (buffer
+ sizeof(buffer
) - 1);
656 switch (attr
->value_tag
)
658 case IPP_TAG_INTEGER
:
660 snprintf(bufptr
, sizeof(buffer
) - (bufptr
- buffer
),
661 "%d", attr
->values
[i
].integer
);
662 bufptr
+= strlen(bufptr
);
665 case IPP_TAG_BOOLEAN
:
666 snprintf(bufptr
, sizeof(buffer
) - (bufptr
- buffer
),
667 "%d", attr
->values
[i
].boolean
);
668 bufptr
+= strlen(bufptr
);
671 case IPP_TAG_TEXTLANG
:
672 case IPP_TAG_NAMELANG
:
675 case IPP_TAG_KEYWORD
:
677 case IPP_TAG_URISCHEME
:
678 case IPP_TAG_CHARSET
:
679 case IPP_TAG_LANGUAGE
:
680 case IPP_TAG_MIMETYPE
:
681 strlcpy(bufptr
, attr
->values
[i
].string
.text
,
682 sizeof(buffer
) - (bufptr
- buffer
));
683 bufptr
+= strlen(bufptr
);
687 strlcpy(bufptr
, "???",
688 sizeof(buffer
) - (bufptr
- buffer
));
689 bufptr
+= strlen(bufptr
);
694 else if (bufptr
< (buffer
+ sizeof(buffer
) - 1))
700 if (bufptr
< (buffer
+ sizeof(buffer
) - 2))
708 else if (bufptr
< (buffer
+ sizeof(buffer
) - 1))
716 * See if we are logging pages via syslog...
719 if (!strcmp(PageLog
, "syslog"))
721 syslog(LOG_INFO
, "%s", buffer
);
725 #endif /* HAVE_VSYSLOG */
728 * Not using syslog; check the log file...
731 if (!cupsdCheckLogFile(&PageFile
, PageLog
))
735 * Print a page log entry of the form:
737 * printer user job-id [DD/MON/YYYY:HH:MM:SS +TTTT] page num-copies \
741 cupsFilePrintf(PageFile
, "%s\n", buffer
);
742 cupsFileFlush(PageFile
);
749 * 'cupsdLogRequest()' - Log an HTTP request in Common Log Format.
752 int /* O - 1 on success, 0 on error */
753 cupsdLogRequest(cupsd_client_t
*con
, /* I - Request to log */
754 http_status_t code
) /* I - Response code */
756 char temp
[2048]; /* Temporary string for URI */
757 static const char * const states
[] = /* HTTP client states... */
777 * Filter requests as needed...
780 if (AccessLogLevel
< CUPSD_ACCESSLOG_ALL
)
783 * Eliminate simple GET, POST, and PUT requests...
786 if ((con
->operation
== HTTP_GET
&&
787 strncmp(con
->uri
, "/admin/conf", 11) &&
788 strncmp(con
->uri
, "/admin/log", 10)) ||
789 (con
->operation
== HTTP_POST
&& !con
->request
&&
790 strncmp(con
->uri
, "/admin", 6)) ||
791 (con
->operation
!= HTTP_GET
&& con
->operation
!= HTTP_POST
&&
792 con
->operation
!= HTTP_PUT
))
795 if (con
->request
&& con
->response
&&
796 (con
->response
->request
.status
.status_code
< IPP_REDIRECTION_OTHER_SITE
||
797 con
->response
->request
.status
.status_code
== IPP_NOT_FOUND
))
800 * Check successful requests...
803 ipp_op_t op
= con
->request
->request
.op
.operation_id
;
804 static cupsd_accesslog_t standard_ops
[] =
806 CUPSD_ACCESSLOG_ALL
, /* reserved */
807 CUPSD_ACCESSLOG_ALL
, /* reserved */
808 CUPSD_ACCESSLOG_ACTIONS
,/* Print-Job */
809 CUPSD_ACCESSLOG_ACTIONS
,/* Print-URI */
810 CUPSD_ACCESSLOG_ACTIONS
,/* Validate-Job */
811 CUPSD_ACCESSLOG_ACTIONS
,/* Create-Job */
812 CUPSD_ACCESSLOG_ACTIONS
,/* Send-Document */
813 CUPSD_ACCESSLOG_ACTIONS
,/* Send-URI */
814 CUPSD_ACCESSLOG_ACTIONS
,/* Cancel-Job */
815 CUPSD_ACCESSLOG_ALL
, /* Get-Job-Attributes */
816 CUPSD_ACCESSLOG_ALL
, /* Get-Jobs */
817 CUPSD_ACCESSLOG_ALL
, /* Get-Printer-Attributes */
818 CUPSD_ACCESSLOG_ACTIONS
,/* Hold-Job */
819 CUPSD_ACCESSLOG_ACTIONS
,/* Release-Job */
820 CUPSD_ACCESSLOG_ACTIONS
,/* Restart-Job */
821 CUPSD_ACCESSLOG_ALL
, /* reserved */
822 CUPSD_ACCESSLOG_CONFIG
, /* Pause-Printer */
823 CUPSD_ACCESSLOG_CONFIG
, /* Resume-Printer */
824 CUPSD_ACCESSLOG_CONFIG
, /* Purge-Jobs */
825 CUPSD_ACCESSLOG_CONFIG
, /* Set-Printer-Attributes */
826 CUPSD_ACCESSLOG_ACTIONS
,/* Set-Job-Attributes */
827 CUPSD_ACCESSLOG_CONFIG
, /* Get-Printer-Supported-Values */
828 CUPSD_ACCESSLOG_ACTIONS
,/* Create-Printer-Subscription */
829 CUPSD_ACCESSLOG_ACTIONS
,/* Create-Job-Subscription */
830 CUPSD_ACCESSLOG_ALL
, /* Get-Subscription-Attributes */
831 CUPSD_ACCESSLOG_ALL
, /* Get-Subscriptions */
832 CUPSD_ACCESSLOG_ACTIONS
,/* Renew-Subscription */
833 CUPSD_ACCESSLOG_ACTIONS
,/* Cancel-Subscription */
834 CUPSD_ACCESSLOG_ALL
, /* Get-Notifications */
835 CUPSD_ACCESSLOG_ACTIONS
,/* Send-Notifications */
836 CUPSD_ACCESSLOG_ALL
, /* reserved */
837 CUPSD_ACCESSLOG_ALL
, /* reserved */
838 CUPSD_ACCESSLOG_ALL
, /* reserved */
839 CUPSD_ACCESSLOG_ALL
, /* Get-Print-Support-Files */
840 CUPSD_ACCESSLOG_CONFIG
, /* Enable-Printer */
841 CUPSD_ACCESSLOG_CONFIG
, /* Disable-Printer */
842 CUPSD_ACCESSLOG_CONFIG
, /* Pause-Printer-After-Current-Job */
843 CUPSD_ACCESSLOG_ACTIONS
,/* Hold-New-Jobs */
844 CUPSD_ACCESSLOG_ACTIONS
,/* Release-Held-New-Jobs */
845 CUPSD_ACCESSLOG_CONFIG
, /* Deactivate-Printer */
846 CUPSD_ACCESSLOG_CONFIG
, /* Activate-Printer */
847 CUPSD_ACCESSLOG_CONFIG
, /* Restart-Printer */
848 CUPSD_ACCESSLOG_CONFIG
, /* Shutdown-Printer */
849 CUPSD_ACCESSLOG_CONFIG
, /* Startup-Printer */
850 CUPSD_ACCESSLOG_ACTIONS
,/* Reprocess-Job */
851 CUPSD_ACCESSLOG_ACTIONS
,/* Cancel-Current-Job */
852 CUPSD_ACCESSLOG_ACTIONS
,/* Suspend-Current-Job */
853 CUPSD_ACCESSLOG_ACTIONS
,/* Resume-Job */
854 CUPSD_ACCESSLOG_ACTIONS
,/* Promote-Job */
855 CUPSD_ACCESSLOG_ACTIONS
/* Schedule-Job-After */
857 static cupsd_accesslog_t cups_ops
[] =
859 CUPSD_ACCESSLOG_ALL
, /* CUPS-Get-Default */
860 CUPSD_ACCESSLOG_ALL
, /* CUPS-Get-Printers */
861 CUPSD_ACCESSLOG_CONFIG
, /* CUPS-Add-Modify-Printer */
862 CUPSD_ACCESSLOG_CONFIG
, /* CUPS-Delete-Printer */
863 CUPSD_ACCESSLOG_ALL
, /* CUPS-Get-Classes */
864 CUPSD_ACCESSLOG_CONFIG
, /* CUPS-Add-Modify-Class */
865 CUPSD_ACCESSLOG_CONFIG
, /* CUPS-Delete-Class */
866 CUPSD_ACCESSLOG_CONFIG
, /* CUPS-Accept-Jobs */
867 CUPSD_ACCESSLOG_CONFIG
, /* CUPS-Reject-Jobs */
868 CUPSD_ACCESSLOG_CONFIG
, /* CUPS-Set-Default */
869 CUPSD_ACCESSLOG_CONFIG
, /* CUPS-Get-Devices */
870 CUPSD_ACCESSLOG_CONFIG
, /* CUPS-Get-PPDs */
871 CUPSD_ACCESSLOG_ACTIONS
,/* CUPS-Move-Job */
872 CUPSD_ACCESSLOG_ACTIONS
,/* CUPS-Authenticate-Job */
873 CUPSD_ACCESSLOG_ALL
/* CUPS-Get-PPD */
877 if ((op
<= IPP_SCHEDULE_JOB_AFTER
&& standard_ops
[op
] > AccessLogLevel
) ||
878 (op
>= CUPS_GET_DEFAULT
&& op
<= CUPS_GET_PPD
&&
879 cups_ops
[op
- CUPS_GET_DEFAULT
] > AccessLogLevel
))
886 * See if we are logging accesses via syslog...
889 if (!strcmp(AccessLog
, "syslog"))
892 "REQUEST %s - %s \"%s %s HTTP/%d.%d\" %d " CUPS_LLFMT
" %s %s\n",
893 con
->http
.hostname
, con
->username
[0] != '\0' ? con
->username
: "-",
894 states
[con
->operation
], _httpEncodeURI(temp
, con
->uri
, sizeof(temp
)),
895 con
->http
.version
/ 100, con
->http
.version
% 100,
896 code
, CUPS_LLCAST con
->bytes
,
898 ippOpString(con
->request
->request
.op
.operation_id
) : "-",
900 ippErrorString(con
->response
->request
.status
.status_code
) : "-");
904 #endif /* HAVE_VSYSLOG */
907 * Not using syslog; check the log file...
910 if (!cupsdCheckLogFile(&AccessFile
, AccessLog
))
914 * Write a log of the request in "common log format"...
917 cupsFilePrintf(AccessFile
,
918 "%s - %s %s \"%s %s HTTP/%d.%d\" %d " CUPS_LLFMT
" %s %s\n",
920 con
->username
[0] != '\0' ? con
->username
: "-",
921 cupsdGetDateTime(&(con
->start
), LogTimeFormat
),
922 states
[con
->operation
],
923 _httpEncodeURI(temp
, con
->uri
, sizeof(temp
)),
924 con
->http
.version
/ 100, con
->http
.version
% 100,
925 code
, CUPS_LLCAST con
->bytes
,
927 ippOpString(con
->request
->request
.op
.operation_id
) : "-",
929 ippErrorString(con
->response
->request
.status
.status_code
) :
932 cupsFileFlush(AccessFile
);
939 * 'cupsdWriteErrorLog()' - Write a line to the ErrorLog.
942 int /* O - 1 on success, 0 on failure */
943 cupsdWriteErrorLog(int level
, /* I - Log level */
944 const char *message
) /* I - Message string */
946 static const char levels
[] = /* Log levels... */
960 static const int syslevels
[] = /* SYSLOG levels... */
973 #endif /* HAVE_VSYSLOG */
978 * See if we are logging errors via syslog...
981 if (!strcmp(ErrorLog
, "syslog"))
983 syslog(syslevels
[level
], "%s", message
);
986 #endif /* HAVE_VSYSLOG */
989 * Not using syslog; check the log file...
992 if (!cupsdCheckLogFile(&ErrorFile
, ErrorLog
))
996 * Write the log message...
999 cupsFilePrintf(ErrorFile
, "%c %s %s\n", levels
[level
],
1000 cupsdGetDateTime(NULL
, LogTimeFormat
), message
);
1001 cupsFileFlush(ErrorFile
);
1008 * 'format_log_line()' - Format a line for a log file.
1010 * This function resizes a global string buffer as needed. Each call returns
1011 * a pointer to this buffer, so the contents are only good until the next call
1012 * to format_log_line()...
1015 static int /* O - -1 for fatal, 0 for retry, 1 for success */
1016 format_log_line(const char *message
, /* I - Printf-style format string */
1017 va_list ap
) /* I - Argument list */
1019 int len
; /* Length of formatted line */
1023 * Allocate the line buffer as needed...
1028 log_linesize
= 8192;
1029 log_line
= malloc(log_linesize
);
1036 * Format the log message...
1039 len
= vsnprintf(log_line
, log_linesize
, message
, ap
);
1042 * Resize the buffer as needed...
1045 if (len
>= log_linesize
&& log_linesize
< 65536)
1047 char *temp
; /* Temporary string pointer */
1054 else if (len
> 65536)
1057 temp
= realloc(log_line
, len
);
1073 * End of "$Id: log.c 7918 2008-09-08 22:03:01Z mike $".