2 * "$Id: log.c 7699 2008-06-27 20:44:23Z mike $"
4 * Log file routines for the Common UNIX Printing System (CUPS).
6 * Copyright 2007-2008 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 char *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(time_t t
) /* I - Time value */
60 struct tm
*date
; /* Date/time value */
61 static time_t last_time
= -1; /* Last time value */
62 static char s
[1024]; /* Date/time string */
63 static const char * const months
[12] =/* Months */
81 * Make sure we have a valid time...
92 * Get the date and time from the UNIX time value, and then format it
93 * into a string. Note that we *can't* use the strftime() function since
94 * it is localized and will seriously confuse automatic programs if the
95 * month names are in the wrong language!
97 * Also, we use the "timezone" variable that contains the current timezone
98 * offset from GMT in seconds so that we are reporting local time in the
99 * log files. If you want GMT, set the TZ environment variable accordingly
100 * before starting the scheduler.
102 * (*BSD and Darwin store the timezone offset in the tm structure)
105 date
= localtime(&t
);
107 snprintf(s
, sizeof(s
), "[%02d/%s/%04d:%02d:%02d:%02d %+03ld%02ld]",
108 date
->tm_mday
, months
[date
->tm_mon
], 1900 + date
->tm_year
,
109 date
->tm_hour
, date
->tm_min
, date
->tm_sec
,
110 #ifdef HAVE_TM_GMTOFF
111 date
->tm_gmtoff
/ 3600, (date
->tm_gmtoff
/ 60) % 60);
113 timezone
/ 3600, (timezone
/ 60) % 60);
114 #endif /* HAVE_TM_GMTOFF */
123 * 'cupsdLogGSSMessage()' - Log a GSSAPI error...
126 int /* O - 1 on success, 0 on error */
128 int level
, /* I - Log level */
129 int major_status
, /* I - Major GSSAPI status */
130 int minor_status
, /* I - Minor GSSAPI status */
131 const char *message
, /* I - printf-style message string */
132 ...) /* I - Additional args as needed */
134 OM_uint32 err_major_status
, /* Major status code for display */
135 err_minor_status
; /* Minor status code for display */
136 OM_uint32 msg_ctx
; /* Message context */
137 gss_buffer_desc major_status_string
= GSS_C_EMPTY_BUFFER
,
138 /* Major status message */
139 minor_status_string
= GSS_C_EMPTY_BUFFER
;
140 /* Minor status message */
141 int ret
; /* Return value */
145 err_major_status
= gss_display_status(&err_minor_status
,
150 &major_status_string
);
152 if (!GSS_ERROR(err_major_status
))
153 err_major_status
= gss_display_status(&err_minor_status
,
158 &minor_status_string
);
160 ret
= cupsdLogMessage(level
, "%s: %s, %s", message
,
161 (char *)major_status_string
.value
,
162 (char *)minor_status_string
.value
);
163 gss_release_buffer(&err_minor_status
, &major_status_string
);
164 gss_release_buffer(&err_minor_status
, &minor_status_string
);
168 #endif /* HAVE_GSSAPI */
172 * 'cupsdLogJob()' - Log a job message.
175 int /* O - 1 on success, 0 on error */
176 cupsdLogJob(cupsd_job_t
*job
, /* I - Job */
177 int level
, /* I - Log level */
178 const char *message
, /* I - Printf-style message string */
179 ...) /* I - Additional arguments as needed */
181 va_list ap
; /* Argument pointer */
182 char jobmsg
[1024], /* Format string for job message */
183 *line
; /* Message line */
187 * See if we want to log this message...
190 if (TestConfigFile
|| level
> LogLevel
|| !ErrorLog
)
194 * Format and write the log message...
197 snprintf(jobmsg
, sizeof(jobmsg
), "[Job %d] %s", job
->id
, message
);
199 va_start(ap
, message
);
200 line
= format_log_line(jobmsg
, ap
);
204 return (cupsdWriteErrorLog(level
, line
));
206 return (cupsdWriteErrorLog(CUPSD_LOG_ERROR
,
207 "Unable to allocate memory for log line!"));
212 * 'cupsdLogMessage()' - Log a message to the error log file.
215 int /* O - 1 on success, 0 on error */
216 cupsdLogMessage(int level
, /* I - Log level */
217 const char *message
, /* I - printf-style message string */
218 ...) /* I - Additional args as needed */
220 va_list ap
; /* Argument pointer */
221 char *line
; /* Message line */
225 * See if we want to log this message...
230 if (level
<= CUPSD_LOG_WARN
)
232 va_start(ap
, message
);
233 vfprintf(stderr
, message
, ap
);
241 if (level
> LogLevel
|| !ErrorLog
)
245 * Format and write the log message...
248 va_start(ap
, message
);
249 line
= format_log_line(message
, ap
);
253 return (cupsdWriteErrorLog(level
, line
));
255 return (cupsdWriteErrorLog(CUPSD_LOG_ERROR
,
256 "Unable to allocate memory for log line!"));
261 * 'cupsdLogPage()' - Log a page to the page log file.
264 int /* O - 1 on success, 0 on error */
265 cupsdLogPage(cupsd_job_t
*job
, /* I - Job being printed */
266 const char *page
) /* I - Page being printed */
268 int i
; /* Looping var */
269 char buffer
[2048], /* Buffer for page log */
270 *bufptr
, /* Pointer into buffer */
271 name
[256]; /* Attribute name */
272 const char *format
, /* Pointer into PageLogFormat */
273 *nameend
; /* End of attribute name */
274 ipp_attribute_t
*attr
; /* Current attribute */
275 int number
; /* Page number */
276 char copies
[256]; /* Number of copies */
280 * Format the line going into the page log...
288 sscanf(page
, "%d%255s", &number
, copies
);
290 for (format
= PageLogFormat
, bufptr
= buffer
; *format
; format
++)
298 case '%' : /* Literal % */
299 if (bufptr
< (buffer
+ sizeof(buffer
) - 1))
303 case 'p' : /* Printer name */
304 strlcpy(bufptr
, job
->printer
->name
,
305 sizeof(buffer
) - (bufptr
- buffer
));
306 bufptr
+= strlen(bufptr
);
309 case 'j' : /* Job ID */
310 snprintf(bufptr
, sizeof(buffer
) - (bufptr
- buffer
), "%d", job
->id
);
311 bufptr
+= strlen(bufptr
);
314 case 'u' : /* Username */
315 strlcpy(bufptr
, job
->username
? job
->username
: "-",
316 sizeof(buffer
) - (bufptr
- buffer
));
317 bufptr
+= strlen(bufptr
);
320 case 'T' : /* Date and time */
321 strlcpy(bufptr
, cupsdGetDateTime(time(NULL
)),
322 sizeof(buffer
) - (bufptr
- buffer
));
323 bufptr
+= strlen(bufptr
);
326 case 'P' : /* Page number */
327 snprintf(bufptr
, sizeof(buffer
) - (bufptr
- buffer
), "%d", number
);
328 bufptr
+= strlen(bufptr
);
331 case 'C' : /* Number of copies */
332 strlcpy(bufptr
, copies
, sizeof(buffer
) - (bufptr
- buffer
));
333 bufptr
+= strlen(bufptr
);
336 case '{' : /* {attribute} */
337 if ((nameend
= strchr(format
, '}')) != NULL
&&
338 (nameend
- format
- 2) < (sizeof(name
) - 1))
341 * Pull the name from inside the brackets...
344 memcpy(name
, format
+ 1, nameend
- format
- 1);
345 name
[nameend
- format
- 1] = '\0';
349 if ((attr
= ippFindAttribute(job
->attrs
, name
,
350 IPP_TAG_ZERO
)) != NULL
)
353 * Add the attribute value...
357 i
< attr
->num_values
&&
358 bufptr
< (buffer
+ sizeof(buffer
) - 1);
364 switch (attr
->value_tag
)
366 case IPP_TAG_INTEGER
:
368 snprintf(bufptr
, sizeof(buffer
) - (bufptr
- buffer
),
369 "%d", attr
->values
[i
].integer
);
370 bufptr
+= strlen(bufptr
);
373 case IPP_TAG_BOOLEAN
:
374 snprintf(bufptr
, sizeof(buffer
) - (bufptr
- buffer
),
375 "%d", attr
->values
[i
].boolean
);
376 bufptr
+= strlen(bufptr
);
379 case IPP_TAG_TEXTLANG
:
380 case IPP_TAG_NAMELANG
:
383 case IPP_TAG_KEYWORD
:
385 case IPP_TAG_URISCHEME
:
386 case IPP_TAG_CHARSET
:
387 case IPP_TAG_LANGUAGE
:
388 case IPP_TAG_MIMETYPE
:
389 strlcpy(bufptr
, attr
->values
[i
].string
.text
,
390 sizeof(buffer
) - (bufptr
- buffer
));
391 bufptr
+= strlen(bufptr
);
395 strlcpy(bufptr
, "???",
396 sizeof(buffer
) - (bufptr
- buffer
));
397 bufptr
+= strlen(bufptr
);
402 else if (bufptr
< (buffer
+ sizeof(buffer
) - 1))
408 if (bufptr
< (buffer
+ sizeof(buffer
) - 2))
416 else if (bufptr
< (buffer
+ sizeof(buffer
) - 1))
424 * See if we are logging pages via syslog...
427 if (!strcmp(PageLog
, "syslog"))
429 syslog(LOG_INFO
, "%s", buffer
);
433 #endif /* HAVE_VSYSLOG */
436 * Not using syslog; check the log file...
439 if (!check_log_file(&PageFile
, PageLog
))
443 * Print a page log entry of the form:
445 * printer user job-id [DD/MON/YYYY:HH:MM:SS +TTTT] page num-copies \
449 cupsFilePrintf(PageFile
, "%s\n", buffer
);
450 cupsFileFlush(PageFile
);
457 * 'cupsdLogRequest()' - Log an HTTP request in Common Log Format.
460 int /* O - 1 on success, 0 on error */
461 cupsdLogRequest(cupsd_client_t
*con
, /* I - Request to log */
462 http_status_t code
) /* I - Response code */
464 char temp
[2048]; /* Temporary string for URI */
465 static const char * const states
[] = /* HTTP client states... */
486 * See if we are logging accesses via syslog...
489 if (!strcmp(AccessLog
, "syslog"))
492 "REQUEST %s - %s \"%s %s HTTP/%d.%d\" %d " CUPS_LLFMT
" %s %s\n",
493 con
->http
.hostname
, con
->username
[0] != '\0' ? con
->username
: "-",
494 states
[con
->operation
], _httpEncodeURI(temp
, con
->uri
, sizeof(temp
)),
495 con
->http
.version
/ 100, con
->http
.version
% 100,
496 code
, CUPS_LLCAST con
->bytes
,
498 ippOpString(con
->request
->request
.op
.operation_id
) : "-",
500 ippErrorString(con
->response
->request
.status
.status_code
) : "-");
504 #endif /* HAVE_VSYSLOG */
507 * Not using syslog; check the log file...
510 if (!check_log_file(&AccessFile
, AccessLog
))
514 * Write a log of the request in "common log format"...
517 cupsFilePrintf(AccessFile
,
518 "%s - %s %s \"%s %s HTTP/%d.%d\" %d " CUPS_LLFMT
" %s %s\n",
519 con
->http
.hostname
, con
->username
[0] != '\0' ? con
->username
: "-",
520 cupsdGetDateTime(con
->start
), states
[con
->operation
],
521 _httpEncodeURI(temp
, con
->uri
, sizeof(temp
)),
522 con
->http
.version
/ 100, con
->http
.version
% 100,
523 code
, CUPS_LLCAST con
->bytes
,
525 ippOpString(con
->request
->request
.op
.operation_id
) : "-",
527 ippErrorString(con
->response
->request
.status
.status_code
) :
530 cupsFileFlush(AccessFile
);
537 * 'cupsdWriteErrorLog()' - Write a line to the ErrorLog.
540 int /* O - 1 on success, 0 on failure */
541 cupsdWriteErrorLog(int level
, /* I - Log level */
542 const char *message
) /* I - Message string */
544 static const char levels
[] = /* Log levels... */
558 static const int syslevels
[] = /* SYSLOG levels... */
571 #endif /* HAVE_VSYSLOG */
576 * See if we are logging errors via syslog...
579 if (!strcmp(ErrorLog
, "syslog"))
581 syslog(syslevels
[level
], "%s", message
);
584 #endif /* HAVE_VSYSLOG */
587 * Not using syslog; check the log file...
590 if (!check_log_file(&ErrorFile
, ErrorLog
))
594 * Write the log message...
597 cupsFilePrintf(ErrorFile
, "%c %s %s\n", levels
[level
],
598 cupsdGetDateTime(time(NULL
)), message
);
599 cupsFileFlush(ErrorFile
);
606 * 'check_log_file()' - Open/rotate a log file if it needs it.
609 static int /* O - 1 if log file open */
610 check_log_file(cups_file_t
**lf
, /* IO - Log file */
611 const char *logname
) /* I - Log filename */
613 char backname
[1024], /* Backup log filename */
614 filename
[1024], /* Formatted log filename */
615 *ptr
; /* Pointer into filename */
616 const char *logptr
; /* Pointer into log filename */
620 * See if we have a log file to check...
623 if (!lf
|| !logname
|| !logname
[0])
627 * Format the filename as needed...
631 (strncmp(logname
, "/dev/", 5) && cupsFileTell(*lf
) > MaxLogSize
&&
635 * Handle format strings...
638 filename
[sizeof(filename
) - 1] = '\0';
640 if (logname
[0] != '/')
642 strlcpy(filename
, ServerRoot
, sizeof(filename
));
643 strlcat(filename
, "/", sizeof(filename
));
648 for (logptr
= logname
, ptr
= filename
+ strlen(filename
);
649 *logptr
&& ptr
< (filename
+ sizeof(filename
) - 1);
661 * Insert the server name...
664 strlcpy(ptr
, ServerName
, sizeof(filename
) - (ptr
- filename
));
670 * Otherwise just insert the character...
683 * See if the log file is open...
689 * Nope, open the log file...
692 if ((*lf
= cupsFileOpen(filename
, "a")) == NULL
)
695 * If the file is in CUPS_LOGDIR then try to create a missing directory...
698 if (!strncmp(filename
, CUPS_LOGDIR
, strlen(CUPS_LOGDIR
)))
700 cupsdCheckPermissions(CUPS_LOGDIR
, NULL
, 0755, RunUser
, Group
, 1, -1);
702 *lf
= cupsFileOpen(filename
, "a");
707 syslog(LOG_ERR
, "Unable to open log file \"%s\" - %s", filename
,
713 if (strncmp(filename
, "/dev/", 5))
716 * Change ownership and permissions of non-device logs...
719 fchown(cupsFileNumber(*lf
), RunUser
, Group
);
720 fchmod(cupsFileNumber(*lf
), LogFilePerm
);
725 * Do we need to rotate the log?
728 if (strncmp(logname
, "/dev/", 5) && cupsFileTell(*lf
) > MaxLogSize
&&
737 strcpy(backname
, filename
);
738 strlcat(backname
, ".O", sizeof(backname
));
741 rename(filename
, backname
);
743 if ((*lf
= cupsFileOpen(filename
, "a")) == NULL
)
745 syslog(LOG_ERR
, "Unable to open log file \"%s\" - %s", filename
,
752 * Change ownership and permissions of non-device logs...
755 fchown(cupsFileNumber(*lf
), RunUser
, Group
);
756 fchmod(cupsFileNumber(*lf
), LogFilePerm
);
764 * 'format_log_line()' - Format a line for a log file.
766 * This function resizes a global string buffer as needed. Each call returns
767 * a pointer to this buffer, so the contents are only good until the next call
768 * to format_log_line()...
771 static char * /* O - Text or NULL on error */
772 format_log_line(const char *message
, /* I - Printf-style format string */
773 va_list ap
) /* I - Argument list */
775 int len
; /* Length of formatted line */
779 * Allocate the line buffer as needed...
785 log_line
= malloc(log_linesize
);
792 * Format the log message...
795 len
= vsnprintf(log_line
, log_linesize
, message
, ap
);
798 * Resize the buffer as needed...
801 if (len
>= log_linesize
)
803 char *temp
; /* Temporary string pointer */
810 else if (len
> 65536)
813 temp
= realloc(log_line
, len
);
821 len
= vsnprintf(log_line
, log_linesize
, message
, ap
);
829 * End of "$Id: log.c 7699 2008-06-27 20:44:23Z mike $".