2 * Log file routines for the CUPS scheduler.
4 * Copyright © 2007-2018 by Apple Inc.
5 * Copyright © 1997-2007 by Easy Software Products, all rights reserved.
7 * Licensed under Apache License v2.0. See the file "LICENSE" for more
12 * Include necessary headers...
19 #elif defined(HAVE_SYSTEMD_SD_JOURNAL_H)
20 # define SD_JOURNAL_SUPPRESS_LOCATION
21 # include <systemd/sd-journal.h>
22 #endif /* HAVE_ASL_H */
27 * Constants for log keys from PWG 5110.3 (PWG Common Log Format)...
30 #define PWG_DeviceUUID "DUU"
32 #define PWG_LogNaturalLanguage "NL"
33 #define PWG_Status "S"
34 #define PWG_ServiceURI "URI"
35 #define PWG_UserHost "UH"
36 #define PWG_UserName "UN"
37 #define PWG_UserURI "UU"
38 #define PWG_ServiceIsAcceptingJobs "IAJ"
39 #define PWG_ServiceState "ST"
40 #define PWG_ServiceStateReasons "SR"
41 #define PWG_ServiceUUID "SUU"
42 #define PWG_JobID "JID"
43 #define PWG_JobUUID "JUU"
44 #define PWG_JobImagesCompleted "JIM"
45 #define PWG_JobImpressionsCompleted "JIC"
46 #define PWG_JobDestinationURI "JD"
47 #define PWG_JobState "JS"
48 #define PWG_JobStateReasons "JR"
49 #define PWG_JobAccountingID "JA"
50 #define PWG_JobAcountingUserName "JAUN"
51 #define PWG_JobAccountingUserURI "JAUU"
58 static _cups_mutex_t log_mutex
= _CUPS_MUTEX_INITIALIZER
;
59 /* Mutex for logging */
60 static size_t log_linesize
= 0; /* Size of line for output file */
61 static char *log_line
= NULL
; /* Line for output file */
64 static const int log_levels
[] = /* ASL levels... */
77 #elif defined(HAVE_VSYSLOG) || defined(HAVE_SYSTEMD_SD_JOURNAL_H)
78 static const int log_levels
[] = /* SYSLOG levels... */
91 #endif /* HAVE_ASL_H */
98 static int format_log_line(const char *message
, va_list ap
);
102 * 'cupsdCheckLogFile()' - Open/rotate a log file if it needs it.
105 int /* O - 1 if log file open */
106 cupsdCheckLogFile(cups_file_t
**lf
, /* IO - Log file */
107 const char *logname
) /* I - Log filename */
109 char backname
[1024], /* Backup log filename */
110 filename
[1024], /* Formatted log filename */
111 *ptr
; /* Pointer into filename */
112 const char *logptr
; /* Pointer into log filename */
116 * See if we have a log file to check...
119 if (!lf
|| !logname
|| !logname
[0])
123 * Handle logging to stderr...
126 if (!strcmp(logname
, "stderr"))
133 * Format the filename as needed...
137 (strncmp(logname
, "/dev/", 5) && cupsFileTell(*lf
) > MaxLogSize
&&
141 * Handle format strings...
144 filename
[sizeof(filename
) - 1] = '\0';
146 if (logname
[0] != '/')
148 strlcpy(filename
, ServerRoot
, sizeof(filename
));
149 strlcat(filename
, "/", sizeof(filename
));
154 for (logptr
= logname
, ptr
= filename
+ strlen(filename
);
155 *logptr
&& ptr
< (filename
+ sizeof(filename
) - 1);
167 * Insert the server name...
170 strlcpy(ptr
, ServerName
, sizeof(filename
) - (size_t)(ptr
- filename
));
176 * Otherwise just insert the character...
189 * See if the log file is open...
195 * Nope, open the log file...
198 if ((*lf
= cupsFileOpen(filename
, "a")) == NULL
)
201 * If the file is in CUPS_LOGDIR then try to create a missing directory...
204 if (!strncmp(filename
, CUPS_LOGDIR
, strlen(CUPS_LOGDIR
)))
207 * Try updating the permissions of the containing log directory, using
208 * the log file permissions as a basis...
211 mode_t log_dir_perm
= (mode_t
)(0300 | LogFilePerm
);
212 /* LogFilePerm + owner write/search */
213 if (log_dir_perm
& 0040)
214 log_dir_perm
|= 0010; /* Add group search */
215 if (log_dir_perm
& 0004)
216 log_dir_perm
|= 0001; /* Add other search */
218 cupsdCheckPermissions(CUPS_LOGDIR
, NULL
, log_dir_perm
, RunUser
, Group
, 1, -1);
220 *lf
= cupsFileOpen(filename
, "a");
225 #ifdef HAVE_SYSTEMD_SD_JOURNAL_H
226 sd_journal_print(LOG_ERR
, "Unable to open log file \"%s\" - %s", filename
, strerror(errno
));
228 syslog(LOG_ERR
, "Unable to open log file \"%s\" - %s", filename
, strerror(errno
));
229 #endif /* HAVE_SYSTEMD_SD_JOURNAL_H */
231 if (FatalErrors
& CUPSD_FATAL_LOG
)
232 cupsdEndProcess(getpid(), 0);
238 if (strncmp(filename
, "/dev/", 5))
241 * Change ownership and permissions of non-device logs...
244 fchown(cupsFileNumber(*lf
), RunUser
, Group
);
245 fchmod(cupsFileNumber(*lf
), LogFilePerm
);
250 * Do we need to rotate the log?
253 if (strncmp(logname
, "/dev/", 5) && cupsFileTell(*lf
) > MaxLogSize
&&
262 strlcpy(backname
, filename
, sizeof(backname
));
263 strlcat(backname
, ".O", sizeof(backname
));
266 rename(filename
, backname
);
268 if ((*lf
= cupsFileOpen(filename
, "a")) == NULL
)
270 #ifdef HAVE_SYSTEMD_SD_JOURNAL_H
271 sd_journal_print(LOG_ERR
, "Unable to open log file \"%s\" - %s", filename
, strerror(errno
));
274 syslog(LOG_ERR
, "Unable to open log file \"%s\" - %s", filename
, strerror(errno
));
275 #endif /* HAVE_SYSTEMD_SD_JOURNAL_H */
277 if (FatalErrors
& CUPSD_FATAL_LOG
)
278 cupsdEndProcess(getpid(), 0);
284 * Change ownership and permissions of non-device logs...
287 fchown(cupsFileNumber(*lf
), RunUser
, Group
);
288 fchmod(cupsFileNumber(*lf
), LogFilePerm
);
296 * 'cupsdGetDateTime()' - Returns a pointer to a date/time string.
299 char * /* O - Date/time string */
300 cupsdGetDateTime(struct timeval
*t
, /* I - Time value or NULL for current */
301 cupsd_time_t format
) /* I - Format to use */
303 struct timeval curtime
; /* Current time value */
304 struct tm
*date
; /* Date/time value */
305 static struct timeval last_time
= { 0, 0 };
306 /* Last time we formatted */
307 static char s
[1024]; /* Date/time string */
308 static const char * const months
[12] =/* Months */
326 * Make sure we have a valid time...
331 gettimeofday(&curtime
, NULL
);
335 if (t
->tv_sec
!= last_time
.tv_sec
||
336 (LogTimeFormat
== CUPSD_TIME_USECS
&& t
->tv_usec
!= last_time
.tv_usec
))
341 * Get the date and time from the UNIX time value, and then format it
342 * into a string. Note that we *can't* use the strftime() function since
343 * it is localized and will seriously confuse automatic programs if the
344 * month names are in the wrong language!
346 * Also, we use the "timezone" variable that contains the current timezone
347 * offset from GMT in seconds so that we are reporting local time in the
348 * log files. If you want GMT, set the TZ environment variable accordingly
349 * before starting the scheduler.
351 * (*BSD and Darwin store the timezone offset in the tm structure)
354 date
= localtime(&(t
->tv_sec
));
356 if (format
== CUPSD_TIME_STANDARD
)
357 snprintf(s
, sizeof(s
), "[%02d/%s/%04d:%02d:%02d:%02d %+03ld%02ld]",
358 date
->tm_mday
, months
[date
->tm_mon
], 1900 + date
->tm_year
,
359 date
->tm_hour
, date
->tm_min
, date
->tm_sec
,
360 #ifdef HAVE_TM_GMTOFF
361 date
->tm_gmtoff
/ 3600, (date
->tm_gmtoff
/ 60) % 60);
363 timezone
/ 3600, (timezone
/ 60) % 60);
364 #endif /* HAVE_TM_GMTOFF */
366 snprintf(s
, sizeof(s
), "[%02d/%s/%04d:%02d:%02d:%02d.%06d %+03ld%02ld]",
367 date
->tm_mday
, months
[date
->tm_mon
], 1900 + date
->tm_year
,
368 date
->tm_hour
, date
->tm_min
, date
->tm_sec
, (int)t
->tv_usec
,
369 #ifdef HAVE_TM_GMTOFF
370 date
->tm_gmtoff
/ 3600, (date
->tm_gmtoff
/ 60) % 60);
372 timezone
/ 3600, (timezone
/ 60) % 60);
373 #endif /* HAVE_TM_GMTOFF */
381 * 'cupsdLogFCMessage()' - Log a file checking message.
386 void *context
, /* I - Printer (if any) */
387 _cups_fc_result_t result
, /* I - Check result */
388 const char *message
) /* I - Message to log */
390 cupsd_printer_t
*p
= (cupsd_printer_t
*)context
;
392 cupsd_loglevel_t level
; /* Log level */
395 if (result
== _CUPS_FILE_CHECK_OK
)
396 level
= CUPSD_LOG_DEBUG2
;
398 level
= CUPSD_LOG_ERROR
;
402 cupsdLogMessage(level
, "%s: %s", p
->name
, message
);
404 if (result
== _CUPS_FILE_CHECK_MISSING
||
405 result
== _CUPS_FILE_CHECK_WRONG_TYPE
)
407 strlcpy(p
->state_message
, message
, sizeof(p
->state_message
));
409 if (cupsdSetPrinterReasons(p
, "+cups-missing-filter-warning"))
410 cupsdAddEvent(CUPSD_EVENT_PRINTER_STATE
, p
, NULL
, "%s", message
);
412 else if (result
== _CUPS_FILE_CHECK_PERMISSIONS
||
413 result
== _CUPS_FILE_CHECK_RELATIVE_PATH
)
415 strlcpy(p
->state_message
, message
, sizeof(p
->state_message
));
417 if (cupsdSetPrinterReasons(p
, "+cups-insecure-filter-warning"))
418 cupsdAddEvent(CUPSD_EVENT_PRINTER_STATE
, p
, NULL
, "%s", message
);
422 cupsdLogMessage(level
, "%s", message
);
428 * 'cupsdLogGSSMessage()' - Log a GSSAPI error...
431 int /* O - 1 on success, 0 on error */
433 int level
, /* I - Log level */
434 OM_uint32 major_status
, /* I - Major GSSAPI status */
435 OM_uint32 minor_status
, /* I - Minor GSSAPI status */
436 const char *message
, /* I - printf-style message string */
437 ...) /* I - Additional args as needed */
439 OM_uint32 err_major_status
, /* Major status code for display */
440 err_minor_status
; /* Minor status code for display */
441 OM_uint32 msg_ctx
; /* Message context */
442 gss_buffer_desc major_status_string
= GSS_C_EMPTY_BUFFER
,
443 /* Major status message */
444 minor_status_string
= GSS_C_EMPTY_BUFFER
;
445 /* Minor status message */
446 int ret
; /* Return value */
447 char buffer
[8192]; /* Buffer for vsnprintf */
450 if (strchr(message
, '%'))
453 * Format the message string...
456 va_list ap
; /* Pointer to arguments */
458 va_start(ap
, message
);
459 vsnprintf(buffer
, sizeof(buffer
), message
, ap
);
466 err_major_status
= gss_display_status(&err_minor_status
,
471 &major_status_string
);
473 if (!GSS_ERROR(err_major_status
))
474 gss_display_status(&err_minor_status
, minor_status
, GSS_C_MECH_CODE
,
475 GSS_C_NULL_OID
, &msg_ctx
, &minor_status_string
);
477 ret
= cupsdLogMessage(level
, "%s: %s, %s", message
,
478 (char *)major_status_string
.value
,
479 (char *)minor_status_string
.value
);
480 gss_release_buffer(&err_minor_status
, &major_status_string
);
481 gss_release_buffer(&err_minor_status
, &minor_status_string
);
485 #endif /* HAVE_GSSAPI */
489 * 'cupsdLogClient()' - Log a client message.
492 int /* O - 1 on success, 0 on error */
493 cupsdLogClient(cupsd_client_t
*con
, /* I - Client connection */
494 int level
, /* I - Log level */
495 const char *message
, /* I - Printf-style message string */
496 ...) /* I - Additional arguments as needed */
498 va_list ap
, ap2
; /* Argument pointers */
499 char clientmsg
[1024];/* Format string for client message */
500 int status
; /* Formatting status */
504 * See if we want to log this message...
507 if (TestConfigFile
|| !ErrorLog
)
510 if (level
> LogLevel
)
514 * Format and write the log message...
518 snprintf(clientmsg
, sizeof(clientmsg
), "[Client %d] %s", con
->number
,
521 strlcpy(clientmsg
, message
, sizeof(clientmsg
));
523 va_start(ap
, message
);
528 status
= format_log_line(clientmsg
, ap2
);
536 return (cupsdWriteErrorLog(level
, log_line
));
538 return (cupsdWriteErrorLog(CUPSD_LOG_ERROR
,
539 "Unable to allocate memory for log line."));
544 * 'cupsdLogJob()' - Log a job message.
547 int /* O - 1 on success, 0 on error */
548 cupsdLogJob(cupsd_job_t
*job
, /* I - Job */
549 int level
, /* I - Log level */
550 const char *message
, /* I - Printf-style message string */
551 ...) /* I - Additional arguments as needed */
553 va_list ap
, ap2
; /* Argument pointers */
554 char jobmsg
[1024]; /* Format string for job message */
555 int status
; /* Formatting status */
559 * See if we want to log this message...
562 if (TestConfigFile
|| !ErrorLog
)
565 if (level
> LogLevel
&& LogDebugHistory
<= 0)
569 * Format and write the log message...
572 #ifdef HAVE_SYSTEMD_SD_JOURNAL_H
573 if (job
&& strcmp(ErrorLog
, "syslog"))
576 #endif /* HAVE_SYSTEMD_SD_JOURNAL_H */
577 snprintf(jobmsg
, sizeof(jobmsg
), "[Job %d] %s", job
->id
, message
);
579 strlcpy(jobmsg
, message
, sizeof(jobmsg
));
581 va_start(ap
, message
);
586 status
= format_log_line(jobmsg
, ap2
);
595 if (job
&& level
> LogLevel
&& LogDebugHistory
> 0)
598 * Add message to the job history...
601 cupsd_joblog_t
*temp
; /* Copy of log message */
602 size_t log_len
= strlen(log_line
);
603 /* Length of log message */
605 if ((temp
= malloc(sizeof(cupsd_joblog_t
) + log_len
)) != NULL
)
607 temp
->time
= time(NULL
);
608 memcpy(temp
->message
, log_line
, log_len
+ 1);
612 job
->history
= cupsArrayNew(NULL
, NULL
);
614 if (job
->history
&& temp
)
616 cupsArrayAdd(job
->history
, temp
);
618 if (cupsArrayCount(job
->history
) > LogDebugHistory
)
621 * Remove excess messages...
624 temp
= cupsArrayFirst(job
->history
);
625 cupsArrayRemove(job
->history
, temp
);
634 else if (level
<= LogLevel
)
636 #ifdef HAVE_SYSTEMD_SD_JOURNAL_H
637 if (!strcmp(ErrorLog
, "syslog"))
639 cupsd_printer_t
*printer
= job
? (job
->printer
? job
->printer
: (job
->dest
? cupsdFindDest(job
->dest
) : NULL
)) : NULL
;
640 static const char * const job_states
[] =
641 { /* job-state strings */
652 sd_journal_send("MESSAGE=%s", log_line
,
653 "PRIORITY=%i", log_levels
[level
],
654 PWG_Event
"=JobStateChanged",
655 PWG_ServiceURI
"=%s", printer
? printer
->uri
: "",
656 PWG_JobID
"=%d", job
->id
,
657 PWG_JobState
"=%s", job
->state_value
< IPP_JSTATE_PENDING
? "" : job_states
[job
->state_value
- IPP_JSTATE_PENDING
],
658 PWG_JobImpressionsCompleted
"=%d", ippGetInteger(job
->impressions
, 0),
661 sd_journal_send("MESSAGE=%s", log_line
,
662 "PRIORITY=%i", log_levels
[level
],
668 #endif /* HAVE_SYSTEMD_SD_JOURNAL_H */
670 return (cupsdWriteErrorLog(level
, log_line
));
676 return (cupsdWriteErrorLog(CUPSD_LOG_ERROR
,
677 "Unable to allocate memory for log line."));
682 * 'cupsdLogMessage()' - Log a message to the error log file.
685 int /* O - 1 on success, 0 on error */
686 cupsdLogMessage(int level
, /* I - Log level */
687 const char *message
, /* I - printf-style message string */
688 ...) /* I - Additional args as needed */
690 va_list ap
, ap2
; /* Argument pointers */
691 int status
; /* Formatting status */
695 * See if we want to log this message...
698 if (TestConfigFile
&& level
<= CUPSD_LOG_WARN
)
700 va_start(ap
, message
);
702 vfprintf(stderr
, message
, ap
);
709 else if (!ErrorLog
&& level
<= CUPSD_LOG_WARN
)
711 va_start(ap
, message
);
713 #ifdef HAVE_SYSTEMD_SD_JOURNAL_H
714 sd_journal_printv(log_levels
[level
], message
, ap
);
716 #elif defined(HAVE_VSYSLOG)
717 vsyslog(LOG_LPR
| log_levels
[level
], message
, ap
);
720 vfprintf(stderr
, message
, ap
);
722 #endif /* HAVE_SYSTEMD_SD_JOURNAL_H */
728 else if (level
> LogLevel
|| !ErrorLog
)
731 #ifdef HAVE_SYSTEMD_SD_JOURNAL_H
732 else if (!strcmp(ErrorLog
, "syslog"))
734 va_start(ap
, message
);
735 sd_journal_printv(log_levels
[level
], message
, ap
);
739 #endif /* HAVE_SYSTEMD_SD_JOURNAL_H */
742 * Format and write the log message...
745 va_start(ap
, message
);
750 status
= format_log_line(message
, ap2
);
758 return (cupsdWriteErrorLog(level
, log_line
));
760 return (cupsdWriteErrorLog(CUPSD_LOG_ERROR
,
761 "Unable to allocate memory for log line!"));
766 * 'cupsdLogPage()' - Log a page to the page log file.
769 int /* O - 1 on success, 0 on error */
770 cupsdLogPage(cupsd_job_t
*job
, /* I - Job being printed */
771 const char *page
) /* I - Page being printed */
773 int i
; /* Looping var */
774 char buffer
[2048], /* Buffer for page log */
775 *bufptr
, /* Pointer into buffer */
776 name
[256]; /* Attribute name */
777 const char *format
, /* Pointer into PageLogFormat */
778 *nameend
; /* End of attribute name */
779 ipp_attribute_t
*attr
; /* Current attribute */
780 char number
[256]; /* Page number */
781 int copies
; /* Number of copies */
785 * Format the line going into the page log...
791 strlcpy(number
, "1", sizeof(number
));
793 sscanf(page
, "%255s%d", number
, &copies
);
795 for (format
= PageLogFormat
, bufptr
= buffer
; *format
; format
++)
803 case '%' : /* Literal % */
804 if (bufptr
< (buffer
+ sizeof(buffer
) - 1))
808 case 'p' : /* Printer name */
809 strlcpy(bufptr
, job
->dest
, sizeof(buffer
) - (size_t)(bufptr
- buffer
));
810 bufptr
+= strlen(bufptr
);
813 case 'j' : /* Job ID */
814 snprintf(bufptr
, sizeof(buffer
) - (size_t)(bufptr
- buffer
), "%d", job
->id
);
815 bufptr
+= strlen(bufptr
);
818 case 'u' : /* Username */
819 strlcpy(bufptr
, job
->username
? job
->username
: "-", sizeof(buffer
) - (size_t)(bufptr
- buffer
));
820 bufptr
+= strlen(bufptr
);
823 case 'T' : /* Date and time */
824 strlcpy(bufptr
, cupsdGetDateTime(NULL
, LogTimeFormat
), sizeof(buffer
) - (size_t)(bufptr
- buffer
));
825 bufptr
+= strlen(bufptr
);
828 case 'P' : /* Page number */
829 strlcpy(bufptr
, number
, sizeof(buffer
) - (size_t)(bufptr
- buffer
));
830 bufptr
+= strlen(bufptr
);
833 case 'C' : /* Number of copies */
834 snprintf(bufptr
, sizeof(buffer
) - (size_t)(bufptr
- buffer
), "%d", copies
);
835 bufptr
+= strlen(bufptr
);
838 case '{' : /* {attribute} */
839 if ((nameend
= strchr(format
, '}')) != NULL
&& (size_t)(nameend
- format
- 2) < (sizeof(name
) - 1))
842 * Pull the name from inside the brackets...
845 memcpy(name
, format
+ 1, (size_t)(nameend
- format
- 1));
846 name
[nameend
- format
- 1] = '\0';
850 attr
= ippFindAttribute(job
->attrs
, name
, IPP_TAG_ZERO
);
851 if (!attr
&& !strcmp(name
, "job-billing"))
854 * Handle alias "job-account-id" (which was standardized after
855 * "job-billing" was defined for CUPS...
858 attr
= ippFindAttribute(job
->attrs
, "job-account-id", IPP_TAG_ZERO
);
860 else if (!attr
&& !strcmp(name
, "media"))
863 * Handle alias "media-col" which uses dimensions instead of
867 attr
= ippFindAttribute(job
->attrs
, "media-col/media-size", IPP_TAG_BEGIN_COLLECTION
);
873 * Add the attribute value...
877 i
< attr
->num_values
&&
878 bufptr
< (buffer
+ sizeof(buffer
) - 1);
884 switch (attr
->value_tag
)
886 case IPP_TAG_INTEGER
:
888 snprintf(bufptr
, sizeof(buffer
) - (size_t)(bufptr
- buffer
), "%d", attr
->values
[i
].integer
);
889 bufptr
+= strlen(bufptr
);
892 case IPP_TAG_BOOLEAN
:
893 snprintf(bufptr
, sizeof(buffer
) - (size_t)(bufptr
- buffer
), "%d", attr
->values
[i
].boolean
);
894 bufptr
+= strlen(bufptr
);
897 case IPP_TAG_TEXTLANG
:
898 case IPP_TAG_NAMELANG
:
901 case IPP_TAG_KEYWORD
:
903 case IPP_TAG_URISCHEME
:
904 case IPP_TAG_CHARSET
:
905 case IPP_TAG_LANGUAGE
:
906 case IPP_TAG_MIMETYPE
:
907 strlcpy(bufptr
, attr
->values
[i
].string
.text
, sizeof(buffer
) - (size_t)(bufptr
- buffer
));
908 bufptr
+= strlen(bufptr
);
911 case IPP_TAG_BEGIN_COLLECTION
:
912 if (!strcmp(attr
->name
, "media-size"))
914 ipp_attribute_t
*x_dimension
= ippFindAttribute(ippGetCollection(attr
, 0), "x-dimension", IPP_TAG_INTEGER
);
915 ipp_attribute_t
*y_dimension
= ippFindAttribute(ippGetCollection(attr
, 0), "y-dimension", IPP_TAG_INTEGER
);
916 /* Media dimensions */
918 if (x_dimension
&& y_dimension
)
920 pwg_media_t
*pwg
= pwgMediaForSize(ippGetInteger(x_dimension
, 0), ippGetInteger(y_dimension
, 0));
922 strlcpy(bufptr
, pwg
->pwg
, sizeof(buffer
) - (size_t)(bufptr
- buffer
));
928 strlcpy(bufptr
, "???", sizeof(buffer
) - (size_t)(bufptr
- buffer
));
929 bufptr
+= strlen(bufptr
);
934 else if (bufptr
< (buffer
+ sizeof(buffer
) - 1))
940 if (bufptr
< (buffer
+ sizeof(buffer
) - 2))
948 else if (bufptr
< (buffer
+ sizeof(buffer
) - 1))
954 #ifdef HAVE_SYSTEMD_SD_JOURNAL_H
955 if (!strcmp(PageLog
, "syslog"))
957 static const char * const job_states
[] =
958 { /* job-state strings */
968 sd_journal_send("MESSAGE=%s", buffer
,
969 "PRIORITY=%i", LOG_INFO
,
970 PWG_Event
"=JobStateChanged",
971 PWG_ServiceURI
"=%s", job
->printer
->uri
,
972 PWG_JobID
"=%d", job
->id
,
973 PWG_JobState
"=%s", job_states
[job
->state_value
- IPP_JSTATE_PENDING
],
974 PWG_JobImpressionsCompleted
"=%d", ippGetInteger(job
->impressions
, 0),
979 #elif defined(HAVE_VSYSLOG)
981 * See if we are logging pages via syslog...
984 if (!strcmp(PageLog
, "syslog"))
986 syslog(LOG_INFO
, "%s", buffer
);
990 #endif /* HAVE_SYSTEMD_SD_JOURNAL_H */
993 * Not using syslog; check the log file...
996 if (!cupsdCheckLogFile(&PageFile
, PageLog
))
1000 * Print a page log entry of the form:
1002 * printer user job-id [DD/MON/YYYY:HH:MM:SS +TTTT] page num-copies \
1006 cupsFilePrintf(PageFile
, "%s\n", buffer
);
1007 cupsFileFlush(PageFile
);
1014 * 'cupsdLogRequest()' - Log an HTTP request in Common Log Format.
1017 int /* O - 1 on success, 0 on error */
1018 cupsdLogRequest(cupsd_client_t
*con
, /* I - Request to log */
1019 http_status_t code
) /* I - Response code */
1021 char temp
[2048]; /* Temporary string for URI */
1022 static const char * const states
[] = /* HTTP client states... */
1042 * Filter requests as needed...
1045 if (AccessLogLevel
== CUPSD_ACCESSLOG_NONE
|| !AccessLog
)
1047 else if (AccessLogLevel
< CUPSD_ACCESSLOG_ALL
)
1050 * Eliminate simple GET, POST, and PUT requests...
1053 if ((con
->operation
== HTTP_GET
&&
1054 strncmp(con
->uri
, "/admin/conf", 11) &&
1055 strncmp(con
->uri
, "/admin/log", 10)) ||
1056 (con
->operation
== HTTP_POST
&& !con
->request
&&
1057 strncmp(con
->uri
, "/admin", 6)) ||
1058 (con
->operation
!= HTTP_GET
&& con
->operation
!= HTTP_POST
&&
1059 con
->operation
!= HTTP_PUT
))
1062 if (con
->request
&& con
->response
&&
1063 (con
->response
->request
.status
.status_code
< IPP_REDIRECTION_OTHER_SITE
||
1064 con
->response
->request
.status
.status_code
== IPP_NOT_FOUND
))
1067 * Check successful requests...
1070 ipp_op_t op
= con
->request
->request
.op
.operation_id
;
1071 static cupsd_accesslog_t standard_ops
[] =
1073 CUPSD_ACCESSLOG_ALL
, /* reserved */
1074 CUPSD_ACCESSLOG_ALL
, /* reserved */
1075 CUPSD_ACCESSLOG_ACTIONS
,/* Print-Job */
1076 CUPSD_ACCESSLOG_ACTIONS
,/* Print-URI */
1077 CUPSD_ACCESSLOG_ACTIONS
,/* Validate-Job */
1078 CUPSD_ACCESSLOG_ACTIONS
,/* Create-Job */
1079 CUPSD_ACCESSLOG_ACTIONS
,/* Send-Document */
1080 CUPSD_ACCESSLOG_ACTIONS
,/* Send-URI */
1081 CUPSD_ACCESSLOG_ACTIONS
,/* Cancel-Job */
1082 CUPSD_ACCESSLOG_ALL
, /* Get-Job-Attributes */
1083 CUPSD_ACCESSLOG_ALL
, /* Get-Jobs */
1084 CUPSD_ACCESSLOG_ALL
, /* Get-Printer-Attributes */
1085 CUPSD_ACCESSLOG_ACTIONS
,/* Hold-Job */
1086 CUPSD_ACCESSLOG_ACTIONS
,/* Release-Job */
1087 CUPSD_ACCESSLOG_ACTIONS
,/* Restart-Job */
1088 CUPSD_ACCESSLOG_ALL
, /* reserved */
1089 CUPSD_ACCESSLOG_CONFIG
, /* Pause-Printer */
1090 CUPSD_ACCESSLOG_CONFIG
, /* Resume-Printer */
1091 CUPSD_ACCESSLOG_CONFIG
, /* Purge-Jobs */
1092 CUPSD_ACCESSLOG_CONFIG
, /* Set-Printer-Attributes */
1093 CUPSD_ACCESSLOG_ACTIONS
,/* Set-Job-Attributes */
1094 CUPSD_ACCESSLOG_CONFIG
, /* Get-Printer-Supported-Values */
1095 CUPSD_ACCESSLOG_ACTIONS
,/* Create-Printer-Subscription */
1096 CUPSD_ACCESSLOG_ACTIONS
,/* Create-Job-Subscription */
1097 CUPSD_ACCESSLOG_ALL
, /* Get-Subscription-Attributes */
1098 CUPSD_ACCESSLOG_ALL
, /* Get-Subscriptions */
1099 CUPSD_ACCESSLOG_ACTIONS
,/* Renew-Subscription */
1100 CUPSD_ACCESSLOG_ACTIONS
,/* Cancel-Subscription */
1101 CUPSD_ACCESSLOG_ALL
, /* Get-Notifications */
1102 CUPSD_ACCESSLOG_ACTIONS
,/* Send-Notifications */
1103 CUPSD_ACCESSLOG_ALL
, /* reserved */
1104 CUPSD_ACCESSLOG_ALL
, /* reserved */
1105 CUPSD_ACCESSLOG_ALL
, /* reserved */
1106 CUPSD_ACCESSLOG_ALL
, /* Get-Print-Support-Files */
1107 CUPSD_ACCESSLOG_CONFIG
, /* Enable-Printer */
1108 CUPSD_ACCESSLOG_CONFIG
, /* Disable-Printer */
1109 CUPSD_ACCESSLOG_CONFIG
, /* Pause-Printer-After-Current-Job */
1110 CUPSD_ACCESSLOG_ACTIONS
,/* Hold-New-Jobs */
1111 CUPSD_ACCESSLOG_ACTIONS
,/* Release-Held-New-Jobs */
1112 CUPSD_ACCESSLOG_CONFIG
, /* Deactivate-Printer */
1113 CUPSD_ACCESSLOG_CONFIG
, /* Activate-Printer */
1114 CUPSD_ACCESSLOG_CONFIG
, /* Restart-Printer */
1115 CUPSD_ACCESSLOG_CONFIG
, /* Shutdown-Printer */
1116 CUPSD_ACCESSLOG_CONFIG
, /* Startup-Printer */
1117 CUPSD_ACCESSLOG_ACTIONS
,/* Reprocess-Job */
1118 CUPSD_ACCESSLOG_ACTIONS
,/* Cancel-Current-Job */
1119 CUPSD_ACCESSLOG_ACTIONS
,/* Suspend-Current-Job */
1120 CUPSD_ACCESSLOG_ACTIONS
,/* Resume-Job */
1121 CUPSD_ACCESSLOG_ACTIONS
,/* Promote-Job */
1122 CUPSD_ACCESSLOG_ACTIONS
/* Schedule-Job-After */
1124 static cupsd_accesslog_t cups_ops
[] =
1126 CUPSD_ACCESSLOG_ALL
, /* CUPS-Get-Default */
1127 CUPSD_ACCESSLOG_ALL
, /* CUPS-Get-Printers */
1128 CUPSD_ACCESSLOG_CONFIG
, /* CUPS-Add-Modify-Printer */
1129 CUPSD_ACCESSLOG_CONFIG
, /* CUPS-Delete-Printer */
1130 CUPSD_ACCESSLOG_ALL
, /* CUPS-Get-Classes */
1131 CUPSD_ACCESSLOG_CONFIG
, /* CUPS-Add-Modify-Class */
1132 CUPSD_ACCESSLOG_CONFIG
, /* CUPS-Delete-Class */
1133 CUPSD_ACCESSLOG_CONFIG
, /* CUPS-Accept-Jobs */
1134 CUPSD_ACCESSLOG_CONFIG
, /* CUPS-Reject-Jobs */
1135 CUPSD_ACCESSLOG_CONFIG
, /* CUPS-Set-Default */
1136 CUPSD_ACCESSLOG_CONFIG
, /* CUPS-Get-Devices */
1137 CUPSD_ACCESSLOG_CONFIG
, /* CUPS-Get-PPDs */
1138 CUPSD_ACCESSLOG_ACTIONS
,/* CUPS-Move-Job */
1139 CUPSD_ACCESSLOG_ACTIONS
,/* CUPS-Authenticate-Job */
1140 CUPSD_ACCESSLOG_ALL
/* CUPS-Get-PPD */
1144 if ((op
<= IPP_SCHEDULE_JOB_AFTER
&& standard_ops
[op
] > AccessLogLevel
) ||
1145 (op
>= CUPS_GET_DEFAULT
&& op
<= CUPS_GET_PPD
&&
1146 cups_ops
[op
- CUPS_GET_DEFAULT
] > AccessLogLevel
))
1151 #ifdef HAVE_SYSTEMD_SD_JOURNAL_H
1152 if (!strcmp(AccessLog
, "syslog"))
1154 sd_journal_print(LOG_INFO
, "REQUEST %s - %s \"%s %s HTTP/%d.%d\" %d " CUPS_LLFMT
" %s %s", con
->http
->hostname
, con
->username
[0] != '\0' ? con
->username
: "-", states
[con
->operation
], _httpEncodeURI(temp
, con
->uri
, sizeof(temp
)), con
->http
->version
/ 100, con
->http
->version
% 100, code
, CUPS_LLCAST con
->bytes
, con
->request
? ippOpString(con
->request
->request
.op
.operation_id
) : "-", con
->response
? ippErrorString(con
->response
->request
.status
.status_code
) : "-");
1158 #elif defined(HAVE_VSYSLOG)
1160 * See if we are logging accesses via syslog...
1163 if (!strcmp(AccessLog
, "syslog"))
1166 "REQUEST %s - %s \"%s %s HTTP/%d.%d\" %d " CUPS_LLFMT
" %s %s\n",
1167 con
->http
->hostname
, con
->username
[0] != '\0' ? con
->username
: "-",
1168 states
[con
->operation
], _httpEncodeURI(temp
, con
->uri
, sizeof(temp
)),
1169 con
->http
->version
/ 100, con
->http
->version
% 100,
1170 code
, CUPS_LLCAST con
->bytes
,
1172 ippOpString(con
->request
->request
.op
.operation_id
) : "-",
1174 ippErrorString(con
->response
->request
.status
.status_code
) : "-");
1178 #endif /* HAVE_SYSTEMD_SD_JOURNAL_H */
1181 * Not using syslog; check the log file...
1184 if (!cupsdCheckLogFile(&AccessFile
, AccessLog
))
1188 * Write a log of the request in "common log format"...
1191 cupsFilePrintf(AccessFile
,
1192 "%s - %s %s \"%s %s HTTP/%d.%d\" %d " CUPS_LLFMT
" %s %s\n",
1193 con
->http
->hostname
,
1194 con
->username
[0] != '\0' ? con
->username
: "-",
1195 cupsdGetDateTime(&(con
->start
), LogTimeFormat
),
1196 states
[con
->operation
],
1197 _httpEncodeURI(temp
, con
->uri
, sizeof(temp
)),
1198 con
->http
->version
/ 100, con
->http
->version
% 100,
1199 code
, CUPS_LLCAST con
->bytes
,
1201 ippOpString(con
->request
->request
.op
.operation_id
) : "-",
1203 ippErrorString(con
->response
->request
.status
.status_code
) :
1206 cupsFileFlush(AccessFile
);
1213 * 'cupsdWriteErrorLog()' - Write a line to the ErrorLog.
1216 int /* O - 1 on success, 0 on failure */
1217 cupsdWriteErrorLog(int level
, /* I - Log level */
1218 const char *message
) /* I - Message string */
1220 int ret
= 1; /* Return value */
1221 static const char levels
[] = /* Log levels... */
1236 #ifdef HAVE_SYSTEMD_SD_JOURNAL_H
1237 if (!strcmp(ErrorLog
, "syslog"))
1239 sd_journal_print(log_levels
[level
], "%s", message
);
1243 #elif defined(HAVE_VSYSLOG)
1245 * See if we are logging errors via syslog...
1248 if (!strcmp(ErrorLog
, "syslog"))
1250 syslog(log_levels
[level
], "%s", message
);
1253 #endif /* HAVE_SYSTEMD_SD_JOURNAL_H */
1256 * Not using syslog; check the log file...
1259 _cupsMutexLock(&log_mutex
);
1261 if (!cupsdCheckLogFile(&ErrorFile
, ErrorLog
))
1268 * Write the log message...
1271 cupsFilePrintf(ErrorFile
, "%c %s %s\n", levels
[level
],
1272 cupsdGetDateTime(NULL
, LogTimeFormat
), message
);
1273 cupsFileFlush(ErrorFile
);
1276 _cupsMutexUnlock(&log_mutex
);
1283 * 'format_log_line()' - Format a line for a log file.
1285 * This function resizes a global string buffer as needed. Each call returns
1286 * a pointer to this buffer, so the contents are only good until the next call
1287 * to format_log_line()...
1290 static int /* O - -1 for fatal, 0 for retry, 1 for success */
1291 format_log_line(const char *message
, /* I - Printf-style format string */
1292 va_list ap
) /* I - Argument list */
1294 ssize_t len
; /* Length of formatted line */
1298 * Allocate the line buffer as needed...
1303 log_linesize
= 8192;
1304 log_line
= malloc(log_linesize
);
1311 * Format the log message...
1314 len
= _cups_safe_vsnprintf(log_line
, log_linesize
, message
, ap
);
1317 * Resize the buffer as needed...
1320 if ((size_t)len
>= log_linesize
&& log_linesize
< 65536)
1322 char *temp
; /* Temporary string pointer */
1328 else if (len
> 65536)
1331 temp
= realloc(log_line
, (size_t)len
);
1336 log_linesize
= (size_t)len
;