2002-05-26 13:56:56 -04:00
|
|
|
;;; logging.scm
|
|
|
|
;;; logging functionality for web server
|
2002-08-27 05:03:22 -04:00
|
|
|
|
|
|
|
;;; This file is part of the Scheme Untergrund Networking package.
|
|
|
|
|
|
|
|
;;; Copyright (c) 2002 by Martin Gasbichler.
|
|
|
|
;;; Copyright (c) 2002 by Andreas Bernauer.
|
|
|
|
;;; For copyright information, see the file COPYING which comes with
|
|
|
|
;;; the distribution.
|
2002-05-26 13:56:56 -04:00
|
|
|
|
2002-09-04 10:01:34 -04:00
|
|
|
(define do-nothing-proc (lambda a #f))
|
2003-01-07 07:16:33 -05:00
|
|
|
|
|
|
|
(define-record-type logging :logging
|
|
|
|
(really-make-logging log-port log-proc
|
|
|
|
syslog? syslog-proc
|
|
|
|
dns-lookup?)
|
|
|
|
logging?
|
|
|
|
;; port to perform CLF-logging
|
|
|
|
(log-port logging-log-port set-logging-log-port!)
|
|
|
|
;; proc to run for CLF-logging (req status-code)
|
|
|
|
(log-proc logging-log-proc set-logging-log-proc!)
|
|
|
|
;; do syslogging?
|
|
|
|
(syslog? logging-syslog? set-logging-syslog?!)
|
|
|
|
;; proc to run for syslog (level fmt . args)
|
|
|
|
(syslog-proc logging-syslog-proc set-logging-syslog-proc!)
|
|
|
|
;; perform dns lookups?
|
|
|
|
(dns-lookup? logging-dns-lookup? set-logging-dns-lookup?!))
|
|
|
|
|
|
|
|
(define (make-logging)
|
|
|
|
(really-make-logging #f
|
|
|
|
do-nothing-proc
|
|
|
|
#f
|
|
|
|
do-nothing-proc
|
|
|
|
#f))
|
2002-09-04 10:01:34 -04:00
|
|
|
|
2002-09-05 06:55:30 -04:00
|
|
|
(define logging (make-preserved-thread-fluid #f))
|
2002-09-04 10:01:34 -04:00
|
|
|
|
|
|
|
(define (make-fluid-selector selector)
|
2002-09-05 06:55:30 -04:00
|
|
|
(lambda () (selector (thread-fluid logging))))
|
2002-09-04 10:01:34 -04:00
|
|
|
|
|
|
|
(define (make-fluid-setter setter)
|
|
|
|
(lambda (value)
|
2002-09-05 06:55:30 -04:00
|
|
|
(setter (thread-fluid logging) value)))
|
2002-09-04 10:01:34 -04:00
|
|
|
|
2003-01-07 07:16:33 -05:00
|
|
|
(define logging-http-log-proc (make-fluid-selector logging-log-proc))
|
|
|
|
(define logging-http-syslog-proc (make-fluid-selector logging-syslog-proc))
|
|
|
|
(define logging-http-syslog? (make-fluid-selector logging-syslog?))
|
|
|
|
(define logging-http-log-port (make-fluid-selector logging-log-port))
|
|
|
|
(define logging-dns-lookup? (make-fluid-selector logging-dns-lookup?))
|
|
|
|
|
|
|
|
(define set-logging-http-log-proc (make-fluid-setter set-logging-log-proc!))
|
|
|
|
(define set-logging-http-syslog-proc (make-fluid-setter set-logging-syslog-proc!))
|
|
|
|
(define set-logging-http-syslog? (make-fluid-setter set-logging-syslog?!))
|
|
|
|
(define set-logging-http-log-port (make-fluid-setter set-logging-log-port!))
|
|
|
|
(define set-logging-dns-lookup? (make-fluid-setter set-logging-dns-lookup?!))
|
2002-09-04 10:01:34 -04:00
|
|
|
|
|
|
|
(define http-syslog
|
|
|
|
(lambda a
|
|
|
|
(apply (logging-http-syslog-proc) a)))
|
|
|
|
|
|
|
|
(define http-log
|
|
|
|
(lambda a
|
|
|
|
(apply (logging-http-log-proc) a)))
|
|
|
|
|
|
|
|
(define (http-syslog?)
|
|
|
|
(logging-http-syslog?))
|
2002-05-26 13:56:56 -04:00
|
|
|
|
|
|
|
(define (init-http-log! options)
|
2002-09-04 10:01:34 -04:00
|
|
|
;; syslog has to be initialized before CLF-logging
|
|
|
|
;; because the latter may generate syslog-messages
|
2002-05-26 13:56:56 -04:00
|
|
|
(init-http-syslog! (httpd-options-syslog? options))
|
2003-01-30 11:09:20 -05:00
|
|
|
(init-http-port-log! (httpd-options-log-file options))
|
2002-08-22 12:40:10 -04:00
|
|
|
(if (httpd-options-resolve-ips? options)
|
2002-09-04 10:01:34 -04:00
|
|
|
(set-logging-dns-lookup? #t)
|
|
|
|
(set-logging-dns-lookup? #f)))
|
|
|
|
|
|
|
|
(define (init-http-syslog! syslog?)
|
|
|
|
(if syslog?
|
|
|
|
(let ((http-syslog-lock (make-lock)))
|
|
|
|
(set-logging-http-syslog? #t)
|
|
|
|
(set-logging-http-syslog-proc
|
|
|
|
(lambda (level fmt . args)
|
2002-12-29 14:10:10 -05:00
|
|
|
(with-lock http-syslog-lock
|
|
|
|
(lambda ()
|
|
|
|
(syslog level
|
|
|
|
(apply format #f fmt args)))))))
|
2002-09-04 10:01:34 -04:00
|
|
|
(begin
|
|
|
|
(set-logging-http-syslog? #f)
|
|
|
|
(set-logging-http-syslog-proc do-nothing-proc))))
|
2002-05-26 13:56:56 -04:00
|
|
|
|
2003-01-30 11:09:20 -05:00
|
|
|
(define (init-http-port-log! log-file)
|
2002-05-26 13:56:56 -04:00
|
|
|
(let ((logport
|
|
|
|
(cond
|
2003-01-30 11:09:20 -05:00
|
|
|
((string? log-file) ; try to open log-file for appending (output)
|
|
|
|
(open-log-file log-file))
|
|
|
|
((output-port? log-file) ; we were given an output port, so let's use it
|
|
|
|
log-file)
|
|
|
|
((eq? log-file #f) ; no logging demanded
|
2002-05-26 13:56:56 -04:00
|
|
|
#f)
|
2003-01-30 11:09:20 -05:00
|
|
|
; unexpected value of log-file;
|
2002-05-26 13:56:56 -04:00
|
|
|
(else
|
|
|
|
(http-syslog
|
|
|
|
(syslog-level warning)
|
2003-01-30 11:09:20 -05:00
|
|
|
"[httpd] Warning: Log-File was not specified correctly (given: ~S).~% No CLF logging."
|
|
|
|
log-file)
|
2002-05-26 13:56:56 -04:00
|
|
|
(make-null-output-port)))))
|
|
|
|
|
2003-01-30 11:09:20 -05:00
|
|
|
(if log-file ; if logging was specified, set up the logger
|
2002-05-26 13:56:56 -04:00
|
|
|
(let ((http-log-lock (make-lock)))
|
2002-09-04 10:01:34 -04:00
|
|
|
(set-logging-http-log-port logport)
|
2003-01-30 11:09:20 -05:00
|
|
|
(if (string? log-file)
|
|
|
|
(spawn (make-log-file-rotator log-file http-log-lock)))
|
2002-09-04 10:01:34 -04:00
|
|
|
(set-logging-http-log-proc (make-http-log-proc http-log-lock))))))
|
2002-05-26 13:56:56 -04:00
|
|
|
|
|
|
|
(define (make-http-log-proc http-log-lock)
|
2002-08-26 05:59:14 -04:00
|
|
|
(lambda (req status-code)
|
2002-05-26 13:56:56 -04:00
|
|
|
(if req
|
2002-12-29 14:10:10 -05:00
|
|
|
(with-lock http-log-lock
|
|
|
|
(lambda ()
|
|
|
|
(display (make-CLF
|
|
|
|
(receive (host-address _)
|
|
|
|
(socket-address->internet-address
|
|
|
|
(socket-remote-address (request-socket req)))
|
|
|
|
(format-internet-host-address host-address))
|
|
|
|
(request-method req) ; request method
|
2005-04-06 08:32:17 -04:00
|
|
|
(http-url-path->path-string
|
|
|
|
(http-url-path (request-url req))) ; requested file (escaped as it was in original request)
|
2002-12-29 14:10:10 -05:00
|
|
|
(version->string (request-version req)) ; protocol version
|
2003-01-09 10:05:30 -05:00
|
|
|
(status-code-number status-code)
|
2002-12-29 14:10:10 -05:00
|
|
|
23 ; filesize (unknown)
|
|
|
|
(get-header (request-headers req) 'referer)
|
|
|
|
(get-header (request-headers req) 'user-agent))
|
|
|
|
(logging-http-log-port))
|
|
|
|
(force-output (logging-http-log-port)))))))
|
2002-05-26 13:56:56 -04:00
|
|
|
|
2003-01-20 11:26:26 -05:00
|
|
|
(define (get-header headers tag)
|
|
|
|
(cond
|
|
|
|
((assq tag headers) => cdr)
|
2003-02-25 12:36:36 -05:00
|
|
|
(else #f)))
|
2002-05-26 13:56:56 -04:00
|
|
|
|
2003-01-30 11:09:20 -05:00
|
|
|
;; does the log-file rotation on signal USR1
|
|
|
|
(define (make-log-file-rotator log-file http-log-lock)
|
2002-05-26 13:56:56 -04:00
|
|
|
(set-interrupt-handler interrupt/usr1 #f)
|
|
|
|
(lambda ()
|
|
|
|
(on-interrupt
|
|
|
|
interrupt/usr1
|
|
|
|
(lambda ()
|
2002-12-29 14:10:10 -05:00
|
|
|
(with-lock http-log-lock
|
|
|
|
(lambda ()
|
|
|
|
(close-output-port (logging-http-log-port))
|
2003-01-30 11:09:20 -05:00
|
|
|
(set-logging-http-log-port (open-log-file log-file))))))))
|
2002-05-26 13:56:56 -04:00
|
|
|
|
2003-01-30 11:09:20 -05:00
|
|
|
(define (open-log-file log-file)
|
2002-05-26 13:56:56 -04:00
|
|
|
(with-errno-handler*
|
|
|
|
(lambda (errno packet)
|
|
|
|
(http-syslog (syslog-level warning)
|
|
|
|
"[httpd] Warning: An error occured while opening ~S for writing (~A).~%Send signal USR1 when the problem is fixed.~%"
|
2003-01-30 11:09:20 -05:00
|
|
|
log-file
|
2002-05-26 13:56:56 -04:00
|
|
|
(car packet))
|
|
|
|
(make-null-output-port))
|
|
|
|
(lambda ()
|
2003-01-30 11:09:20 -05:00
|
|
|
(open-output-file log-file
|
2002-05-26 13:56:56 -04:00
|
|
|
(bitwise-ior open/create open/append)))))
|
|
|
|
|
|
|
|
; returns a string for a CLF entry (Common Log Format)
|
|
|
|
; note: till now, we do not log the user's time zone code
|
|
|
|
(define (make-CLF remote-ip request-type requested-file protocol http-code filesize referer user-agent)
|
|
|
|
(format #f "~A - - ~A ~S ~A ~A ~S ~S~%"
|
2002-08-22 12:40:10 -04:00
|
|
|
(or (maybe-dns-lookup remote-ip) "-")
|
2002-05-26 13:56:56 -04:00
|
|
|
(format-date "[~d/~b/~Y:~H:~M:~S +0000]" (date)) ; +0000 as we don't know
|
2002-08-22 09:19:57 -04:00
|
|
|
(string-join (list request-type
|
|
|
|
(string-append "/" requested-file)
|
|
|
|
protocol))
|
2002-05-26 13:56:56 -04:00
|
|
|
; Unfortunately, we first split the request line into
|
|
|
|
; method/request-type etc. and put it together here.
|
|
|
|
; Files conform to CLF are expected to print the original line.
|
|
|
|
(or http-code "-")
|
|
|
|
(or filesize "-")
|
2003-02-25 12:36:36 -05:00
|
|
|
(if (string? referer) (string-trim referer) '-)
|
2002-05-26 13:56:56 -04:00
|
|
|
(if (string? user-agent)
|
|
|
|
(string-trim user-agent char-set:whitespace)
|
2003-02-25 12:36:36 -05:00
|
|
|
'-)))
|
2002-05-26 13:56:56 -04:00
|
|
|
|
|
|
|
|
2002-08-22 12:40:10 -04:00
|
|
|
(define (maybe-dns-lookup remote-ip)
|
2002-09-04 10:01:34 -04:00
|
|
|
(if (logging-dns-lookup?)
|
2002-12-19 09:30:39 -05:00
|
|
|
(or (with-fatal-error-handler*
|
|
|
|
(lambda (condition decline)
|
2002-12-29 14:10:10 -05:00
|
|
|
(http-syslog (syslog-level debug)
|
|
|
|
"An error occured while resolving IP ~A: ~A"
|
|
|
|
remote-ip condition)
|
2002-12-19 09:30:39 -05:00
|
|
|
remote-ip)
|
|
|
|
(lambda ()
|
|
|
|
(dns-lookup-ip remote-ip)))
|
2002-08-22 12:40:10 -04:00
|
|
|
remote-ip)
|
|
|
|
remote-ip))
|