2 * "$Id: log.c 6875 2007-08-27 23:25:06Z 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 * cupsdLogMessage() - Log a message to the error log file.
20 * cupsdLogPage() - Log a page to the page log file.
21 * cupsdLogRequest() - Log an HTTP request in Common Log Format.
22 * check_log_file() - Open/rotate a log file if it needs it.
26 * Include necessary headers...
38 static int check_log_file(cups_file_t
**, const char *);
42 * 'cupsdGetDateTime()' - Returns a pointer to a date/time string.
45 char * /* O - Date/time string */
46 cupsdGetDateTime(time_t t
) /* I - Time value */
48 struct tm
*date
; /* Date/time value */
49 static time_t last_time
= -1; /* Last time value */
50 static char s
[1024]; /* Date/time string */
51 static const char * const months
[12] =/* Months */
73 * Get the date and time from the UNIX time value, and then format it
74 * into a string. Note that we *can't* use the strftime() function since
75 * it is localized and will seriously confuse automatic programs if the
76 * month names are in the wrong language!
78 * Also, we use the "timezone" variable that contains the current timezone
79 * offset from GMT in seconds so that we are reporting local time in the
80 * log files. If you want GMT, set the TZ environment variable accordingly
81 * before starting the scheduler.
83 * (*BSD and Darwin store the timezone offset in the tm structure)
88 snprintf(s
, sizeof(s
), "[%02d/%s/%04d:%02d:%02d:%02d %+03ld%02ld]",
89 date
->tm_mday
, months
[date
->tm_mon
], 1900 + date
->tm_year
,
90 date
->tm_hour
, date
->tm_min
, date
->tm_sec
,
92 date
->tm_gmtoff
/ 3600, (date
->tm_gmtoff
/ 60) % 60);
94 timezone
/ 3600, (timezone
/ 60) % 60);
95 #endif /* HAVE_TM_GMTOFF */
104 * 'cupsdLogGSSMessage()' - Log a GSSAPI error...
107 int /* O - 1 on success, 0 on error */
109 int level
, /* I - Log level */
110 int major_status
, /* I - Major GSSAPI status */
111 int minor_status
, /* I - Minor GSSAPI status */
112 const char *message
, /* I - printf-style message string */
113 ...) /* I - Additional args as needed */
115 OM_uint32 err_major_status
, /* Major status code for display */
116 err_minor_status
; /* Minor status code for display */
117 OM_uint32 msg_ctx
; /* Message context */
118 gss_buffer_desc major_status_string
= GSS_C_EMPTY_BUFFER
,
119 /* Major status message */
120 minor_status_string
= GSS_C_EMPTY_BUFFER
;
121 /* Minor status message */
122 int ret
; /* Return value */
126 err_major_status
= gss_display_status(&err_minor_status
,
131 &major_status_string
);
133 if (!GSS_ERROR(err_major_status
))
134 err_major_status
= gss_display_status(&err_minor_status
,
139 &minor_status_string
);
141 ret
= cupsdLogMessage(level
, "%s: %s, %s", message
,
142 (char *)major_status_string
.value
,
143 (char *)minor_status_string
.value
);
144 gss_release_buffer(&err_minor_status
, &major_status_string
);
145 gss_release_buffer(&err_minor_status
, &minor_status_string
);
149 #endif /* HAVE_GSSAPI */
153 * 'cupsdLogMessage()' - Log a message to the error log file.
156 int /* O - 1 on success, 0 on error */
157 cupsdLogMessage(int level
, /* I - Log level */
158 const char *message
, /* I - printf-style message string */
159 ...) /* I - Additional args as needed */
161 int len
; /* Length of message */
162 va_list ap
; /* Argument pointer */
163 static const char levels
[] = /* Log levels... */
177 static const int syslevels
[] = /* SYSLOG levels... */
190 #endif /* HAVE_VSYSLOG */
191 static int linesize
= 0; /* Size of line for output file */
192 static char *line
= NULL
; /* Line for output file */
196 * See if we want to log this message...
201 if (level
<= CUPSD_LOG_WARN
)
203 va_start(ap
, message
);
204 vfprintf(stderr
, message
, ap
);
212 if (level
> LogLevel
|| !ErrorLog
)
217 * See if we are logging errors via syslog...
220 if (!strcmp(ErrorLog
, "syslog"))
222 va_start(ap
, message
);
223 vsyslog(syslevels
[level
], message
, ap
);
228 #endif /* HAVE_VSYSLOG */
231 * Not using syslog; check the log file...
234 if (!check_log_file(&ErrorFile
, ErrorLog
))
238 * Print the log level and date/time...
241 cupsFilePrintf(ErrorFile
, "%c %s ", levels
[level
], cupsdGetDateTime(time(NULL
)));
244 * Allocate the line buffer as needed...
250 line
= malloc(linesize
);
254 cupsFilePrintf(ErrorFile
,
255 "ERROR: Unable to allocate memory for line - %s\n",
257 cupsFileFlush(ErrorFile
);
264 * Format the log message...
267 va_start(ap
, message
);
268 len
= vsnprintf(line
, linesize
, message
, ap
);
272 * Resize the buffer as needed...
277 char *temp
; /* Temporary string pointer */
284 else if (len
> 65536)
287 temp
= realloc(line
, len
);
295 va_start(ap
, message
);
296 len
= vsnprintf(line
, linesize
, message
, ap
);
304 * Then the log message...
307 cupsFilePuts(ErrorFile
, line
);
313 if (len
> 0 && line
[len
- 1] != '\n')
314 cupsFilePutChar(ErrorFile
, '\n');
317 * Flush the line to the file and return...
320 cupsFileFlush(ErrorFile
);
327 * 'cupsdLogPage()' - Log a page to the page log file.
330 int /* O - 1 on success, 0 on error */
331 cupsdLogPage(cupsd_job_t
*job
, /* I - Job being printed */
332 const char *page
) /* I - Page being printed */
334 ipp_attribute_t
*billing
, /* job-billing attribute */
335 *hostname
; /* job-originating-host-name attribute */
338 billing
= ippFindAttribute(job
->attrs
, "job-billing", IPP_TAG_ZERO
);
339 hostname
= ippFindAttribute(job
->attrs
, "job-originating-host-name",
344 * See if we are logging pages via syslog...
347 if (!strcmp(PageLog
, "syslog"))
349 syslog(LOG_INFO
, "PAGE %s %s %d %s %s %s", job
->printer
->name
,
350 job
->username
? job
->username
: "-",
351 job
->id
, page
, billing
? billing
->values
[0].string
.text
: "-",
352 hostname
->values
[0].string
.text
);
356 #endif /* HAVE_VSYSLOG */
359 * Not using syslog; check the log file...
362 if (!check_log_file(&PageFile
, PageLog
))
366 * Print a page log entry of the form:
368 * printer user job-id [DD/MON/YYYY:HH:MM:SS +TTTT] page num-copies \
372 cupsFilePrintf(PageFile
, "%s %s %d %s %s %s %s\n", job
->printer
->name
,
373 job
->username
? job
->username
: "-",
374 job
->id
, cupsdGetDateTime(time(NULL
)), page
,
375 billing
? billing
->values
[0].string
.text
: "-",
376 hostname
->values
[0].string
.text
);
377 cupsFileFlush(PageFile
);
384 * 'cupsdLogRequest()' - Log an HTTP request in Common Log Format.
387 int /* O - 1 on success, 0 on error */
388 cupsdLogRequest(cupsd_client_t
*con
, /* I - Request to log */
389 http_status_t code
) /* I - Response code */
391 static const char * const states
[] = /* HTTP client states... */
412 * See if we are logging accesses via syslog...
415 if (!strcmp(AccessLog
, "syslog"))
418 "REQUEST %s - %s \"%s %s HTTP/%d.%d\" %d " CUPS_LLFMT
" %s %s\n",
419 con
->http
.hostname
, con
->username
[0] != '\0' ? con
->username
: "-",
420 states
[con
->operation
], con
->uri
,
421 con
->http
.version
/ 100, con
->http
.version
% 100,
422 code
, CUPS_LLCAST con
->bytes
,
424 ippOpString(con
->request
->request
.op
.operation_id
) : "-",
426 ippErrorString(con
->response
->request
.status
.status_code
) : "-");
430 #endif /* HAVE_VSYSLOG */
433 * Not using syslog; check the log file...
436 if (!check_log_file(&AccessFile
, AccessLog
))
440 * Write a log of the request in "common log format"...
443 cupsFilePrintf(AccessFile
,
444 "%s - %s %s \"%s %s HTTP/%d.%d\" %d " CUPS_LLFMT
" %s %s\n",
445 con
->http
.hostname
, con
->username
[0] != '\0' ? con
->username
: "-",
446 cupsdGetDateTime(con
->start
), states
[con
->operation
], con
->uri
,
447 con
->http
.version
/ 100, con
->http
.version
% 100,
448 code
, CUPS_LLCAST con
->bytes
,
450 ippOpString(con
->request
->request
.op
.operation_id
) : "-",
452 ippErrorString(con
->response
->request
.status
.status_code
) :
455 cupsFileFlush(AccessFile
);
462 * 'check_log_file()' - Open/rotate a log file if it needs it.
465 static int /* O - 1 if log file open */
466 check_log_file(cups_file_t
**lf
, /* IO - Log file */
467 const char *logname
) /* I - Log filename */
469 char backname
[1024], /* Backup log filename */
470 filename
[1024], /* Formatted log filename */
471 *ptr
; /* Pointer into filename */
472 const char *logptr
; /* Pointer into log filename */
476 * See if we have a log file to check...
479 if (!lf
|| !logname
|| !logname
[0])
483 * Format the filename as needed...
487 (strncmp(logname
, "/dev/", 5) && cupsFileTell(*lf
) > MaxLogSize
&&
491 * Handle format strings...
494 filename
[sizeof(filename
) - 1] = '\0';
496 if (logname
[0] != '/')
498 strlcpy(filename
, ServerRoot
, sizeof(filename
));
499 strlcat(filename
, "/", sizeof(filename
));
504 for (logptr
= logname
, ptr
= filename
+ strlen(filename
);
505 *logptr
&& ptr
< (filename
+ sizeof(filename
) - 1);
517 * Insert the server name...
520 strlcpy(ptr
, ServerName
, sizeof(filename
) - (ptr
- filename
));
526 * Otherwise just insert the character...
539 * See if the log file is open...
545 * Nope, open the log file...
548 if ((*lf
= cupsFileOpen(filename
, "a")) == NULL
)
551 * If the file is in CUPS_LOGDIR then try to create a missing directory...
554 if (!strncmp(filename
, CUPS_LOGDIR
, strlen(CUPS_LOGDIR
)))
556 cupsdCheckPermissions(CUPS_LOGDIR
, NULL
, 0755, RunUser
, Group
, 1, -1);
558 *lf
= cupsFileOpen(filename
, "a");
563 syslog(LOG_ERR
, "Unable to open log file \"%s\" - %s", filename
,
569 if (strncmp(filename
, "/dev/", 5))
572 * Change ownership and permissions of non-device logs...
575 fchown(cupsFileNumber(*lf
), RunUser
, Group
);
576 fchmod(cupsFileNumber(*lf
), LogFilePerm
);
581 * Do we need to rotate the log?
584 if (strncmp(logname
, "/dev/", 5) && cupsFileTell(*lf
) > MaxLogSize
&&
593 strcpy(backname
, filename
);
594 strlcat(backname
, ".O", sizeof(backname
));
597 rename(filename
, backname
);
599 if ((*lf
= cupsFileOpen(filename
, "a")) == NULL
)
601 syslog(LOG_ERR
, "Unable to open log file \"%s\" - %s", filename
,
608 * Change ownership and permissions of non-device logs...
611 fchown(cupsFileNumber(*lf
), RunUser
, Group
);
612 fchmod(cupsFileNumber(*lf
), LogFilePerm
);
620 * End of "$Id: log.c 6875 2007-08-27 23:25:06Z mike $".