2 * Log file routines for the CUPS scheduler.
4 * Copyright 2007-2017 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 information.
11 * Include necessary headers...
18 #elif defined(HAVE_SYSTEMD_SD_JOURNAL_H)
19 # define SD_JOURNAL_SUPPRESS_LOCATION
20 # include <systemd/sd-journal.h>
21 #endif /* HAVE_ASL_H */
26 * Constants for log keys from PWG 5110.3 (PWG Common Log Format)...
29 #define PWG_DeviceUUID "DUU"
31 #define PWG_LogNaturalLanguage "NL"
32 #define PWG_Status "S"
33 #define PWG_ServiceURI "URI"
34 #define PWG_UserHost "UH"
35 #define PWG_UserName "UN"
36 #define PWG_UserURI "UU"
37 #define PWG_ServiceIsAcceptingJobs "IAJ"
38 #define PWG_ServiceState "ST"
39 #define PWG_ServiceStateReasons "SR"
40 #define PWG_ServiceUUID "SUU"
41 #define PWG_JobID "JID"
42 #define PWG_JobUUID "JUU"
43 #define PWG_JobImagesCompleted "JIM"
44 #define PWG_JobImpressionsCompleted "JIC"
45 #define PWG_JobDestinationURI "JD"
46 #define PWG_JobState "JS"
47 #define PWG_JobStateReasons "JR"
48 #define PWG_JobAccountingID "JA"
49 #define PWG_JobAcountingUserName "JAUN"
50 #define PWG_JobAccountingUserURI "JAUU"
57 static _cups_mutex_t log_mutex
= _CUPS_MUTEX_INITIALIZER
;
58 /* Mutex for logging */
59 static size_t log_linesize
= 0; /* Size of line for output file */
60 static char *log_line
= NULL
; /* Line for output file */
63 static const int log_levels
[] = /* ASL levels... */
76 #elif defined(HAVE_VSYSLOG) || defined(HAVE_SYSTEMD_SD_JOURNAL_H)
77 static const int log_levels
[] = /* SYSLOG levels... */
90 #endif /* HAVE_ASL_H */
97 static int format_log_line(const char *message
, va_list ap
);
101 * 'cupsdCheckLogFile()' - Open/rotate a log file if it needs it.
104 int /* O - 1 if log file open */
105 cupsdCheckLogFile(cups_file_t
**lf
, /* IO - Log file */
106 const char *logname
) /* I - Log filename */
108 char backname
[1024], /* Backup log filename */
109 filename
[1024], /* Formatted log filename */
110 *ptr
; /* Pointer into filename */
111 const char *logptr
; /* Pointer into log filename */
115 * See if we have a log file to check...
118 if (!lf
|| !logname
|| !logname
[0])
122 * Handle logging to stderr...
125 if (!strcmp(logname
, "stderr"))
132 * Format the filename as needed...
136 (strncmp(logname
, "/dev/", 5) && cupsFileTell(*lf
) > MaxLogSize
&&
140 * Handle format strings...
143 filename
[sizeof(filename
) - 1] = '\0';
145 if (logname
[0] != '/')
147 strlcpy(filename
, ServerRoot
, sizeof(filename
));
148 strlcat(filename
, "/", sizeof(filename
));
153 for (logptr
= logname
, ptr
= filename
+ strlen(filename
);
154 *logptr
&& ptr
< (filename
+ sizeof(filename
) - 1);
166 * Insert the server name...
169 strlcpy(ptr
, ServerName
, sizeof(filename
) - (size_t)(ptr
- filename
));
175 * Otherwise just insert the character...
188 * See if the log file is open...
194 * Nope, open the log file...
197 if ((*lf
= cupsFileOpen(filename
, "a")) == NULL
)
200 * If the file is in CUPS_LOGDIR then try to create a missing directory...
203 if (!strncmp(filename
, CUPS_LOGDIR
, strlen(CUPS_LOGDIR
)))
206 * Try updating the permissions of the containing log directory, using
207 * the log file permissions as a basis...
210 mode_t log_dir_perm
= (mode_t
)(0300 | LogFilePerm
);
211 /* LogFilePerm + owner write/search */
212 if (log_dir_perm
& 0040)
213 log_dir_perm
|= 0010; /* Add group search */
214 if (log_dir_perm
& 0004)
215 log_dir_perm
|= 0001; /* Add other search */
217 cupsdCheckPermissions(CUPS_LOGDIR
, NULL
, log_dir_perm
, RunUser
, Group
, 1, -1);
219 *lf
= cupsFileOpen(filename
, "a");
224 #ifdef HAVE_SYSTEMD_SD_JOURNAL_H
225 sd_journal_print(LOG_ERR
, "Unable to open log file \"%s\" - %s", filename
, strerror(errno
));
227 syslog(LOG_ERR
, "Unable to open log file \"%s\" - %s", filename
, strerror(errno
));
228 #endif /* HAVE_SYSTEMD_SD_JOURNAL_H */
230 if (FatalErrors
& CUPSD_FATAL_LOG
)
231 cupsdEndProcess(getpid(), 0);
237 if (strncmp(filename
, "/dev/", 5))
240 * Change ownership and permissions of non-device logs...
243 fchown(cupsFileNumber(*lf
), RunUser
, Group
);
244 fchmod(cupsFileNumber(*lf
), LogFilePerm
);
249 * Do we need to rotate the log?
252 if (strncmp(logname
, "/dev/", 5) && cupsFileTell(*lf
) > MaxLogSize
&&
261 strlcpy(backname
, filename
, sizeof(backname
));
262 strlcat(backname
, ".O", sizeof(backname
));
265 rename(filename
, backname
);
267 if ((*lf
= cupsFileOpen(filename
, "a")) == NULL
)
269 #ifdef HAVE_SYSTEMD_SD_JOURNAL_H
270 sd_journal_print(LOG_ERR
, "Unable to open log file \"%s\" - %s", filename
, strerror(errno
));
273 syslog(LOG_ERR
, "Unable to open log file \"%s\" - %s", filename
, strerror(errno
));
274 #endif /* HAVE_SYSTEMD_SD_JOURNAL_H */
276 if (FatalErrors
& CUPSD_FATAL_LOG
)
277 cupsdEndProcess(getpid(), 0);
283 * Change ownership and permissions of non-device logs...
286 fchown(cupsFileNumber(*lf
), RunUser
, Group
);
287 fchmod(cupsFileNumber(*lf
), LogFilePerm
);
295 * 'cupsdGetDateTime()' - Returns a pointer to a date/time string.
298 char * /* O - Date/time string */
299 cupsdGetDateTime(struct timeval
*t
, /* I - Time value or NULL for current */
300 cupsd_time_t format
) /* I - Format to use */
302 struct timeval curtime
; /* Current time value */
303 struct tm
*date
; /* Date/time value */
304 static struct timeval last_time
= { 0, 0 };
305 /* Last time we formatted */
306 static char s
[1024]; /* Date/time string */
307 static const char * const months
[12] =/* Months */
325 * Make sure we have a valid time...
330 gettimeofday(&curtime
, NULL
);
334 if (t
->tv_sec
!= last_time
.tv_sec
||
335 (LogTimeFormat
== CUPSD_TIME_USECS
&& t
->tv_usec
!= last_time
.tv_usec
))
340 * Get the date and time from the UNIX time value, and then format it
341 * into a string. Note that we *can't* use the strftime() function since
342 * it is localized and will seriously confuse automatic programs if the
343 * month names are in the wrong language!
345 * Also, we use the "timezone" variable that contains the current timezone
346 * offset from GMT in seconds so that we are reporting local time in the
347 * log files. If you want GMT, set the TZ environment variable accordingly
348 * before starting the scheduler.
350 * (*BSD and Darwin store the timezone offset in the tm structure)
353 date
= localtime(&(t
->tv_sec
));
355 if (format
== CUPSD_TIME_STANDARD
)
356 snprintf(s
, sizeof(s
), "[%02d/%s/%04d:%02d:%02d:%02d %+03ld%02ld]",
357 date
->tm_mday
, months
[date
->tm_mon
], 1900 + date
->tm_year
,
358 date
->tm_hour
, date
->tm_min
, date
->tm_sec
,
359 #ifdef HAVE_TM_GMTOFF
360 date
->tm_gmtoff
/ 3600, (date
->tm_gmtoff
/ 60) % 60);
362 timezone
/ 3600, (timezone
/ 60) % 60);
363 #endif /* HAVE_TM_GMTOFF */
365 snprintf(s
, sizeof(s
), "[%02d/%s/%04d:%02d:%02d:%02d.%06d %+03ld%02ld]",
366 date
->tm_mday
, months
[date
->tm_mon
], 1900 + date
->tm_year
,
367 date
->tm_hour
, date
->tm_min
, date
->tm_sec
, (int)t
->tv_usec
,
368 #ifdef HAVE_TM_GMTOFF
369 date
->tm_gmtoff
/ 3600, (date
->tm_gmtoff
/ 60) % 60);
371 timezone
/ 3600, (timezone
/ 60) % 60);
372 #endif /* HAVE_TM_GMTOFF */
380 * 'cupsdLogFCMessage()' - Log a file checking message.
385 void *context
, /* I - Printer (if any) */
386 _cups_fc_result_t result
, /* I - Check result */
387 const char *message
) /* I - Message to log */
389 cupsd_printer_t
*p
= (cupsd_printer_t
*)context
;
391 cupsd_loglevel_t level
; /* Log level */
394 if (result
== _CUPS_FILE_CHECK_OK
)
395 level
= CUPSD_LOG_DEBUG2
;
397 level
= CUPSD_LOG_ERROR
;
401 cupsdLogMessage(level
, "%s: %s", p
->name
, message
);
403 if (result
== _CUPS_FILE_CHECK_MISSING
||
404 result
== _CUPS_FILE_CHECK_WRONG_TYPE
)
406 strlcpy(p
->state_message
, message
, sizeof(p
->state_message
));
408 if (cupsdSetPrinterReasons(p
, "+cups-missing-filter-warning"))
409 cupsdAddEvent(CUPSD_EVENT_PRINTER_STATE
, p
, NULL
, "%s", message
);
411 else if (result
== _CUPS_FILE_CHECK_PERMISSIONS
||
412 result
== _CUPS_FILE_CHECK_RELATIVE_PATH
)
414 strlcpy(p
->state_message
, message
, sizeof(p
->state_message
));
416 if (cupsdSetPrinterReasons(p
, "+cups-insecure-filter-warning"))
417 cupsdAddEvent(CUPSD_EVENT_PRINTER_STATE
, p
, NULL
, "%s", message
);
421 cupsdLogMessage(level
, "%s", message
);
427 * 'cupsdLogGSSMessage()' - Log a GSSAPI error...
430 int /* O - 1 on success, 0 on error */
432 int level
, /* I - Log level */
433 OM_uint32 major_status
, /* I - Major GSSAPI status */
434 OM_uint32 minor_status
, /* I - Minor GSSAPI status */
435 const char *message
, /* I - printf-style message string */
436 ...) /* I - Additional args as needed */
438 OM_uint32 err_major_status
, /* Major status code for display */
439 err_minor_status
; /* Minor status code for display */
440 OM_uint32 msg_ctx
; /* Message context */
441 gss_buffer_desc major_status_string
= GSS_C_EMPTY_BUFFER
,
442 /* Major status message */
443 minor_status_string
= GSS_C_EMPTY_BUFFER
;
444 /* Minor status message */
445 int ret
; /* Return value */
446 char buffer
[8192]; /* Buffer for vsnprintf */
449 if (strchr(message
, '%'))
452 * Format the message string...
455 va_list ap
; /* Pointer to arguments */
457 va_start(ap
, message
);
458 vsnprintf(buffer
, sizeof(buffer
), message
, ap
);
465 err_major_status
= gss_display_status(&err_minor_status
,
470 &major_status_string
);
472 if (!GSS_ERROR(err_major_status
))
473 gss_display_status(&err_minor_status
, minor_status
, GSS_C_MECH_CODE
,
474 GSS_C_NULL_OID
, &msg_ctx
, &minor_status_string
);
476 ret
= cupsdLogMessage(level
, "%s: %s, %s", message
,
477 (char *)major_status_string
.value
,
478 (char *)minor_status_string
.value
);
479 gss_release_buffer(&err_minor_status
, &major_status_string
);
480 gss_release_buffer(&err_minor_status
, &minor_status_string
);
484 #endif /* HAVE_GSSAPI */
488 * 'cupsdLogClient()' - Log a client message.
491 int /* O - 1 on success, 0 on error */
492 cupsdLogClient(cupsd_client_t
*con
, /* I - Client connection */
493 int level
, /* I - Log level */
494 const char *message
, /* I - Printf-style message string */
495 ...) /* I - Additional arguments as needed */
497 va_list ap
, ap2
; /* Argument pointers */
498 char clientmsg
[1024];/* Format string for client message */
499 int status
; /* Formatting status */
503 * See if we want to log this message...
506 if (TestConfigFile
|| !ErrorLog
)
509 if (level
> LogLevel
)
513 * Format and write the log message...
517 snprintf(clientmsg
, sizeof(clientmsg
), "[Client %d] %s", con
->number
,
520 strlcpy(clientmsg
, message
, sizeof(clientmsg
));
522 va_start(ap
, message
);
527 status
= format_log_line(clientmsg
, ap2
);
535 return (cupsdWriteErrorLog(level
, log_line
));
537 return (cupsdWriteErrorLog(CUPSD_LOG_ERROR
,
538 "Unable to allocate memory for log line."));
543 * 'cupsdLogJob()' - Log a job message.
546 int /* O - 1 on success, 0 on error */
547 cupsdLogJob(cupsd_job_t
*job
, /* I - Job */
548 int level
, /* I - Log level */
549 const char *message
, /* I - Printf-style message string */
550 ...) /* I - Additional arguments as needed */
552 va_list ap
, ap2
; /* Argument pointers */
553 char jobmsg
[1024]; /* Format string for job message */
554 int status
; /* Formatting status */
558 * See if we want to log this message...
561 if (TestConfigFile
|| !ErrorLog
)
564 if (level
> LogLevel
&& LogDebugHistory
<= 0)
567 #ifdef HAVE_SYSTEMD_SD_JOURNAL_H
568 if (!strcmp(ErrorLog
, "syslog"))
570 cupsd_printer_t
*printer
= job
? (job
->printer
? job
->printer
: (job
->dest
? cupsdFindDest(job
->dest
) : NULL
)) : NULL
;
571 static const char * const job_states
[] =
572 { /* job-state strings */
582 va_start(ap
, message
);
587 status
= format_log_line(message
, ap2
);
595 sd_journal_send("MESSAGE=%s", log_line
,
596 "PRIORITY=%i", log_levels
[level
],
597 PWG_Event
"=JobStateChanged",
598 PWG_ServiceURI
"=%s", printer
? printer
->uri
: "",
599 PWG_JobID
"=%d", job
->id
,
600 PWG_JobState
"=%s", job
->state_value
< IPP_JSTATE_PENDING
? "" : job_states
[job
->state_value
- IPP_JSTATE_PENDING
],
601 PWG_JobImpressionsCompleted
"=%d", ippGetInteger(job
->impressions
, 0),
604 sd_journal_send("MESSAGE=%s", log_line
,
605 "PRIORITY=%i", log_levels
[level
],
610 #endif /* HAVE_SYSTEMD_SD_JOURNAL_H */
613 * Format and write the log message...
617 snprintf(jobmsg
, sizeof(jobmsg
), "[Job %d] %s", job
->id
, message
);
619 strlcpy(jobmsg
, message
, sizeof(jobmsg
));
621 va_start(ap
, message
);
626 status
= format_log_line(jobmsg
, ap2
);
635 if (job
&& level
> LogLevel
&& LogDebugHistory
> 0)
638 * Add message to the job history...
641 cupsd_joblog_t
*temp
; /* Copy of log message */
642 size_t log_len
= strlen(log_line
);
643 /* Length of log message */
645 if ((temp
= malloc(sizeof(cupsd_joblog_t
) + log_len
)) != NULL
)
647 temp
->time
= time(NULL
);
648 memcpy(temp
->message
, log_line
, log_len
+ 1);
652 job
->history
= cupsArrayNew(NULL
, NULL
);
654 if (job
->history
&& temp
)
656 cupsArrayAdd(job
->history
, temp
);
658 if (cupsArrayCount(job
->history
) > LogDebugHistory
)
661 * Remove excess messages...
664 temp
= cupsArrayFirst(job
->history
);
665 cupsArrayRemove(job
->history
, temp
);
674 else if (level
<= LogLevel
)
675 return (cupsdWriteErrorLog(level
, log_line
));
680 return (cupsdWriteErrorLog(CUPSD_LOG_ERROR
,
681 "Unable to allocate memory for log line."));
686 * 'cupsdLogMessage()' - Log a message to the error log file.
689 int /* O - 1 on success, 0 on error */
690 cupsdLogMessage(int level
, /* I - Log level */
691 const char *message
, /* I - printf-style message string */
692 ...) /* I - Additional args as needed */
694 va_list ap
, ap2
; /* Argument pointers */
695 int status
; /* Formatting status */
699 * See if we want to log this message...
702 if (TestConfigFile
&& level
<= CUPSD_LOG_WARN
)
704 va_start(ap
, message
);
706 vfprintf(stderr
, message
, ap
);
713 else if (!ErrorLog
&& level
<= CUPSD_LOG_WARN
)
715 va_start(ap
, message
);
717 #ifdef HAVE_SYSTEMD_SD_JOURNAL_H
718 sd_journal_printv(log_levels
[level
], message
, ap
);
720 #elif defined(HAVE_VSYSLOG)
721 vsyslog(LOG_LPR
| log_levels
[level
], message
, ap
);
724 vfprintf(stderr
, message
, ap
);
726 #endif /* HAVE_SYSTEMD_SD_JOURNAL_H */
732 else if (level
> LogLevel
|| !ErrorLog
)
735 #ifdef HAVE_SYSTEMD_SD_JOURNAL_H
736 else if (!strcmp(ErrorLog
, "syslog"))
738 va_start(ap
, message
);
739 sd_journal_printv(log_levels
[level
], message
, ap
);
743 #endif /* HAVE_SYSTEMD_SD_JOURNAL_H */
746 * Format and write the log message...
749 va_start(ap
, message
);
754 status
= format_log_line(message
, ap2
);
762 return (cupsdWriteErrorLog(level
, log_line
));
764 return (cupsdWriteErrorLog(CUPSD_LOG_ERROR
,
765 "Unable to allocate memory for log line!"));
770 * 'cupsdLogPage()' - Log a page to the page log file.
773 int /* O - 1 on success, 0 on error */
774 cupsdLogPage(cupsd_job_t
*job
, /* I - Job being printed */
775 const char *page
) /* I - Page being printed */
777 int i
; /* Looping var */
778 char buffer
[2048], /* Buffer for page log */
779 *bufptr
, /* Pointer into buffer */
780 name
[256]; /* Attribute name */
781 const char *format
, /* Pointer into PageLogFormat */
782 *nameend
; /* End of attribute name */
783 ipp_attribute_t
*attr
; /* Current attribute */
784 char number
[256]; /* Page number */
785 int copies
; /* Number of copies */
789 * Format the line going into the page log...
795 strlcpy(number
, "1", sizeof(number
));
797 sscanf(page
, "%255s%d", number
, &copies
);
799 for (format
= PageLogFormat
, bufptr
= buffer
; *format
; format
++)
807 case '%' : /* Literal % */
808 if (bufptr
< (buffer
+ sizeof(buffer
) - 1))
812 case 'p' : /* Printer name */
813 strlcpy(bufptr
, job
->dest
, sizeof(buffer
) - (size_t)(bufptr
- buffer
));
814 bufptr
+= strlen(bufptr
);
817 case 'j' : /* Job ID */
818 snprintf(bufptr
, sizeof(buffer
) - (size_t)(bufptr
- buffer
), "%d", job
->id
);
819 bufptr
+= strlen(bufptr
);
822 case 'u' : /* Username */
823 strlcpy(bufptr
, job
->username
? job
->username
: "-", sizeof(buffer
) - (size_t)(bufptr
- buffer
));
824 bufptr
+= strlen(bufptr
);
827 case 'T' : /* Date and time */
828 strlcpy(bufptr
, cupsdGetDateTime(NULL
, LogTimeFormat
), sizeof(buffer
) - (size_t)(bufptr
- buffer
));
829 bufptr
+= strlen(bufptr
);
832 case 'P' : /* Page number */
833 strlcpy(bufptr
, number
, sizeof(buffer
) - (size_t)(bufptr
- buffer
));
834 bufptr
+= strlen(bufptr
);
837 case 'C' : /* Number of copies */
838 snprintf(bufptr
, sizeof(buffer
) - (size_t)(bufptr
- buffer
), "%d", copies
);
839 bufptr
+= strlen(bufptr
);
842 case '{' : /* {attribute} */
843 if ((nameend
= strchr(format
, '}')) != NULL
&& (size_t)(nameend
- format
- 2) < (sizeof(name
) - 1))
846 * Pull the name from inside the brackets...
849 memcpy(name
, format
+ 1, (size_t)(nameend
- format
- 1));
850 name
[nameend
- format
- 1] = '\0';
854 attr
= ippFindAttribute(job
->attrs
, name
, IPP_TAG_ZERO
);
855 if (!attr
&& !strcmp(name
, "job-billing"))
858 * Handle alias "job-account-id" (which was standardized after
859 * "job-billing" was defined for CUPS...
862 attr
= ippFindAttribute(job
->attrs
, "job-account-id", IPP_TAG_ZERO
);
864 else if (!attr
&& !strcmp(name
, "media"))
867 * Handle alias "media-col" which uses dimensions instead of
871 attr
= ippFindAttribute(job
->attrs
, "media-col/media-size", IPP_TAG_BEGIN_COLLECTION
);
877 * Add the attribute value...
881 i
< attr
->num_values
&&
882 bufptr
< (buffer
+ sizeof(buffer
) - 1);
888 switch (attr
->value_tag
)
890 case IPP_TAG_INTEGER
:
892 snprintf(bufptr
, sizeof(buffer
) - (size_t)(bufptr
- buffer
), "%d", attr
->values
[i
].integer
);
893 bufptr
+= strlen(bufptr
);
896 case IPP_TAG_BOOLEAN
:
897 snprintf(bufptr
, sizeof(buffer
) - (size_t)(bufptr
- buffer
), "%d", attr
->values
[i
].boolean
);
898 bufptr
+= strlen(bufptr
);
901 case IPP_TAG_TEXTLANG
:
902 case IPP_TAG_NAMELANG
:
905 case IPP_TAG_KEYWORD
:
907 case IPP_TAG_URISCHEME
:
908 case IPP_TAG_CHARSET
:
909 case IPP_TAG_LANGUAGE
:
910 case IPP_TAG_MIMETYPE
:
911 strlcpy(bufptr
, attr
->values
[i
].string
.text
, sizeof(buffer
) - (size_t)(bufptr
- buffer
));
912 bufptr
+= strlen(bufptr
);
915 case IPP_TAG_BEGIN_COLLECTION
:
916 if (!strcmp(attr
->name
, "media-size"))
918 ipp_attribute_t
*x_dimension
= ippFindAttribute(ippGetCollection(attr
, 0), "x-dimension", IPP_TAG_INTEGER
);
919 ipp_attribute_t
*y_dimension
= ippFindAttribute(ippGetCollection(attr
, 0), "y-dimension", IPP_TAG_INTEGER
);
920 /* Media dimensions */
922 if (x_dimension
&& y_dimension
)
924 pwg_media_t
*pwg
= pwgMediaForSize(ippGetInteger(x_dimension
, 0), ippGetInteger(y_dimension
, 0));
926 strlcpy(bufptr
, pwg
->pwg
, sizeof(buffer
) - (size_t)(bufptr
- buffer
));
932 strlcpy(bufptr
, "???", sizeof(buffer
) - (size_t)(bufptr
- buffer
));
933 bufptr
+= strlen(bufptr
);
938 else if (bufptr
< (buffer
+ sizeof(buffer
) - 1))
944 if (bufptr
< (buffer
+ sizeof(buffer
) - 2))
952 else if (bufptr
< (buffer
+ sizeof(buffer
) - 1))
958 #ifdef HAVE_SYSTEMD_SD_JOURNAL_H
959 if (!strcmp(ErrorLog
, "syslog"))
961 static const char * const job_states
[] =
962 { /* job-state strings */
972 sd_journal_send("MESSAGE=%s", buffer
,
973 "PRIORITY=%i", LOG_INFO
,
974 PWG_Event
"=JobStateChanged",
975 PWG_ServiceURI
"=%s", job
->printer
->uri
,
976 PWG_JobID
"=%d", job
->id
,
977 PWG_JobState
"=%s", job_states
[job
->state_value
- IPP_JSTATE_PENDING
],
978 PWG_JobImpressionsCompleted
"=%d", ippGetInteger(job
->impressions
, 0),
983 #elif defined(HAVE_VSYSLOG)
985 * See if we are logging pages via syslog...
988 if (!strcmp(PageLog
, "syslog"))
990 syslog(LOG_INFO
, "%s", buffer
);
994 #endif /* HAVE_SYSTEMD_SD_JOURNAL_H */
997 * Not using syslog; check the log file...
1000 if (!cupsdCheckLogFile(&PageFile
, PageLog
))
1004 * Print a page log entry of the form:
1006 * printer user job-id [DD/MON/YYYY:HH:MM:SS +TTTT] page num-copies \
1010 cupsFilePrintf(PageFile
, "%s\n", buffer
);
1011 cupsFileFlush(PageFile
);
1018 * 'cupsdLogRequest()' - Log an HTTP request in Common Log Format.
1021 int /* O - 1 on success, 0 on error */
1022 cupsdLogRequest(cupsd_client_t
*con
, /* I - Request to log */
1023 http_status_t code
) /* I - Response code */
1025 char temp
[2048]; /* Temporary string for URI */
1026 static const char * const states
[] = /* HTTP client states... */
1046 * Filter requests as needed...
1049 if (AccessLogLevel
== CUPSD_ACCESSLOG_NONE
)
1051 else if (AccessLogLevel
< CUPSD_ACCESSLOG_ALL
)
1054 * Eliminate simple GET, POST, and PUT requests...
1057 if ((con
->operation
== HTTP_GET
&&
1058 strncmp(con
->uri
, "/admin/conf", 11) &&
1059 strncmp(con
->uri
, "/admin/log", 10)) ||
1060 (con
->operation
== HTTP_POST
&& !con
->request
&&
1061 strncmp(con
->uri
, "/admin", 6)) ||
1062 (con
->operation
!= HTTP_GET
&& con
->operation
!= HTTP_POST
&&
1063 con
->operation
!= HTTP_PUT
))
1066 if (con
->request
&& con
->response
&&
1067 (con
->response
->request
.status
.status_code
< IPP_REDIRECTION_OTHER_SITE
||
1068 con
->response
->request
.status
.status_code
== IPP_NOT_FOUND
))
1071 * Check successful requests...
1074 ipp_op_t op
= con
->request
->request
.op
.operation_id
;
1075 static cupsd_accesslog_t standard_ops
[] =
1077 CUPSD_ACCESSLOG_ALL
, /* reserved */
1078 CUPSD_ACCESSLOG_ALL
, /* reserved */
1079 CUPSD_ACCESSLOG_ACTIONS
,/* Print-Job */
1080 CUPSD_ACCESSLOG_ACTIONS
,/* Print-URI */
1081 CUPSD_ACCESSLOG_ACTIONS
,/* Validate-Job */
1082 CUPSD_ACCESSLOG_ACTIONS
,/* Create-Job */
1083 CUPSD_ACCESSLOG_ACTIONS
,/* Send-Document */
1084 CUPSD_ACCESSLOG_ACTIONS
,/* Send-URI */
1085 CUPSD_ACCESSLOG_ACTIONS
,/* Cancel-Job */
1086 CUPSD_ACCESSLOG_ALL
, /* Get-Job-Attributes */
1087 CUPSD_ACCESSLOG_ALL
, /* Get-Jobs */
1088 CUPSD_ACCESSLOG_ALL
, /* Get-Printer-Attributes */
1089 CUPSD_ACCESSLOG_ACTIONS
,/* Hold-Job */
1090 CUPSD_ACCESSLOG_ACTIONS
,/* Release-Job */
1091 CUPSD_ACCESSLOG_ACTIONS
,/* Restart-Job */
1092 CUPSD_ACCESSLOG_ALL
, /* reserved */
1093 CUPSD_ACCESSLOG_CONFIG
, /* Pause-Printer */
1094 CUPSD_ACCESSLOG_CONFIG
, /* Resume-Printer */
1095 CUPSD_ACCESSLOG_CONFIG
, /* Purge-Jobs */
1096 CUPSD_ACCESSLOG_CONFIG
, /* Set-Printer-Attributes */
1097 CUPSD_ACCESSLOG_ACTIONS
,/* Set-Job-Attributes */
1098 CUPSD_ACCESSLOG_CONFIG
, /* Get-Printer-Supported-Values */
1099 CUPSD_ACCESSLOG_ACTIONS
,/* Create-Printer-Subscription */
1100 CUPSD_ACCESSLOG_ACTIONS
,/* Create-Job-Subscription */
1101 CUPSD_ACCESSLOG_ALL
, /* Get-Subscription-Attributes */
1102 CUPSD_ACCESSLOG_ALL
, /* Get-Subscriptions */
1103 CUPSD_ACCESSLOG_ACTIONS
,/* Renew-Subscription */
1104 CUPSD_ACCESSLOG_ACTIONS
,/* Cancel-Subscription */
1105 CUPSD_ACCESSLOG_ALL
, /* Get-Notifications */
1106 CUPSD_ACCESSLOG_ACTIONS
,/* Send-Notifications */
1107 CUPSD_ACCESSLOG_ALL
, /* reserved */
1108 CUPSD_ACCESSLOG_ALL
, /* reserved */
1109 CUPSD_ACCESSLOG_ALL
, /* reserved */
1110 CUPSD_ACCESSLOG_ALL
, /* Get-Print-Support-Files */
1111 CUPSD_ACCESSLOG_CONFIG
, /* Enable-Printer */
1112 CUPSD_ACCESSLOG_CONFIG
, /* Disable-Printer */
1113 CUPSD_ACCESSLOG_CONFIG
, /* Pause-Printer-After-Current-Job */
1114 CUPSD_ACCESSLOG_ACTIONS
,/* Hold-New-Jobs */
1115 CUPSD_ACCESSLOG_ACTIONS
,/* Release-Held-New-Jobs */
1116 CUPSD_ACCESSLOG_CONFIG
, /* Deactivate-Printer */
1117 CUPSD_ACCESSLOG_CONFIG
, /* Activate-Printer */
1118 CUPSD_ACCESSLOG_CONFIG
, /* Restart-Printer */
1119 CUPSD_ACCESSLOG_CONFIG
, /* Shutdown-Printer */
1120 CUPSD_ACCESSLOG_CONFIG
, /* Startup-Printer */
1121 CUPSD_ACCESSLOG_ACTIONS
,/* Reprocess-Job */
1122 CUPSD_ACCESSLOG_ACTIONS
,/* Cancel-Current-Job */
1123 CUPSD_ACCESSLOG_ACTIONS
,/* Suspend-Current-Job */
1124 CUPSD_ACCESSLOG_ACTIONS
,/* Resume-Job */
1125 CUPSD_ACCESSLOG_ACTIONS
,/* Promote-Job */
1126 CUPSD_ACCESSLOG_ACTIONS
/* Schedule-Job-After */
1128 static cupsd_accesslog_t cups_ops
[] =
1130 CUPSD_ACCESSLOG_ALL
, /* CUPS-Get-Default */
1131 CUPSD_ACCESSLOG_ALL
, /* CUPS-Get-Printers */
1132 CUPSD_ACCESSLOG_CONFIG
, /* CUPS-Add-Modify-Printer */
1133 CUPSD_ACCESSLOG_CONFIG
, /* CUPS-Delete-Printer */
1134 CUPSD_ACCESSLOG_ALL
, /* CUPS-Get-Classes */
1135 CUPSD_ACCESSLOG_CONFIG
, /* CUPS-Add-Modify-Class */
1136 CUPSD_ACCESSLOG_CONFIG
, /* CUPS-Delete-Class */
1137 CUPSD_ACCESSLOG_CONFIG
, /* CUPS-Accept-Jobs */
1138 CUPSD_ACCESSLOG_CONFIG
, /* CUPS-Reject-Jobs */
1139 CUPSD_ACCESSLOG_CONFIG
, /* CUPS-Set-Default */
1140 CUPSD_ACCESSLOG_CONFIG
, /* CUPS-Get-Devices */
1141 CUPSD_ACCESSLOG_CONFIG
, /* CUPS-Get-PPDs */
1142 CUPSD_ACCESSLOG_ACTIONS
,/* CUPS-Move-Job */
1143 CUPSD_ACCESSLOG_ACTIONS
,/* CUPS-Authenticate-Job */
1144 CUPSD_ACCESSLOG_ALL
/* CUPS-Get-PPD */
1148 if ((op
<= IPP_SCHEDULE_JOB_AFTER
&& standard_ops
[op
] > AccessLogLevel
) ||
1149 (op
>= CUPS_GET_DEFAULT
&& op
<= CUPS_GET_PPD
&&
1150 cups_ops
[op
- CUPS_GET_DEFAULT
] > AccessLogLevel
))
1155 #ifdef HAVE_SYSTEMD_SD_JOURNAL_H
1156 if (!strcmp(AccessLog
, "syslog"))
1158 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
) : "-");
1162 #elif defined(HAVE_VSYSLOG)
1164 * See if we are logging accesses via syslog...
1167 if (!strcmp(AccessLog
, "syslog"))
1170 "REQUEST %s - %s \"%s %s HTTP/%d.%d\" %d " CUPS_LLFMT
" %s %s\n",
1171 con
->http
->hostname
, con
->username
[0] != '\0' ? con
->username
: "-",
1172 states
[con
->operation
], _httpEncodeURI(temp
, con
->uri
, sizeof(temp
)),
1173 con
->http
->version
/ 100, con
->http
->version
% 100,
1174 code
, CUPS_LLCAST con
->bytes
,
1176 ippOpString(con
->request
->request
.op
.operation_id
) : "-",
1178 ippErrorString(con
->response
->request
.status
.status_code
) : "-");
1182 #endif /* HAVE_SYSTEMD_SD_JOURNAL_H */
1185 * Not using syslog; check the log file...
1188 if (!cupsdCheckLogFile(&AccessFile
, AccessLog
))
1192 * Write a log of the request in "common log format"...
1195 cupsFilePrintf(AccessFile
,
1196 "%s - %s %s \"%s %s HTTP/%d.%d\" %d " CUPS_LLFMT
" %s %s\n",
1197 con
->http
->hostname
,
1198 con
->username
[0] != '\0' ? con
->username
: "-",
1199 cupsdGetDateTime(&(con
->start
), LogTimeFormat
),
1200 states
[con
->operation
],
1201 _httpEncodeURI(temp
, con
->uri
, sizeof(temp
)),
1202 con
->http
->version
/ 100, con
->http
->version
% 100,
1203 code
, CUPS_LLCAST con
->bytes
,
1205 ippOpString(con
->request
->request
.op
.operation_id
) : "-",
1207 ippErrorString(con
->response
->request
.status
.status_code
) :
1210 cupsFileFlush(AccessFile
);
1217 * 'cupsdWriteErrorLog()' - Write a line to the ErrorLog.
1220 int /* O - 1 on success, 0 on failure */
1221 cupsdWriteErrorLog(int level
, /* I - Log level */
1222 const char *message
) /* I - Message string */
1224 int ret
= 1; /* Return value */
1225 static const char levels
[] = /* Log levels... */
1240 #ifdef HAVE_SYSTEMD_SD_JOURNAL_H
1241 if (!strcmp(ErrorLog
, "syslog"))
1243 sd_journal_print(log_levels
[level
], "%s", message
);
1247 #elif defined(HAVE_VSYSLOG)
1249 * See if we are logging errors via syslog...
1252 if (!strcmp(ErrorLog
, "syslog"))
1254 syslog(log_levels
[level
], "%s", message
);
1257 #endif /* HAVE_SYSTEMD_SD_JOURNAL_H */
1260 * Not using syslog; check the log file...
1263 _cupsMutexLock(&log_mutex
);
1265 if (!cupsdCheckLogFile(&ErrorFile
, ErrorLog
))
1272 * Write the log message...
1275 cupsFilePrintf(ErrorFile
, "%c %s %s\n", levels
[level
],
1276 cupsdGetDateTime(NULL
, LogTimeFormat
), message
);
1277 cupsFileFlush(ErrorFile
);
1280 _cupsMutexUnlock(&log_mutex
);
1287 * 'format_log_line()' - Format a line for a log file.
1289 * This function resizes a global string buffer as needed. Each call returns
1290 * a pointer to this buffer, so the contents are only good until the next call
1291 * to format_log_line()...
1294 static int /* O - -1 for fatal, 0 for retry, 1 for success */
1295 format_log_line(const char *message
, /* I - Printf-style format string */
1296 va_list ap
) /* I - Argument list */
1298 ssize_t len
; /* Length of formatted line */
1302 * Allocate the line buffer as needed...
1307 log_linesize
= 8192;
1308 log_line
= malloc(log_linesize
);
1315 * Format the log message...
1318 len
= _cups_safe_vsnprintf(log_line
, log_linesize
, message
, ap
);
1321 * Resize the buffer as needed...
1324 if ((size_t)len
>= log_linesize
&& log_linesize
< 65536)
1326 char *temp
; /* Temporary string pointer */
1332 else if (len
> 65536)
1335 temp
= realloc(log_line
, (size_t)len
);
1340 log_linesize
= (size_t)len
;