log.lisp 1 ;;; -*- Mode: LISP; Syntax: COMMON-LISP; Package: HUNCHENTOOT; Base: 10 -*-
2
3 ;;; Copyright (c) 2004-2010, Dr. Edmund Weitz. All rights reserved.
4
5 ;;; Redistribution and use in source and binary forms, with or without
6 ;;; modification, are permitted provided that the following conditions
7 ;;; are met:
8
9 ;;; * Redistributions of source code must retain the above copyright
10 ;;; notice, this list of conditions and the following disclaimer.
11
12 ;;; * Redistributions in binary form must reproduce the above
13 ;;; copyright notice, this list of conditions and the following
14 ;;; disclaimer in the documentation and/or other materials
15 ;;; provided with the distribution.
16
17 ;;; THIS SOFTWARE IS PROVIDED BY THE AUTHOR 'AS IS' AND ANY EXPRESSED
18 ;;; OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 ;;; WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 ;;; ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
21 ;;; DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 ;;; DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23 ;;; GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 ;;; INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25 ;;; WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26 ;;; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27 ;;; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
29 (in-package :hunchentoot)
30
31 (defmacro with-log-stream ((stream-var destination lock) &body body)
32 "Helper macro to write log entries. STREAM-VAR is a symbol that
33 will be bound to the logging stream during the execution of BODY.
34 DESTINATION is the logging destination, which can be either a pathname
35 designator of the log file, a symbol designating an open stream or NIL
36 if no logging should be done. LOCK refers to the lock that should be
37 held during the logging operation. If DESTINATION is a pathname, a
38 flexi stream with UTF-8 encoding will be created and bound to
39 STREAM-VAR. If an error occurs while writing to the log file, that
40 error will be logged to *ERROR-OUTPUT*.
41
42 Note that logging to a file involves opening and closing the log file
43 for every logging operation, which is overall costly. Web servers
44 with high throughput demands should make use of a specialized logging
45 function rather than relying on Hunchentoot's default logging
46 facility."
47 (with-unique-names (binary-stream)
48 (with-rebinding (destination)
49 (let ((body body))
50 `(when ,destination
51 (with-lock-held (,lock)
52 (etypecase ,destination
53 ((or string pathname)
54 (with-open-file (,binary-stream ,destination
55 :direction :output
56 :element-type 'octet
57 :if-does-not-exist :create
58 :if-exists :append
59 #+:openmcl :sharing #+:openmcl :lock)
60 (let ((,stream-var (make-flexi-stream ,binary-stream :external-format +utf-8+)))
61 ,@body)))
62 (stream
63 (let ((,stream-var ,destination))
64 (prog1 (progn ,@body)
65 (finish-output ,destination)))))))))))
66