source: trunk/j/src/org/armedbear/lisp/trace.lisp @ 7848

Last change on this file since 7848 was 7848, checked in by piso, 17 years ago

TRACE-1: wrap trace output in WITH-STANDARD-IO-SYNTAX.

File size: 3.1 KB
Line 
1;;; trace.lisp
2;;;
3;;; Copyright (C) 2003-2004 Peter Graves
4;;; $Id: trace.lisp,v 1.7 2004-09-29 00:49:25 piso Exp $
5;;;
6;;; This program is free software; you can redistribute it and/or
7;;; modify it under the terms of the GNU General Public License
8;;; as published by the Free Software Foundation; either version 2
9;;; of the License, or (at your option) any later version.
10;;;
11;;; This program is distributed in the hope that it will be useful,
12;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
13;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14;;; GNU General Public License for more details.
15;;;
16;;; You should have received a copy of the GNU General Public License
17;;; along with this program; if not, write to the Free Software
18;;; Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19
20(in-package #:system)
21
22(defconstant *untraced-function* (make-symbol "untraced-function"))
23
24(defvar *traced-functions* nil)
25
26(defvar *trace-depth* 0)
27
28(defun list-traced-functions ()
29  *traced-functions*)
30
31(defmacro trace (&rest args)
32  (setf *trace-depth* 0)
33  (if args
34      (expand-trace args)
35      '(list-traced-functions)))
36
37(defun expand-trace (args)
38  (let ((results ()))
39    (dolist (arg args)
40      (if (trace-1 arg)
41          (push arg results)))
42    `',results))
43
44(defun trace-1 (symbol)
45  (unless (fboundp symbol)
46    (error "~S is not the name of a function" symbol))
47  (if (member symbol *traced-functions*)
48      (%format t "~S is already being traced." symbol)
49      (let* ((untraced-function (symbol-function symbol))
50             (trace-function
51              (lambda (&rest args)
52                (with-standard-io-syntax
53                  (%format t (indent "~D: ~S~%") *trace-depth*
54                           (append (list symbol) args)))
55                (incf *trace-depth*)
56                (let ((r (multiple-value-list (apply untraced-function args))))
57                  (decf *trace-depth*)
58                  (with-standard-io-syntax
59                    (%format t (indent "~D: ~A returned") *trace-depth* symbol)
60                    (dolist (val r)
61                      (%format t " ~S" val))
62                    (%format t "~%"))
63                  (values-list r)))))
64        (setf (symbol-function symbol) trace-function)
65        (setf (get symbol *untraced-function*) untraced-function)
66        (push symbol *traced-functions*)
67        symbol)))
68
69(defun indent (string)
70  (concatenate 'string
71               (make-string (* (1+ *trace-depth*) 2) :initial-element #\space)
72               string))
73
74(defmacro untrace (&rest args)
75  (setf *trace-depth* 0)
76  (if (null args)
77      (untrace-all)
78      (dolist (arg args)
79        (if (member arg *traced-functions*)
80            (untrace-1 arg)
81            (%format t "~S is not being traced.~%" arg)))))
82
83(defun untrace-all ()
84  (dolist (arg *traced-functions*)
85    (untrace-1 arg)))
86
87(defun untrace-1 (symbol)
88  (let ((untraced-function (get symbol *untraced-function*)))
89    (setf (symbol-function symbol) untraced-function)
90    (remprop symbol *untraced-function*)
91    (setf *traced-functions* (remove symbol *traced-functions*))))
Note: See TracBrowser for help on using the repository browser.