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 */
69 * Make sure we have a valid time...
80 * Get the date and time from the UNIX time value, and then format it
81 * into a string. Note that we *can't* use the strftime() function since
82 * it is localized and will seriously confuse automatic programs if the
83 * month names are in the wrong language!
85 * Also, we use the "timezone" variable that contains the current timezone
86 * offset from GMT in seconds so that we are reporting local time in the
87 * log files. If you want GMT, set the TZ environment variable accordingly
88 * before starting the scheduler.
90 * (*BSD and Darwin store the timezone offset in the tm structure)
95 snprintf(s
, sizeof(s
), "[%02d/%s/%04d:%02d:%02d:%02d %+03ld%02ld]",
96 date
->tm_mday
, months
[date
->tm_mon
], 1900 + date
->tm_year
,
97 date
->tm_hour
, date
->tm_min
, date
->tm_sec
,
99 date
->tm_gmtoff
/ 3600, (date
->tm_gmtoff
/ 60) % 60);
101 timezone
/ 3600, (timezone
/ 60) % 60);
102 #endif /* HAVE_TM_GMTOFF */
111 * 'cupsdLogGSSMessage()' - Log a GSSAPI error...
114 int /* O - 1 on success, 0 on error */
116 int level
, /* I - Log level */
117 int major_status
, /* I - Major GSSAPI status */
118 int minor_status
, /* I - Minor GSSAPI status */
119 const char *message
, /* I - printf-style message string */
120 ...) /* I - Additional args as needed */
122 OM_uint32 err_major_status
, /* Major status code for display */
123 err_minor_status
; /* Minor status code for display */
124 OM_uint32 msg_ctx
; /* Message context */
125 gss_buffer_desc major_status_string
= GSS_C_EMPTY_BUFFER
,
126 /* Major status message */
127 minor_status_string
= GSS_C_EMPTY_BUFFER
;
128 /* Minor status message */
129 int ret
; /* Return value */
133 err_major_status
= gss_display_status(&err_minor_status
,
138 &major_status_string
);
140 if (!GSS_ERROR(err_major_status
))
141 err_major_status
= gss_display_status(&err_minor_status
,
146 &minor_status_string
);
148 ret
= cupsdLogMessage(level
, "%s: %s, %s", message
,
149 (char *)major_status_string
.value
,
150 (char *)minor_status_string
.value
);
151 gss_release_buffer(&err_minor_status
, &major_status_string
);
152 gss_release_buffer(&err_minor_status
, &minor_status_string
);
156 #endif /* HAVE_GSSAPI */
160 * 'cupsdLogMessage()' - Log a message to the error log file.
163 int /* O - 1 on success, 0 on error */
164 cupsdLogMessage(int level
, /* I - Log level */
165 const char *message
, /* I - printf-style message string */
166 ...) /* I - Additional args as needed */
168 int len
; /* Length of message */
169 va_list ap
; /* Argument pointer */
170 static const char levels
[] = /* Log levels... */
184 static const int syslevels
[] = /* SYSLOG levels... */
197 #endif /* HAVE_VSYSLOG */
198 static int linesize
= 0; /* Size of line for output file */
199 static char *line
= NULL
; /* Line for output file */
203 * See if we want to log this message...
208 if (level
<= CUPSD_LOG_WARN
)
210 va_start(ap
, message
);
211 vfprintf(stderr
, message
, ap
);
219 if (level
> LogLevel
|| !ErrorLog
)
224 * See if we are logging errors via syslog...
227 if (!strcmp(ErrorLog
, "syslog"))
229 va_start(ap
, message
);
230 vsyslog(syslevels
[level
], message
, ap
);
235 #endif /* HAVE_VSYSLOG */
238 * Not using syslog; check the log file...
241 if (!check_log_file(&ErrorFile
, ErrorLog
))
245 * Print the log level and date/time...
248 cupsFilePrintf(ErrorFile
, "%c %s ", levels
[level
], cupsdGetDateTime(time(NULL
)));
251 * Allocate the line buffer as needed...
257 line
= malloc(linesize
);
261 cupsFilePrintf(ErrorFile
,
262 "ERROR: Unable to allocate memory for line - %s\n",
264 cupsFileFlush(ErrorFile
);
271 * Format the log message...
274 va_start(ap
, message
);
275 len
= vsnprintf(line
, linesize
, message
, ap
);
279 * Resize the buffer as needed...
284 char *temp
; /* Temporary string pointer */
291 else if (len
> 65536)
294 temp
= realloc(line
, len
);
302 va_start(ap
, message
);
303 len
= vsnprintf(line
, linesize
, message
, ap
);
311 * Then the log message...
314 cupsFilePuts(ErrorFile
, line
);
320 if (len
> 0 && line
[len
- 1] != '\n')
321 cupsFilePutChar(ErrorFile
, '\n');
324 * Flush the line to the file and return...
327 cupsFileFlush(ErrorFile
);
334 * 'cupsdLogPage()' - Log a page to the page log file.
337 int /* O - 1 on success, 0 on error */
338 cupsdLogPage(cupsd_job_t
*job
, /* I - Job being printed */
339 const char *page
) /* I - Page being printed */
341 ipp_attribute_t
*billing
, /* job-billing attribute */
342 *hostname
; /* job-originating-host-name attribute */
345 billing
= ippFindAttribute(job
->attrs
, "job-billing", IPP_TAG_ZERO
);
346 hostname
= ippFindAttribute(job
->attrs
, "job-originating-host-name",
351 * See if we are logging pages via syslog...
354 if (!strcmp(PageLog
, "syslog"))
356 syslog(LOG_INFO
, "PAGE %s %s %d %s %s %s", job
->printer
->name
,
357 job
->username
? job
->username
: "-",
358 job
->id
, page
, billing
? billing
->values
[0].string
.text
: "-",
359 hostname
->values
[0].string
.text
);
363 #endif /* HAVE_VSYSLOG */
366 * Not using syslog; check the log file...
369 if (!check_log_file(&PageFile
, PageLog
))
373 * Print a page log entry of the form:
375 * printer user job-id [DD/MON/YYYY:HH:MM:SS +TTTT] page num-copies \
379 cupsFilePrintf(PageFile
, "%s %s %d %s %s %s %s\n", job
->printer
->name
,
380 job
->username
? job
->username
: "-",
381 job
->id
, cupsdGetDateTime(time(NULL
)), page
,
382 billing
? billing
->values
[0].string
.text
: "-",
383 hostname
->values
[0].string
.text
);
384 cupsFileFlush(PageFile
);
391 * 'cupsdLogRequest()' - Log an HTTP request in Common Log Format.
394 int /* O - 1 on success, 0 on error */
395 cupsdLogRequest(cupsd_client_t
*con
, /* I - Request to log */
396 http_status_t code
) /* I - Response code */
398 char temp
[2048]; /* Temporary string for URI */
399 static const char * const states
[] = /* HTTP client states... */
420 * See if we are logging accesses via syslog...
423 if (!strcmp(AccessLog
, "syslog"))
426 "REQUEST %s - %s \"%s %s HTTP/%d.%d\" %d " CUPS_LLFMT
" %s %s\n",
427 con
->http
.hostname
, con
->username
[0] != '\0' ? con
->username
: "-",
428 states
[con
->operation
], _httpEncodeURI(temp
, con
->uri
, sizeof(temp
)),
429 con
->http
.version
/ 100, con
->http
.version
% 100,
430 code
, CUPS_LLCAST con
->bytes
,
432 ippOpString(con
->request
->request
.op
.operation_id
) : "-",
434 ippErrorString(con
->response
->request
.status
.status_code
) : "-");
438 #endif /* HAVE_VSYSLOG */
441 * Not using syslog; check the log file...
444 if (!check_log_file(&AccessFile
, AccessLog
))
448 * Write a log of the request in "common log format"...
451 cupsFilePrintf(AccessFile
,
452 "%s - %s %s \"%s %s HTTP/%d.%d\" %d " CUPS_LLFMT
" %s %s\n",
453 con
->http
.hostname
, con
->username
[0] != '\0' ? con
->username
: "-",
454 cupsdGetDateTime(con
->start
), states
[con
->operation
],
455 _httpEncodeURI(temp
, con
->uri
, sizeof(temp
)),
456 con
->http
.version
/ 100, con
->http
.version
% 100,
457 code
, CUPS_LLCAST con
->bytes
,
459 ippOpString(con
->request
->request
.op
.operation_id
) : "-",
461 ippErrorString(con
->response
->request
.status
.status_code
) :
464 cupsFileFlush(AccessFile
);
471 * 'check_log_file()' - Open/rotate a log file if it needs it.
474 static int /* O - 1 if log file open */
475 check_log_file(cups_file_t
**lf
, /* IO - Log file */
476 const char *logname
) /* I - Log filename */
478 char backname
[1024], /* Backup log filename */
479 filename
[1024], /* Formatted log filename */
480 *ptr
; /* Pointer into filename */
481 const char *logptr
; /* Pointer into log filename */
485 * See if we have a log file to check...
488 if (!lf
|| !logname
|| !logname
[0])
492 * Format the filename as needed...
496 (strncmp(logname
, "/dev/", 5) && cupsFileTell(*lf
) > MaxLogSize
&&
500 * Handle format strings...
503 filename
[sizeof(filename
) - 1] = '\0';
505 if (logname
[0] != '/')
507 strlcpy(filename
, ServerRoot
, sizeof(filename
));
508 strlcat(filename
, "/", sizeof(filename
));
513 for (logptr
= logname
, ptr
= filename
+ strlen(filename
);
514 *logptr
&& ptr
< (filename
+ sizeof(filename
) - 1);
526 * Insert the server name...
529 strlcpy(ptr
, ServerName
, sizeof(filename
) - (ptr
- filename
));
535 * Otherwise just insert the character...
548 * See if the log file is open...
554 * Nope, open the log file...
557 if ((*lf
= cupsFileOpen(filename
, "a")) == NULL
)
560 * If the file is in CUPS_LOGDIR then try to create a missing directory...
563 if (!strncmp(filename
, CUPS_LOGDIR
, strlen(CUPS_LOGDIR
)))
565 cupsdCheckPermissions(CUPS_LOGDIR
, NULL
, 0755, RunUser
, Group
, 1, -1);
567 *lf
= cupsFileOpen(filename
, "a");
572 syslog(LOG_ERR
, "Unable to open log file \"%s\" - %s", filename
,
578 if (strncmp(filename
, "/dev/", 5))
581 * Change ownership and permissions of non-device logs...
584 fchown(cupsFileNumber(*lf
), RunUser
, Group
);
585 fchmod(cupsFileNumber(*lf
), LogFilePerm
);
590 * Do we need to rotate the log?
593 if (strncmp(logname
, "/dev/", 5) && cupsFileTell(*lf
) > MaxLogSize
&&
602 strcpy(backname
, filename
);
603 strlcat(backname
, ".O", sizeof(backname
));
606 rename(filename
, backname
);
608 if ((*lf
= cupsFileOpen(filename
, "a")) == NULL
)
610 syslog(LOG_ERR
, "Unable to open log file \"%s\" - %s", filename
,
617 * Change ownership and permissions of non-device logs...
620 fchown(cupsFileNumber(*lf
), RunUser
, Group
);
621 fchmod(cupsFileNumber(*lf
), LogFilePerm
);
629 * End of "$Id: log.c 6875 2007-08-27 23:25:06Z mike $".