2 * "$Id: log.c 7918 2008-09-08 22:03:01Z mike $"
4 * Log file routines for the Common UNIX Printing System (CUPS).
6 * Copyright 2007-2009 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 * cupsdGetDateTime() - Returns a pointer to a date/time string.
18 * cupsdLogGSSMessage() - Log a GSSAPI error...
19 * cupsdLogJob() - Log a job message.
20 * cupsdLogMessage() - Log a message to the error log file.
21 * cupsdLogPage() - Log a page to the page log file.
22 * cupsdLogRequest() - Log an HTTP request in Common Log Format.
23 * cupsdWriteErrorLog() - Write a line to the ErrorLog.
24 * check_log_file() - Open/rotate a log file if it needs it.
25 * format_log_line() - Format a line for a log file.
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 check_log_file(cups_file_t
**lf
, const char *logname
);
50 static int format_log_line(const char *message
, va_list ap
);
54 * 'cupsdGetDateTime()' - Returns a pointer to a date/time string.
57 char * /* O - Date/time string */
58 cupsdGetDateTime(struct timeval
*t
, /* I - Time value or NULL for current */
59 cupsd_time_t format
) /* I - Format to use */
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 */
66 static const char * const months
[12] =/* Months */
84 * Make sure we have a valid time...
89 gettimeofday(&curtime
, NULL
);
93 if (t
->tv_sec
!= last_time
.tv_sec
||
94 (LogTimeFormat
== CUPSD_TIME_USECS
&& t
->tv_usec
!= last_time
.tv_usec
))
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!
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.
109 * (*BSD and Darwin store the timezone offset in the tm structure)
112 date
= localtime(&(t
->tv_sec
));
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);
121 timezone
/ 3600, (timezone
/ 60) % 60);
122 #endif /* HAVE_TM_GMTOFF */
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
,
126 date
->tm_hour
, date
->tm_min
, date
->tm_sec
, (int)t
->tv_usec
,
127 #ifdef HAVE_TM_GMTOFF
128 date
->tm_gmtoff
/ 3600, (date
->tm_gmtoff
/ 60) % 60);
130 timezone
/ 3600, (timezone
/ 60) % 60);
131 #endif /* HAVE_TM_GMTOFF */
140 * 'cupsdLogGSSMessage()' - Log a GSSAPI error...
143 int /* O - 1 on success, 0 on error */
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 */
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 */
162 err_major_status
= gss_display_status(&err_minor_status
,
167 &major_status_string
);
169 if (!GSS_ERROR(err_major_status
))
170 gss_display_status(&err_minor_status
, minor_status
, GSS_C_MECH_CODE
,
171 GSS_C_NULL_OID
, &msg_ctx
, &minor_status_string
);
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
);
181 #endif /* HAVE_GSSAPI */
185 * 'cupsdLogJob()' - Log a job message.
188 int /* O - 1 on success, 0 on error */
189 cupsdLogJob(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 */
194 va_list ap
; /* Argument pointer */
195 char jobmsg
[1024]; /* Format string for job message */
196 int status
; /* Formatting status */
200 * See if we want to log this message...
203 if (TestConfigFile
|| !ErrorLog
)
206 if ((level
> LogLevel
||
207 (level
== CUPSD_LOG_INFO
&& LogLevel
< CUPSD_LOG_DEBUG
)) &&
208 LogDebugHistory
<= 0)
212 * Format and write the log message...
215 snprintf(jobmsg
, sizeof(jobmsg
), "[Job %d] %s", job
->id
, message
);
219 va_start(ap
, message
);
220 status
= format_log_line(jobmsg
, ap
);
227 if ((level
> LogLevel
||
228 (level
== CUPSD_LOG_INFO
&& LogLevel
< CUPSD_LOG_DEBUG
)) &&
232 * Add message to the job history...
235 cupsd_joblog_t
*temp
; /* Copy of log message */
238 if ((temp
= malloc(sizeof(cupsd_joblog_t
) + strlen(log_line
))) != NULL
)
240 temp
->time
= time(NULL
);
241 strcpy(temp
->message
, log_line
);
245 job
->history
= cupsArrayNew(NULL
, NULL
);
247 if (job
->history
&& temp
)
249 cupsArrayAdd(job
->history
, temp
);
251 if (cupsArrayCount(job
->history
) > LogDebugHistory
)
254 * Remove excess messages...
257 temp
= cupsArrayFirst(job
->history
);
258 cupsArrayRemove(job
->history
, temp
);
267 else if (level
<= LogLevel
&&
268 (level
!= CUPSD_LOG_INFO
|| LogLevel
>= CUPSD_LOG_DEBUG
))
269 return (cupsdWriteErrorLog(level
, log_line
));
274 return (cupsdWriteErrorLog(CUPSD_LOG_ERROR
,
275 "Unable to allocate memory for log line!"));
280 * 'cupsdLogMessage()' - Log a message to the error log file.
283 int /* O - 1 on success, 0 on error */
284 cupsdLogMessage(int level
, /* I - Log level */
285 const char *message
, /* I - printf-style message string */
286 ...) /* I - Additional args as needed */
288 va_list ap
; /* Argument pointer */
289 int status
; /* Formatting status */
293 * See if we want to log this message...
298 if (level
<= CUPSD_LOG_WARN
)
300 va_start(ap
, message
);
301 vfprintf(stderr
, message
, ap
);
309 if (level
> LogLevel
|| !ErrorLog
)
313 * Format and write the log message...
318 va_start(ap
, message
);
319 status
= format_log_line(message
, ap
);
325 return (cupsdWriteErrorLog(level
, log_line
));
327 return (cupsdWriteErrorLog(CUPSD_LOG_ERROR
,
328 "Unable to allocate memory for log line!"));
333 * 'cupsdLogPage()' - Log a page to the page log file.
336 int /* O - 1 on success, 0 on error */
337 cupsdLogPage(cupsd_job_t
*job
, /* I - Job being printed */
338 const char *page
) /* I - Page being printed */
340 int i
; /* Looping var */
341 char buffer
[2048], /* Buffer for page log */
342 *bufptr
, /* Pointer into buffer */
343 name
[256]; /* Attribute name */
344 const char *format
, /* Pointer into PageLogFormat */
345 *nameend
; /* End of attribute name */
346 ipp_attribute_t
*attr
; /* Current attribute */
347 int number
; /* Page number */
348 char copies
[256]; /* Number of copies */
352 * Format the line going into the page log...
360 sscanf(page
, "%d%255s", &number
, copies
);
362 for (format
= PageLogFormat
, bufptr
= buffer
; *format
; format
++)
370 case '%' : /* Literal % */
371 if (bufptr
< (buffer
+ sizeof(buffer
) - 1))
375 case 'p' : /* Printer name */
376 strlcpy(bufptr
, job
->printer
->name
,
377 sizeof(buffer
) - (bufptr
- buffer
));
378 bufptr
+= strlen(bufptr
);
381 case 'j' : /* Job ID */
382 snprintf(bufptr
, sizeof(buffer
) - (bufptr
- buffer
), "%d", job
->id
);
383 bufptr
+= strlen(bufptr
);
386 case 'u' : /* Username */
387 strlcpy(bufptr
, job
->username
? job
->username
: "-",
388 sizeof(buffer
) - (bufptr
- buffer
));
389 bufptr
+= strlen(bufptr
);
392 case 'T' : /* Date and time */
393 strlcpy(bufptr
, cupsdGetDateTime(NULL
, LogTimeFormat
),
394 sizeof(buffer
) - (bufptr
- buffer
));
395 bufptr
+= strlen(bufptr
);
398 case 'P' : /* Page number */
399 snprintf(bufptr
, sizeof(buffer
) - (bufptr
- buffer
), "%d", number
);
400 bufptr
+= strlen(bufptr
);
403 case 'C' : /* Number of copies */
404 strlcpy(bufptr
, copies
, sizeof(buffer
) - (bufptr
- buffer
));
405 bufptr
+= strlen(bufptr
);
408 case '{' : /* {attribute} */
409 if ((nameend
= strchr(format
, '}')) != NULL
&&
410 (nameend
- format
- 2) < (sizeof(name
) - 1))
413 * Pull the name from inside the brackets...
416 memcpy(name
, format
+ 1, nameend
- format
- 1);
417 name
[nameend
- format
- 1] = '\0';
421 if ((attr
= ippFindAttribute(job
->attrs
, name
,
422 IPP_TAG_ZERO
)) != NULL
)
425 * Add the attribute value...
429 i
< attr
->num_values
&&
430 bufptr
< (buffer
+ sizeof(buffer
) - 1);
436 switch (attr
->value_tag
)
438 case IPP_TAG_INTEGER
:
440 snprintf(bufptr
, sizeof(buffer
) - (bufptr
- buffer
),
441 "%d", attr
->values
[i
].integer
);
442 bufptr
+= strlen(bufptr
);
445 case IPP_TAG_BOOLEAN
:
446 snprintf(bufptr
, sizeof(buffer
) - (bufptr
- buffer
),
447 "%d", attr
->values
[i
].boolean
);
448 bufptr
+= strlen(bufptr
);
451 case IPP_TAG_TEXTLANG
:
452 case IPP_TAG_NAMELANG
:
455 case IPP_TAG_KEYWORD
:
457 case IPP_TAG_URISCHEME
:
458 case IPP_TAG_CHARSET
:
459 case IPP_TAG_LANGUAGE
:
460 case IPP_TAG_MIMETYPE
:
461 strlcpy(bufptr
, attr
->values
[i
].string
.text
,
462 sizeof(buffer
) - (bufptr
- buffer
));
463 bufptr
+= strlen(bufptr
);
467 strlcpy(bufptr
, "???",
468 sizeof(buffer
) - (bufptr
- buffer
));
469 bufptr
+= strlen(bufptr
);
474 else if (bufptr
< (buffer
+ sizeof(buffer
) - 1))
480 if (bufptr
< (buffer
+ sizeof(buffer
) - 2))
488 else if (bufptr
< (buffer
+ sizeof(buffer
) - 1))
496 * See if we are logging pages via syslog...
499 if (!strcmp(PageLog
, "syslog"))
501 syslog(LOG_INFO
, "%s", buffer
);
505 #endif /* HAVE_VSYSLOG */
508 * Not using syslog; check the log file...
511 if (!check_log_file(&PageFile
, PageLog
))
515 * Print a page log entry of the form:
517 * printer user job-id [DD/MON/YYYY:HH:MM:SS +TTTT] page num-copies \
521 cupsFilePrintf(PageFile
, "%s\n", buffer
);
522 cupsFileFlush(PageFile
);
529 * 'cupsdLogRequest()' - Log an HTTP request in Common Log Format.
532 int /* O - 1 on success, 0 on error */
533 cupsdLogRequest(cupsd_client_t
*con
, /* I - Request to log */
534 http_status_t code
) /* I - Response code */
536 char temp
[2048]; /* Temporary string for URI */
537 static const char * const states
[] = /* HTTP client states... */
557 * Filter requests as needed...
560 if (AccessLogLevel
< CUPSD_ACCESSLOG_ALL
)
563 * Eliminate simple GET requests...
566 if ((con
->operation
== HTTP_GET
&&
567 strncmp(con
->uri
, "/admin/conf", 11) &&
568 strncmp(con
->uri
, "/admin/log", 10)) ||
569 (con
->operation
== HTTP_POST
&& !con
->request
&&
570 strncmp(con
->uri
, "/admin", 6)) ||
571 (con
->operation
!= HTTP_POST
&& con
->operation
!= HTTP_PUT
))
574 if (con
->request
&& con
->response
&&
575 (con
->response
->request
.status
.status_code
< IPP_REDIRECTION_OTHER_SITE
||
576 con
->response
->request
.status
.status_code
== IPP_NOT_FOUND
))
579 * Check successful requests...
582 ipp_op_t op
= con
->request
->request
.op
.operation_id
;
583 static cupsd_accesslog_t standard_ops
[] =
585 CUPSD_ACCESSLOG_ALL
, /* reserved */
586 CUPSD_ACCESSLOG_ALL
, /* reserved */
587 CUPSD_ACCESSLOG_ACTIONS
,/* Print-Job */
588 CUPSD_ACCESSLOG_ACTIONS
,/* Print-URI */
589 CUPSD_ACCESSLOG_ACTIONS
,/* Validate-Job */
590 CUPSD_ACCESSLOG_ACTIONS
,/* Create-Job */
591 CUPSD_ACCESSLOG_ACTIONS
,/* Send-Document */
592 CUPSD_ACCESSLOG_ACTIONS
,/* Send-URI */
593 CUPSD_ACCESSLOG_ACTIONS
,/* Cancel-Job */
594 CUPSD_ACCESSLOG_ALL
, /* Get-Job-Attributes */
595 CUPSD_ACCESSLOG_ALL
, /* Get-Jobs */
596 CUPSD_ACCESSLOG_ALL
, /* Get-Printer-Attributes */
597 CUPSD_ACCESSLOG_ACTIONS
,/* Hold-Job */
598 CUPSD_ACCESSLOG_ACTIONS
,/* Release-Job */
599 CUPSD_ACCESSLOG_ACTIONS
,/* Restart-Job */
600 CUPSD_ACCESSLOG_ALL
, /* reserved */
601 CUPSD_ACCESSLOG_CONFIG
, /* Pause-Printer */
602 CUPSD_ACCESSLOG_CONFIG
, /* Resume-Printer */
603 CUPSD_ACCESSLOG_CONFIG
, /* Purge-Jobs */
604 CUPSD_ACCESSLOG_CONFIG
, /* Set-Printer-Attributes */
605 CUPSD_ACCESSLOG_ACTIONS
,/* Set-Job-Attributes */
606 CUPSD_ACCESSLOG_CONFIG
, /* Get-Printer-Supported-Values */
607 CUPSD_ACCESSLOG_ACTIONS
,/* Create-Printer-Subscription */
608 CUPSD_ACCESSLOG_ACTIONS
,/* Create-Job-Subscription */
609 CUPSD_ACCESSLOG_ALL
, /* Get-Subscription-Attributes */
610 CUPSD_ACCESSLOG_ALL
, /* Get-Subscriptions */
611 CUPSD_ACCESSLOG_ACTIONS
,/* Renew-Subscription */
612 CUPSD_ACCESSLOG_ACTIONS
,/* Cancel-Subscription */
613 CUPSD_ACCESSLOG_ALL
, /* Get-Notifications */
614 CUPSD_ACCESSLOG_ACTIONS
,/* Send-Notifications */
615 CUPSD_ACCESSLOG_ALL
, /* reserved */
616 CUPSD_ACCESSLOG_ALL
, /* reserved */
617 CUPSD_ACCESSLOG_ALL
, /* reserved */
618 CUPSD_ACCESSLOG_ALL
, /* Get-Print-Support-Files */
619 CUPSD_ACCESSLOG_CONFIG
, /* Enable-Printer */
620 CUPSD_ACCESSLOG_CONFIG
, /* Disable-Printer */
621 CUPSD_ACCESSLOG_CONFIG
, /* Pause-Printer-After-Current-Job */
622 CUPSD_ACCESSLOG_ACTIONS
,/* Hold-New-Jobs */
623 CUPSD_ACCESSLOG_ACTIONS
,/* Release-Held-New-Jobs */
624 CUPSD_ACCESSLOG_CONFIG
, /* Deactivate-Printer */
625 CUPSD_ACCESSLOG_CONFIG
, /* Activate-Printer */
626 CUPSD_ACCESSLOG_CONFIG
, /* Restart-Printer */
627 CUPSD_ACCESSLOG_CONFIG
, /* Shutdown-Printer */
628 CUPSD_ACCESSLOG_CONFIG
, /* Startup-Printer */
629 CUPSD_ACCESSLOG_ACTIONS
,/* Reprocess-Job */
630 CUPSD_ACCESSLOG_ACTIONS
,/* Cancel-Current-Job */
631 CUPSD_ACCESSLOG_ACTIONS
,/* Suspend-Current-Job */
632 CUPSD_ACCESSLOG_ACTIONS
,/* Resume-Job */
633 CUPSD_ACCESSLOG_ACTIONS
,/* Promote-Job */
634 CUPSD_ACCESSLOG_ACTIONS
/* Schedule-Job-After */
636 static cupsd_accesslog_t cups_ops
[] =
638 CUPSD_ACCESSLOG_ALL
, /* CUPS-Get-Default */
639 CUPSD_ACCESSLOG_ALL
, /* CUPS-Get-Printers */
640 CUPSD_ACCESSLOG_CONFIG
, /* CUPS-Add-Modify-Printer */
641 CUPSD_ACCESSLOG_CONFIG
, /* CUPS-Delete-Printer */
642 CUPSD_ACCESSLOG_ALL
, /* CUPS-Get-Classes */
643 CUPSD_ACCESSLOG_CONFIG
, /* CUPS-Add-Modify-Class */
644 CUPSD_ACCESSLOG_CONFIG
, /* CUPS-Delete-Class */
645 CUPSD_ACCESSLOG_CONFIG
, /* CUPS-Accept-Jobs */
646 CUPSD_ACCESSLOG_CONFIG
, /* CUPS-Reject-Jobs */
647 CUPSD_ACCESSLOG_CONFIG
, /* CUPS-Set-Default */
648 CUPSD_ACCESSLOG_CONFIG
, /* CUPS-Get-Devices */
649 CUPSD_ACCESSLOG_CONFIG
, /* CUPS-Get-PPDs */
650 CUPSD_ACCESSLOG_ACTIONS
,/* CUPS-Move-Job */
651 CUPSD_ACCESSLOG_ACTIONS
,/* CUPS-Authenticate-Job */
652 CUPSD_ACCESSLOG_ALL
/* CUPS-Get-PPD */
656 if ((op
<= IPP_SCHEDULE_JOB_AFTER
&& standard_ops
[op
] > AccessLogLevel
) ||
657 (op
>= CUPS_GET_DEFAULT
&& op
<= CUPS_GET_PPD
&&
658 cups_ops
[op
- CUPS_GET_DEFAULT
] > AccessLogLevel
))
665 * See if we are logging accesses via syslog...
668 if (!strcmp(AccessLog
, "syslog"))
671 "REQUEST %s - %s \"%s %s HTTP/%d.%d\" %d " CUPS_LLFMT
" %s %s\n",
672 con
->http
.hostname
, con
->username
[0] != '\0' ? con
->username
: "-",
673 states
[con
->operation
], _httpEncodeURI(temp
, con
->uri
, sizeof(temp
)),
674 con
->http
.version
/ 100, con
->http
.version
% 100,
675 code
, CUPS_LLCAST con
->bytes
,
677 ippOpString(con
->request
->request
.op
.operation_id
) : "-",
679 ippErrorString(con
->response
->request
.status
.status_code
) : "-");
683 #endif /* HAVE_VSYSLOG */
686 * Not using syslog; check the log file...
689 if (!check_log_file(&AccessFile
, AccessLog
))
693 * Write a log of the request in "common log format"...
696 cupsFilePrintf(AccessFile
,
697 "%s - %s %s \"%s %s HTTP/%d.%d\" %d " CUPS_LLFMT
" %s %s\n",
698 con
->http
.hostname
, con
->username
[0] != '\0' ? con
->username
: "-",
699 cupsdGetDateTime(&(con
->start
), LogTimeFormat
),
700 states
[con
->operation
],
701 _httpEncodeURI(temp
, con
->uri
, sizeof(temp
)),
702 con
->http
.version
/ 100, con
->http
.version
% 100,
703 code
, CUPS_LLCAST con
->bytes
,
705 ippOpString(con
->request
->request
.op
.operation_id
) : "-",
707 ippErrorString(con
->response
->request
.status
.status_code
) :
710 cupsFileFlush(AccessFile
);
717 * 'cupsdWriteErrorLog()' - Write a line to the ErrorLog.
720 int /* O - 1 on success, 0 on failure */
721 cupsdWriteErrorLog(int level
, /* I - Log level */
722 const char *message
) /* I - Message string */
724 static const char levels
[] = /* Log levels... */
738 static const int syslevels
[] = /* SYSLOG levels... */
751 #endif /* HAVE_VSYSLOG */
756 * See if we are logging errors via syslog...
759 if (!strcmp(ErrorLog
, "syslog"))
761 syslog(syslevels
[level
], "%s", message
);
764 #endif /* HAVE_VSYSLOG */
767 * Not using syslog; check the log file...
770 if (!check_log_file(&ErrorFile
, ErrorLog
))
774 * Write the log message...
777 cupsFilePrintf(ErrorFile
, "%c %s %s\n", levels
[level
],
778 cupsdGetDateTime(NULL
, LogTimeFormat
), message
);
779 cupsFileFlush(ErrorFile
);
786 * 'check_log_file()' - Open/rotate a log file if it needs it.
789 static int /* O - 1 if log file open */
790 check_log_file(cups_file_t
**lf
, /* IO - Log file */
791 const char *logname
) /* I - Log filename */
793 char backname
[1024], /* Backup log filename */
794 filename
[1024], /* Formatted log filename */
795 *ptr
; /* Pointer into filename */
796 const char *logptr
; /* Pointer into log filename */
800 * See if we have a log file to check...
803 if (!lf
|| !logname
|| !logname
[0])
807 * Format the filename as needed...
811 (strncmp(logname
, "/dev/", 5) && cupsFileTell(*lf
) > MaxLogSize
&&
815 * Handle format strings...
818 filename
[sizeof(filename
) - 1] = '\0';
820 if (logname
[0] != '/')
822 strlcpy(filename
, ServerRoot
, sizeof(filename
));
823 strlcat(filename
, "/", sizeof(filename
));
828 for (logptr
= logname
, ptr
= filename
+ strlen(filename
);
829 *logptr
&& ptr
< (filename
+ sizeof(filename
) - 1);
841 * Insert the server name...
844 strlcpy(ptr
, ServerName
, sizeof(filename
) - (ptr
- filename
));
850 * Otherwise just insert the character...
863 * See if the log file is open...
869 * Nope, open the log file...
872 if ((*lf
= cupsFileOpen(filename
, "a")) == NULL
)
875 * If the file is in CUPS_LOGDIR then try to create a missing directory...
878 if (!strncmp(filename
, CUPS_LOGDIR
, strlen(CUPS_LOGDIR
)))
881 * Try updating the permissions of the containing log directory, using
882 * the log file permissions as a basis...
885 int log_dir_perm
= 0300 | LogFilePerm
;
886 /* LogFilePerm + owner write/search */
887 if (log_dir_perm
& 0040)
888 log_dir_perm
|= 0010; /* Add group search */
889 if (log_dir_perm
& 0004)
890 log_dir_perm
|= 0001; /* Add other search */
892 cupsdCheckPermissions(CUPS_LOGDIR
, NULL
, log_dir_perm
, RunUser
, Group
,
895 *lf
= cupsFileOpen(filename
, "a");
900 syslog(LOG_ERR
, "Unable to open log file \"%s\" - %s", filename
,
903 if (FatalErrors
& CUPSD_FATAL_LOG
)
904 cupsdEndProcess(getpid(), 0);
910 if (strncmp(filename
, "/dev/", 5))
913 * Change ownership and permissions of non-device logs...
916 fchown(cupsFileNumber(*lf
), RunUser
, Group
);
917 fchmod(cupsFileNumber(*lf
), LogFilePerm
);
922 * Do we need to rotate the log?
925 if (strncmp(logname
, "/dev/", 5) && cupsFileTell(*lf
) > MaxLogSize
&&
934 strcpy(backname
, filename
);
935 strlcat(backname
, ".O", sizeof(backname
));
938 rename(filename
, backname
);
940 if ((*lf
= cupsFileOpen(filename
, "a")) == NULL
)
942 syslog(LOG_ERR
, "Unable to open log file \"%s\" - %s", filename
,
945 if (FatalErrors
& CUPSD_FATAL_LOG
)
946 cupsdEndProcess(getpid(), 0);
952 * Change ownership and permissions of non-device logs...
955 fchown(cupsFileNumber(*lf
), RunUser
, Group
);
956 fchmod(cupsFileNumber(*lf
), LogFilePerm
);
964 * 'format_log_line()' - Format a line for a log file.
966 * This function resizes a global string buffer as needed. Each call returns
967 * a pointer to this buffer, so the contents are only good until the next call
968 * to format_log_line()...
971 static int /* O - -1 for fatal, 0 for retry, 1 for success */
972 format_log_line(const char *message
, /* I - Printf-style format string */
973 va_list ap
) /* I - Argument list */
975 int len
; /* Length of formatted line */
979 * Allocate the line buffer as needed...
985 log_line
= malloc(log_linesize
);
992 * Format the log message...
995 len
= vsnprintf(log_line
, log_linesize
, message
, ap
);
998 * Resize the buffer as needed...
1001 if (len
>= log_linesize
&& log_linesize
< 65536)
1003 char *temp
; /* Temporary string pointer */
1010 else if (len
> 65536)
1013 temp
= realloc(log_line
, len
);
1029 * End of "$Id: log.c 7918 2008-09-08 22:03:01Z mike $".