aboutsummaryrefslogtreecommitdiff
path: root/legacy/bigloo/debug.scm
blob: 8f1691c1ccab134d0e6cac8a77955f318a696960 (plain)
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
;*=====================================================================*/
;*    serrano/prgm/project/skribe/src/bigloo/debug.scm                 */
;*    -------------------------------------------------------------    */
;*    Author      :  Manuel Serrano                                    */
;*    Creation    :  Wed Jun 11 10:01:47 2003                          */
;*    Last change :  Thu Oct 28 21:33:00 2004 (eg)                     */
;*    Copyright   :  2003 Manuel Serrano                               */
;*    -------------------------------------------------------------    */
;*    Simple debug facilities                                          */
;*=====================================================================*/

;*---------------------------------------------------------------------*/
;*    The module                                                       */
;*---------------------------------------------------------------------*/
(module skribe_debug

   (export *skribe-debug*
	   *skribe-debug-symbols*
	   *skribe-debug-color*

	   (skribe-debug::int)
	   (debug-port::output-port . ::obj)
	   (debug-margin::bstring)
	   (debug-color::bstring ::int . ::obj)
	   (debug-bold::bstring . ::obj)
	   (debug-string ::obj)
	   (debug-item . ::obj)
	   
	   (%with-debug ::obj ::obj ::procedure)))

;*---------------------------------------------------------------------*/
;*    *skribe-debug* ...                                               */
;*---------------------------------------------------------------------*/
(define *skribe-debug* 0)

;*---------------------------------------------------------------------*/
;*    *skribe-debug-symbols* ...                                       */
;*---------------------------------------------------------------------*/
(define *skribe-debug-symbols* '())

;*---------------------------------------------------------------------*/
;*    *skribe-debug-color* ...                                         */
;*---------------------------------------------------------------------*/
(define *skribe-debug-color* #t)

;*---------------------------------------------------------------------*/
;*    *skribe-debug-item* ...                                          */
;*---------------------------------------------------------------------*/
(define *skribe-debug-item* #f)

;*---------------------------------------------------------------------*/
;*    *debug-port* ...                                                 */
;*---------------------------------------------------------------------*/
(define *debug-port* (current-error-port))

;*---------------------------------------------------------------------*/
;*    *debug-depth* ...                                                */
;*---------------------------------------------------------------------*/
(define *debug-depth* 0)

;*---------------------------------------------------------------------*/
;*    *debug-margin* ...                                               */
;*---------------------------------------------------------------------*/
(define *debug-margin* "")

;*---------------------------------------------------------------------*/
;*    *skribe-margin-debug-level* ...                                  */
;*---------------------------------------------------------------------*/
(define *skribe-margin-debug-level* 0)

;*---------------------------------------------------------------------*/
;*    skribe-debug ...                                                 */
;*---------------------------------------------------------------------*/
(define (skribe-debug)
  *skribe-debug*)

;*---------------------------------------------------------------------*/
;*    debug-port ...                                                   */
;*---------------------------------------------------------------------*/
(define (debug-port . o)
   (cond
      ((null? o)
       *debug-port*)
      ((output-port? (car o))
       (set! *debug-port* o)
       o)
      (else
       (error 'debug-port "Illegal debug port" (car o)))))

;*---------------------------------------------------------------------*/
;*    debug-margin ...                                                 */
;*---------------------------------------------------------------------*/
(define (debug-margin)
   *debug-margin*)

;*---------------------------------------------------------------------*/
;*    debug-color ...                                                  */
;*---------------------------------------------------------------------*/
(define (debug-color col::int . o)
   (with-output-to-string
      (if *skribe-debug-color*
	  (lambda ()
	     (display* "[1;" (+ 31 col) "m")
	     (apply display* o)
	     (display ""))
	  (lambda ()
	     (apply display* o)))))

;*---------------------------------------------------------------------*/
;*    debug-bold ...                                                   */
;*---------------------------------------------------------------------*/
(define (debug-bold . o)
   (apply debug-color -30 o))

;*---------------------------------------------------------------------*/
;*    debug-item ...                                                   */
;*---------------------------------------------------------------------*/
(define (debug-item . args)
   (if (or (>= *skribe-debug* *skribe-margin-debug-level*)
	   *skribe-debug-item*)
       (begin
	  (display (debug-margin) *debug-port*)
	  (display (debug-color (-fx *debug-depth* 1) "- "))
	  (for-each (lambda (a) (display a *debug-port*)) args)
	  (newline *debug-port*))))

;*---------------------------------------------------------------------*/
;*    %with-debug-margin ...                                           */
;*---------------------------------------------------------------------*/
(define (%with-debug-margin margin thunk)
   (let ((om *debug-margin*))
      (set! *debug-depth* (+fx *debug-depth* 1))
      (set! *debug-margin* (string-append om margin))
      (let ((res (thunk)))
	 (set! *debug-depth* (-fx *debug-depth* 1))
	 (set! *debug-margin* om)
	 res)))
      
;*---------------------------------------------------------------------*/
;*    %with-debug ...                                                  */
;*---------------------------------------------------------------------*/
(define (%with-debug lvl lbl thunk)
   (let ((ol *skribe-margin-debug-level*)
	 (oi *skribe-debug-item*))
      (set! *skribe-margin-debug-level* lvl)
      (let ((r (if (or (and (number? lvl) (>= *skribe-debug* lvl))
		       (and (symbol? lbl)
			    (memq lbl *skribe-debug-symbols*)
			    (set! *skribe-debug-item* #t)))
		   (with-output-to-port *debug-port*
		      (lambda ()
			 (display (debug-margin))
			 (display (if (= *debug-depth* 0)
				      (debug-color *debug-depth* "+ " lbl)
				      (debug-color *debug-depth* "--+ " lbl)))
			 (newline)
			 (%with-debug-margin (debug-color *debug-depth* "  |")
					     thunk)))
		   (thunk))))
	 (set! *skribe-debug-item* oi)
	 (set! *skribe-margin-debug-level* ol)
	 r)))

;*---------------------------------------------------------------------*/
;*    debug-string ...                                                 */
;*---------------------------------------------------------------------*/
(define (debug-string o)
   (with-output-to-string
      (lambda ()
	 (write o))))

;*---------------------------------------------------------------------*/
;*    example                                                          */
;*---------------------------------------------------------------------*/
;; (%with-debug 0 'foo1.1
;; 	     (lambda ()
;; 		(debug-item 'foo2.1)
;; 		(debug-item 'foo2.2)
;; 		(%with-debug 0 'foo2.3
;; 			     (lambda ()
;; 				(debug-item 'foo3.1)
;; 				(%with-debug 0 'foo3.2
;; 					     (lambda ()
;; 						(debug-item 'foo4.1)
;; 						(debug-item 'foo4.2)))
;; 				(debug-item 'foo3.3)))
;; 		(debug-item 'foo2.4)))