]>
git.ipfire.org Git - thirdparty/cups.git/blob - scheduler/log.c
4158a645cb476e0ed17a2024956297dc701df3d7
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 * GetDateTime() - Returns a pointer to a date/time string.
27 * LogMessage() - Log a message to the error log file.
28 * LogPage() - Log a page to the page log file.
29 * LogRequest() - 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 * 'GetDateTime()' - Returns a pointer to a date/time string.
56 char * /* O - Date/time string */
57 GetDateTime(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] =
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 * 'LogMessage()' - Log a message to the error log file.
117 int /* O - 1 on success, 0 on error */
118 LogMessage(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
)
165 * See if we are logging errors via syslog...
168 if (strcmp(ErrorLog
, "syslog") == 0)
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
], GetDateTime(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 * 'LogPage()' - Log a page to the page log file.
281 int /* O - 1 on success, 0 on error */
282 LogPage(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") == 0)
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
, GetDateTime(time(NULL
)), page
,
326 billing
? billing
->values
[0].string
.text
: "-",
327 hostname
->values
[0].string
.text
);
328 cupsFileFlush(PageFile
);
335 * 'LogRequest()' - Log an HTTP request in Common Log Format.
338 int /* O - 1 on success, 0 on error */
339 LogRequest(client_t
*con
, /* I - Request to log */
340 http_status_t code
) /* I - Response code */
342 static const char * const states
[] =
343 { /* HTTP client states... */
363 * See if we are logging accesses via syslog...
366 if (strcmp(AccessLog
, "syslog") == 0)
368 syslog(LOG_INFO
, "REQUEST %s - %s \"%s %s HTTP/%d.%d\" %d %d\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,
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
, "%s - %s %s \"%s %s HTTP/%d.%d\" %d %d\n",
390 con
->http
.hostname
, con
->username
[0] != '\0' ? con
->username
: "-",
391 GetDateTime(con
->start
), states
[con
->operation
], con
->uri
,
392 con
->http
.version
/ 100, con
->http
.version
% 100,
394 cupsFileFlush(AccessFile
);
401 * 'check_log_file()' - Open/rotate a log file if it needs it.
404 static int /* O - 1 if log file open */
405 check_log_file(cups_file_t
**lf
, /* IO - Log file */
406 const char *logname
) /* I - Log filename */
408 char backname
[1024], /* Backup log filename */
409 filename
[1024], /* Formatted log filename */
410 *ptr
; /* Pointer into filename */
414 * See if we have a log file to check or we are handling a signal...
417 if (!lf
|| !logname
|| !logname
[0] || SignalCount
)
421 * Format the filename as needed...
424 if (!*lf
|| (cupsFileTell(*lf
) > MaxLogSize
&& MaxLogSize
> 0))
427 * Handle format strings...
430 filename
[sizeof(filename
) - 1] = '\0';
432 if (logname
[0] != '/')
434 strlcpy(filename
, ServerRoot
, sizeof(filename
));
435 strlcat(filename
, "/", sizeof(filename
));
440 for (ptr
= filename
+ strlen(filename
);
441 *logname
&& ptr
< (filename
+ sizeof(filename
) - 1);
453 * Insert the server name...
456 strlcpy(ptr
, ServerName
, sizeof(filename
) - (ptr
- filename
));
462 * Otherwise just insert the character...
475 * See if the log file is open...
481 * Nope, open the log file...
484 if ((*lf
= cupsFileOpen(filename
, "a")) == NULL
)
487 if (strncmp(filename
, "/dev/", 5))
490 * Change ownership and permissions of non-device logs...
493 fchown(cupsFileNumber(*lf
), RunUser
, Group
);
494 fchmod(cupsFileNumber(*lf
), LogFilePerm
);
499 * Do we need to rotate the log?
502 if (cupsFileTell(*lf
) > MaxLogSize
&& MaxLogSize
> 0)
510 strcpy(backname
, filename
);
511 strlcat(backname
, ".O", sizeof(backname
));
514 rename(filename
, backname
);
516 if ((*lf
= cupsFileOpen(filename
, "a")) == NULL
)
519 if (strncmp(filename
, "/dev/", 5))
522 * Change ownership and permissions of non-device logs...
525 fchown(cupsFileNumber(*lf
), RunUser
, Group
);
526 fchmod(cupsFileNumber(*lf
), LogFilePerm
);