-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogging.lisp
95 lines (73 loc) · 2.58 KB
/
logging.lisp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
(in-package "LOG")
;;------------------------------------------------------------------------------
;;
;; File: LOGGING.LISP
;; Created: 10/19/94
;; Author: Will Fitzgerald
;;
;; Description: A simple logging facility
;;
;;------------------------------------------------------------------------------
;;------------------------------------------------------------------------------
;; Packages
;;------------------------------------------------------------------------------
(eval-when (load eval compile)
(unless (find-package :log)
(make-package :log)))
(in-package :log)
(use-package :tables)
(export '(reset-log set-logging record-log print-log with-logging))
;;------------------------------------------------------------------------------
;; A log is a list of statements keyed off a symbolic form.
;;------------------------------------------------------------------------------
(deftable log-of)
(defvar *logging* nil)
(defvar *log-keys* nil)
(defun reset-log ()
(clear-table (log-of))
(setf *log-keys* nil)
*logging*)
;;------------------------------------------------------------------------------
;; Turning logging off and on.
;;------------------------------------------------------------------------------
(defun set-logging (&optional (value t))
(setf *logging* value))
(defmacro with-logging (&rest body)
`(let ((*logging* t))
(reset-log)
,@body))
;;------------------------------------------------------------------------------
;; Making records in the log
;;------------------------------------------------------------------------------
(defun make-statement (string args)
(format nil "~?" string args))
(defun record-log (logname string &rest args)
(when *logging*
(push (make-statement string args) (log-of logname))
(pushnew logname *log-keys* )
*logging*))
;;------------------------------------------------------------------------------
;; Printing the log
;;------------------------------------------------------------------------------
(defun print-log (&optional logname (stream *standard-output*))
(if logname
(loop for log-entry in (reverse (log-of logname))
doing
(format stream "~A~%" log-entry))
(loop for log-key in (reverse *log-keys*) doing
(print-log log-key stream)))
(values))
#|
(defun fact (n)
(record-log 'fact "entering FACT with ~S" n)
(cond
((= n 1) 1)
(t (* (fact (1- n)) n))))
(set-logging)
(reset-log)
(fact 20)
(print-log)
(set-logging nil)
(with-logging (fact 4))
(print-log)
|#