]>
git.ipfire.org Git - thirdparty/cups.git/blob - scheduler/log.c
2 * "$Id: log.c 4860 2005-12-01 22:07:26Z mike $"
4 * Log file routines for the Common UNIX Printing System (CUPS).
6 * Copyright 1997-2005 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 * cupsdLogMessage() - Log a message to the error log file.
28 * cupsdLogPage() - Log a page to the page log file.
29 * cupsdLogRequest() - Log an HTTP request in Common Log Format.
30 * check_log_file() - Open/rotate a log file if it needs it.
34 * Include necessary headers...
42 #endif /* HAVE_VSYSLOG */
49 static int check_log_file(cups_file_t
**, const char *);
53 * 'cupsdGetDateTime()' - Returns a pointer to a date/time string.
56 char * /* O - Date/time string */
57 cupsdGetDateTime(time_t t
) /* I - Time value */
59 struct tm
*date
; /* Date/time value */
60 static time_t last_time
= -1; /* Last time value */
61 static char s
[1024]; /* Date/time string */
62 static const char * const months
[12] =/* Months */
84 * Get the date and time from the UNIX time value, and then format it
85 * into a string. Note that we *can't* use the strftime() function since
86 * it is localized and will seriously confuse automatic programs if the
87 * month names are in the wrong language!
89 * Also, we use the "timezone" variable that contains the current timezone
90 * offset from GMT in seconds so that we are reporting local time in the
91 * log files. If you want GMT, set the TZ environment variable accordingly
92 * before starting the scheduler.
94 * (*BSD and Darwin store the timezone offset in the tm structure)
99 snprintf(s
, sizeof(s
), "[%02d/%s/%04d:%02d:%02d:%02d %+03ld%02ld]",
100 date
->tm_mday
, months
[date
->tm_mon
], 1900 + date
->tm_year
,
101 date
->tm_hour
, date
->tm_min
, date
->tm_sec
,
102 #ifdef HAVE_TM_GMTOFF
103 date
->tm_gmtoff
/ 3600, (date
->tm_gmtoff
/ 60) % 60);
105 timezone
/ 3600, (timezone
/ 60) % 60);
106 #endif /* HAVE_TM_GMTOFF */
114 * 'cupsdLogMessage()' - Log a message to the error log file.
117 int /* O - 1 on success, 0 on error */
118 cupsdLogMessage(int level
, /* I - Log level */
119 const char *message
, /* I - printf-style message string */
120 ...) /* I - Additional args as needed */
122 int len
; /* Length of message */
123 va_list ap
; /* Argument pointer */
124 static const char levels
[] = /* Log levels... */
138 static const int syslevels
[] = /* SYSLOG levels... */
151 #endif /* HAVE_VSYSLOG */
152 static int linesize
= 0; /* Size of line for output file */
153 static char *line
= NULL
; /* Line for output file */
157 * See if we want to log this message...
160 if (level
> LogLevel
|| !ErrorLog
)
165 * See if we are logging errors via syslog...
168 if (!strcmp(ErrorLog
, "syslog"))
170 va_start(ap
, message
);
171 vsyslog(syslevels
[level
], message
, ap
);
176 #endif /* HAVE_VSYSLOG */
179 * Not using syslog; check the log file...
182 if (!check_log_file(&ErrorFile
, ErrorLog
))
186 * Print the log level and date/time...
189 cupsFilePrintf(ErrorFile
, "%c %s ", levels
[level
], cupsdGetDateTime(time(NULL
)));
192 * Allocate the line buffer as needed...
198 line
= malloc(linesize
);
202 cupsFilePrintf(ErrorFile
,
203 "ERROR: Unable to allocate memory for line - %s\n",
205 cupsFileFlush(ErrorFile
);
212 * Format the log message...
215 va_start(ap
, message
);
216 len
= vsnprintf(line
, linesize
, message
, ap
);
220 * Resize the buffer as needed...
229 else if (len
> 65536)
232 line
= realloc(line
, len
);
238 cupsFilePrintf(ErrorFile
,
239 "ERROR: Unable to allocate memory for line - %s\n",
241 cupsFileFlush(ErrorFile
);
246 va_start(ap
, message
);
247 len
= vsnprintf(line
, linesize
, message
, ap
);
255 * Then the log message...
258 cupsFilePuts(ErrorFile
, line
);
264 if (len
> 0 && line
[len
- 1] != '\n')
265 cupsFilePutChar(ErrorFile
, '\n');
268 * Flush the line to the file and return...
271 cupsFileFlush(ErrorFile
);
278 * 'cupsdLogPage()' - Log a page to the page log file.
281 int /* O - 1 on success, 0 on error */
282 cupsdLogPage(cupsd_job_t
*job
, /* I - Job being printed */
283 const char *page
) /* I - Page being printed */
285 ipp_attribute_t
*billing
, /* job-billing attribute */
286 *hostname
; /* job-originating-host-name attribute */
289 billing
= ippFindAttribute(job
->attrs
, "job-billing", IPP_TAG_ZERO
);
290 hostname
= ippFindAttribute(job
->attrs
, "job-originating-host-name",
295 * See if we are logging pages via syslog...
298 if (!strcmp(PageLog
, "syslog"))
300 syslog(LOG_INFO
, "PAGE %s %s %d %s %s %s", job
->printer
->name
,
301 job
->username
? job
->username
: "-",
302 job
->id
, page
, billing
? billing
->values
[0].string
.text
: "-",
303 hostname
->values
[0].string
.text
);
307 #endif /* HAVE_VSYSLOG */
310 * Not using syslog; check the log file...
313 if (!check_log_file(&PageFile
, PageLog
))
317 * Print a page log entry of the form:
319 * printer job-id user [DD/MON/YYYY:HH:MM:SS +TTTT] page num-copies \
323 cupsFilePrintf(PageFile
, "%s %s %d %s %s %s %s\n", job
->printer
->name
,
324 job
->username
? job
->username
: "-",
325 job
->id
, cupsdGetDateTime(time(NULL
)), page
,
326 billing
? billing
->values
[0].string
.text
: "-",
327 hostname
->values
[0].string
.text
);
328 cupsFileFlush(PageFile
);
335 * 'cupsdLogRequest()' - Log an HTTP request in Common Log Format.
338 int /* O - 1 on success, 0 on error */
339 cupsdLogRequest(cupsd_client_t
*con
, /* I - Request to log */
340 http_status_t code
) /* I - Response code */
342 static const char * const states
[] = /* HTTP client states... */
363 * See if we are logging accesses via syslog...
366 if (!strcmp(AccessLog
, "syslog"))
368 syslog(LOG_INFO
, "REQUEST %s - %s \"%s %s HTTP/%d.%d\" %d " CUPS_LLFMT
"\n",
369 con
->http
.hostname
, con
->username
[0] != '\0' ? con
->username
: "-",
370 states
[con
->operation
], con
->uri
,
371 con
->http
.version
/ 100, con
->http
.version
% 100,
372 code
, CUPS_LLCAST con
->bytes
);
376 #endif /* HAVE_VSYSLOG */
379 * Not using syslog; check the log file...
382 if (!check_log_file(&AccessFile
, AccessLog
))
386 * Write a log of the request in "common log format"...
389 cupsFilePrintf(AccessFile
,
390 "%s - %s %s \"%s %s HTTP/%d.%d\" %d " CUPS_LLFMT
" %s %s\n",
391 con
->http
.hostname
, con
->username
[0] != '\0' ? con
->username
: "-",
392 cupsdGetDateTime(con
->start
), states
[con
->operation
], con
->uri
,
393 con
->http
.version
/ 100, con
->http
.version
% 100,
394 code
, CUPS_LLCAST con
->bytes
,
396 ippOpString(con
->request
->request
.op
.operation_id
) : "-",
398 ippErrorString(con
->response
->request
.status
.status_code
) :
401 cupsFileFlush(AccessFile
);
408 * 'check_log_file()' - Open/rotate a log file if it needs it.
411 static int /* O - 1 if log file open */
412 check_log_file(cups_file_t
**lf
, /* IO - Log file */
413 const char *logname
) /* I - Log filename */
415 char backname
[1024], /* Backup log filename */
416 filename
[1024], /* Formatted log filename */
417 *ptr
; /* Pointer into filename */
421 * See if we have a log file to check...
424 if (!lf
|| !logname
|| !logname
[0])
428 * Format the filename as needed...
431 if (!*lf
|| (cupsFileTell(*lf
) > MaxLogSize
&& MaxLogSize
> 0))
434 * Handle format strings...
437 filename
[sizeof(filename
) - 1] = '\0';
439 if (logname
[0] != '/')
441 strlcpy(filename
, ServerRoot
, sizeof(filename
));
442 strlcat(filename
, "/", sizeof(filename
));
447 for (ptr
= filename
+ strlen(filename
);
448 *logname
&& ptr
< (filename
+ sizeof(filename
) - 1);
460 * Insert the server name...
463 strlcpy(ptr
, ServerName
, sizeof(filename
) - (ptr
- filename
));
469 * Otherwise just insert the character...
482 * See if the log file is open...
488 * Nope, open the log file...
491 if ((*lf
= cupsFileOpen(filename
, "a")) == NULL
)
493 syslog(LOG_ERR
, "Unable to open log file \"%s\" - %s", filename
,
499 if (strncmp(filename
, "/dev/", 5))
502 * Change ownership and permissions of non-device logs...
505 fchown(cupsFileNumber(*lf
), RunUser
, Group
);
506 fchmod(cupsFileNumber(*lf
), LogFilePerm
);
511 * Do we need to rotate the log?
514 if (cupsFileTell(*lf
) > MaxLogSize
&& MaxLogSize
> 0)
522 strcpy(backname
, filename
);
523 strlcat(backname
, ".O", sizeof(backname
));
526 rename(filename
, backname
);
528 if ((*lf
= cupsFileOpen(filename
, "a")) == NULL
)
530 syslog(LOG_ERR
, "Unable to open log file \"%s\" - %s", filename
,
536 if (strncmp(filename
, "/dev/", 5))
539 * Change ownership and permissions of non-device logs...
542 fchown(cupsFileNumber(*lf
), RunUser
, Group
);
543 fchmod(cupsFileNumber(*lf
), LogFilePerm
);
552 * End of "$Id: log.c 4860 2005-12-01 22:07:26Z mike $".