2 * "$Id: log.c 6328 2007-03-12 14:45:42Z mike $"
4 * Log file routines for the Common UNIX Printing System (CUPS).
6 * Copyright 1997-2006 by Easy Software Products, all rights reserved.
8 * These coded instructions, statements, and computer programs are the
9 * property of Easy Software Products and are protected by Federal
10 * copyright law. Distribution and use rights are outlined in the file
11 * "LICENSE.txt" which should have been included with this file. If this
12 * file is missing or damaged please contact Easy Software Products
15 * Attn: CUPS Licensing Information
16 * Easy Software Products
17 * 44141 Airport View Drive, Suite 204
18 * Hollywood, Maryland 20636 USA
20 * Voice: (301) 373-9600
21 * EMail: cups-info@cups.org
22 * WWW: http://www.cups.org
26 * cupsdGetDateTime() - Returns a pointer to a date/time string.
27 * cupsdLogGSSMessage() - Log a GSSAPI error...
28 * cupsdLogMessage() - Log a message to the error log file.
29 * cupsdLogPage() - Log a page to the page log file.
30 * cupsdLogRequest() - Log an HTTP request in Common Log Format.
31 * check_log_file() - Open/rotate a log file if it needs it.
35 * Include necessary headers...
47 static int check_log_file(cups_file_t
**, const char *);
51 * 'cupsdGetDateTime()' - Returns a pointer to a date/time string.
54 char * /* O - Date/time string */
55 cupsdGetDateTime(time_t t
) /* I - Time value */
57 struct tm
*date
; /* Date/time value */
58 static time_t last_time
= -1; /* Last time value */
59 static char s
[1024]; /* Date/time string */
60 static const char * const months
[12] =/* Months */
82 * Get the date and time from the UNIX time value, and then format it
83 * into a string. Note that we *can't* use the strftime() function since
84 * it is localized and will seriously confuse automatic programs if the
85 * month names are in the wrong language!
87 * Also, we use the "timezone" variable that contains the current timezone
88 * offset from GMT in seconds so that we are reporting local time in the
89 * log files. If you want GMT, set the TZ environment variable accordingly
90 * before starting the scheduler.
92 * (*BSD and Darwin store the timezone offset in the tm structure)
97 snprintf(s
, sizeof(s
), "[%02d/%s/%04d:%02d:%02d:%02d %+03ld%02ld]",
98 date
->tm_mday
, months
[date
->tm_mon
], 1900 + date
->tm_year
,
99 date
->tm_hour
, date
->tm_min
, date
->tm_sec
,
100 #ifdef HAVE_TM_GMTOFF
101 date
->tm_gmtoff
/ 3600, (date
->tm_gmtoff
/ 60) % 60);
103 timezone
/ 3600, (timezone
/ 60) % 60);
104 #endif /* HAVE_TM_GMTOFF */
113 * 'cupsdLogGSSMessage()' - Log a GSSAPI error...
116 int /* O - 1 on success, 0 on error */
118 int level
, /* I - Log level */
119 int major_status
, /* I - Major GSSAPI status */
120 int minor_status
, /* I - Minor GSSAPI status */
121 const char *message
, /* I - printf-style message string */
122 ...) /* I - Additional args as needed */
124 OM_uint32 err_major_status
, /* Major status code for display */
125 err_minor_status
; /* Minor status code for display */
126 OM_uint32 msg_ctx
; /* Message context */
127 gss_buffer_desc major_status_string
= GSS_C_EMPTY_BUFFER
,
128 /* Major status message */
129 minor_status_string
= GSS_C_EMPTY_BUFFER
;
130 /* Minor status message */
131 int ret
; /* Return value */
135 err_major_status
= gss_display_status(&err_minor_status
,
140 &major_status_string
);
142 if (!GSS_ERROR(err_major_status
))
143 err_major_status
= gss_display_status(&err_minor_status
,
148 &minor_status_string
);
150 ret
= cupsdLogMessage(level
, "%s: %s, %s", message
,
151 (char *)major_status_string
.value
,
152 (char *)minor_status_string
.value
);
153 gss_release_buffer(&err_minor_status
, &major_status_string
);
154 gss_release_buffer(&err_minor_status
, &minor_status_string
);
158 #endif /* HAVE_GSSAPI */
162 * 'cupsdLogMessage()' - Log a message to the error log file.
165 int /* O - 1 on success, 0 on error */
166 cupsdLogMessage(int level
, /* I - Log level */
167 const char *message
, /* I - printf-style message string */
168 ...) /* I - Additional args as needed */
170 int len
; /* Length of message */
171 va_list ap
; /* Argument pointer */
172 static const char levels
[] = /* Log levels... */
186 static const int syslevels
[] = /* SYSLOG levels... */
199 #endif /* HAVE_VSYSLOG */
200 static int linesize
= 0; /* Size of line for output file */
201 static char *line
= NULL
; /* Line for output file */
205 * See if we want to log this message...
208 if (level
> LogLevel
|| !ErrorLog
)
213 * See if we are logging errors via syslog...
216 if (!strcmp(ErrorLog
, "syslog"))
218 va_start(ap
, message
);
219 vsyslog(syslevels
[level
], message
, ap
);
224 #endif /* HAVE_VSYSLOG */
227 * Not using syslog; check the log file...
230 if (!check_log_file(&ErrorFile
, ErrorLog
))
234 * Print the log level and date/time...
237 cupsFilePrintf(ErrorFile
, "%c %s ", levels
[level
], cupsdGetDateTime(time(NULL
)));
240 * Allocate the line buffer as needed...
246 line
= malloc(linesize
);
250 cupsFilePrintf(ErrorFile
,
251 "ERROR: Unable to allocate memory for line - %s\n",
253 cupsFileFlush(ErrorFile
);
260 * Format the log message...
263 va_start(ap
, message
);
264 len
= vsnprintf(line
, linesize
, message
, ap
);
268 * Resize the buffer as needed...
277 else if (len
> 65536)
280 line
= realloc(line
, len
);
286 cupsFilePrintf(ErrorFile
,
287 "ERROR: Unable to allocate memory for line - %s\n",
289 cupsFileFlush(ErrorFile
);
294 va_start(ap
, message
);
295 len
= vsnprintf(line
, linesize
, message
, ap
);
303 * Then the log message...
306 cupsFilePuts(ErrorFile
, line
);
312 if (len
> 0 && line
[len
- 1] != '\n')
313 cupsFilePutChar(ErrorFile
, '\n');
316 * Flush the line to the file and return...
319 cupsFileFlush(ErrorFile
);
326 * 'cupsdLogPage()' - Log a page to the page log file.
329 int /* O - 1 on success, 0 on error */
330 cupsdLogPage(cupsd_job_t
*job
, /* I - Job being printed */
331 const char *page
) /* I - Page being printed */
333 ipp_attribute_t
*billing
, /* job-billing attribute */
334 *hostname
; /* job-originating-host-name attribute */
337 billing
= ippFindAttribute(job
->attrs
, "job-billing", IPP_TAG_ZERO
);
338 hostname
= ippFindAttribute(job
->attrs
, "job-originating-host-name",
343 * See if we are logging pages via syslog...
346 if (!strcmp(PageLog
, "syslog"))
348 syslog(LOG_INFO
, "PAGE %s %s %d %s %s %s", job
->printer
->name
,
349 job
->username
? job
->username
: "-",
350 job
->id
, page
, billing
? billing
->values
[0].string
.text
: "-",
351 hostname
->values
[0].string
.text
);
355 #endif /* HAVE_VSYSLOG */
358 * Not using syslog; check the log file...
361 if (!check_log_file(&PageFile
, PageLog
))
365 * Print a page log entry of the form:
367 * printer job-id user [DD/MON/YYYY:HH:MM:SS +TTTT] page num-copies \
371 cupsFilePrintf(PageFile
, "%s %s %d %s %s %s %s\n", job
->printer
->name
,
372 job
->username
? job
->username
: "-",
373 job
->id
, cupsdGetDateTime(time(NULL
)), page
,
374 billing
? billing
->values
[0].string
.text
: "-",
375 hostname
->values
[0].string
.text
);
376 cupsFileFlush(PageFile
);
383 * 'cupsdLogRequest()' - Log an HTTP request in Common Log Format.
386 int /* O - 1 on success, 0 on error */
387 cupsdLogRequest(cupsd_client_t
*con
, /* I - Request to log */
388 http_status_t code
) /* I - Response code */
390 static const char * const states
[] = /* HTTP client states... */
411 * See if we are logging accesses via syslog...
414 if (!strcmp(AccessLog
, "syslog"))
417 "REQUEST %s - %s \"%s %s HTTP/%d.%d\" %d " CUPS_LLFMT
" %s %s\n",
418 con
->http
.hostname
, con
->username
[0] != '\0' ? con
->username
: "-",
419 states
[con
->operation
], con
->uri
,
420 con
->http
.version
/ 100, con
->http
.version
% 100,
421 code
, CUPS_LLCAST con
->bytes
,
423 ippOpString(con
->request
->request
.op
.operation_id
) : "-",
425 ippErrorString(con
->response
->request
.status
.status_code
) : "-");
429 #endif /* HAVE_VSYSLOG */
432 * Not using syslog; check the log file...
435 if (!check_log_file(&AccessFile
, AccessLog
))
439 * Write a log of the request in "common log format"...
442 cupsFilePrintf(AccessFile
,
443 "%s - %s %s \"%s %s HTTP/%d.%d\" %d " CUPS_LLFMT
" %s %s\n",
444 con
->http
.hostname
, con
->username
[0] != '\0' ? con
->username
: "-",
445 cupsdGetDateTime(con
->start
), states
[con
->operation
], con
->uri
,
446 con
->http
.version
/ 100, con
->http
.version
% 100,
447 code
, CUPS_LLCAST con
->bytes
,
449 ippOpString(con
->request
->request
.op
.operation_id
) : "-",
451 ippErrorString(con
->response
->request
.status
.status_code
) :
454 cupsFileFlush(AccessFile
);
461 * 'check_log_file()' - Open/rotate a log file if it needs it.
464 static int /* O - 1 if log file open */
465 check_log_file(cups_file_t
**lf
, /* IO - Log file */
466 const char *logname
) /* I - Log filename */
468 char backname
[1024], /* Backup log filename */
469 filename
[1024], /* Formatted log filename */
470 *ptr
; /* Pointer into filename */
471 const char *logptr
; /* Pointer into log filename */
475 * See if we have a log file to check...
478 if (!lf
|| !logname
|| !logname
[0])
482 * Format the filename as needed...
486 (strncmp(logname
, "/dev/", 5) && cupsFileTell(*lf
) > MaxLogSize
&&
490 * Handle format strings...
493 filename
[sizeof(filename
) - 1] = '\0';
495 if (logname
[0] != '/')
497 strlcpy(filename
, ServerRoot
, sizeof(filename
));
498 strlcat(filename
, "/", sizeof(filename
));
503 for (logptr
= logname
, ptr
= filename
+ strlen(filename
);
504 *logptr
&& ptr
< (filename
+ sizeof(filename
) - 1);
516 * Insert the server name...
519 strlcpy(ptr
, ServerName
, sizeof(filename
) - (ptr
- filename
));
525 * Otherwise just insert the character...
538 * See if the log file is open...
544 * Nope, open the log file...
547 if ((*lf
= cupsFileOpen(filename
, "a")) == NULL
)
549 syslog(LOG_ERR
, "Unable to open log file \"%s\" - %s", filename
,
555 if (strncmp(filename
, "/dev/", 5))
558 * Change ownership and permissions of non-device logs...
561 fchown(cupsFileNumber(*lf
), RunUser
, Group
);
562 fchmod(cupsFileNumber(*lf
), LogFilePerm
);
567 * Do we need to rotate the log?
570 if (strncmp(logname
, "/dev/", 5) && cupsFileTell(*lf
) > MaxLogSize
&&
579 strcpy(backname
, filename
);
580 strlcat(backname
, ".O", sizeof(backname
));
583 rename(filename
, backname
);
585 if ((*lf
= cupsFileOpen(filename
, "a")) == NULL
)
587 syslog(LOG_ERR
, "Unable to open log file \"%s\" - %s", filename
,
594 * Change ownership and permissions of non-device logs...
597 fchown(cupsFileNumber(*lf
), RunUser
, Group
);
598 fchmod(cupsFileNumber(*lf
), LogFilePerm
);
606 * End of "$Id: log.c 6328 2007-03-12 14:45:42Z mike $".