Merge pull request #145 from Devon7/master
[hunchentoot.git] / log.lisp
blobd76169255289b9b66d7f2b0c0f364d4670408da4
1 ;;; -*- Mode: LISP; Syntax: COMMON-LISP; Package: HUNCHENTOOT; Base: 10 -*-
3 ;;; Copyright (c) 2004-2010, Dr. Edmund Weitz. All rights reserved.
5 ;;; Redistribution and use in source and binary forms, with or without
6 ;;; modification, are permitted provided that the following conditions
7 ;;; are met:
9 ;;; * Redistributions of source code must retain the above copyright
10 ;;; notice, this list of conditions and the following disclaimer.
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.
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.
29 (in-package :hunchentoot)
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*.
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)))))))))))