opened up the trace library a little more to make it more useable for Redex

svn: r13027
This commit is contained in:
Robby Findler 2009-01-07 15:44:07 +00:00
parent fe62b97137
commit cda64e40da
2 changed files with 52 additions and 29 deletions

View File

@ -56,3 +56,26 @@ end with a newline, but it may contain internal newlines. Each call or
result is converted into a string using @scheme[pretty-print]. The result is converted into a string using @scheme[pretty-print]. The
parameter's default value prints the given string followed by a newline to parameter's default value prints the given string followed by a newline to
@scheme[(current-output-port)].} @scheme[(current-output-port)].}
@defproc[(trace-apply [id symbol?] [proc procedure?] [kws (listof keyword)] [kw-vals list?] [arg any/c] ...) any/c]{
Calls @scheme[proc] with the arguments supplied in
@scheme[args], @scheme[kws], and @scheme[kw-vals]. Also prints out the
trace information during the call, as described above in the docs for
@scheme[trace], using @scheme[id] as the name of @scheme[proc].
}
@defparam[current-trace-print-args trace-print-args
(-> symbol?
(listof keyword?)
list?
list?
number?)]{
The value of this parameter is invoked to print out the arguments of a
traced call. It receives the name of the function, the function's
ordinary arguments, its keywords, the values of the keywords, and a
number indicating the depth of the call.
}

View File

@ -4,18 +4,15 @@
(for-syntax scheme/base)) (for-syntax scheme/base))
(provide trace untrace (provide trace untrace
current-trace-print-args trace-apply
current-trace-notify) current-trace-notify)
(define max-dash-space-depth 10) (define max-dash-space-depth 10)
(define number-nesting-depth 6) (define number-nesting-depth 6)
(define as-spaces (define (as-spaces s)
(lambda (s) (build-string (string-length s)
(let ((n (string-length s))) (lambda (i) #\space)))
(apply string-append
(let loop ((k n))
(if (zero? k) '("")
(cons " " (loop (sub1 k)))))))))
(define-struct prefix-entry (for-first for-rest)) (define-struct prefix-entry (for-first for-rest))
@ -101,27 +98,28 @@
(lambda (name args kws kw-vals level) (lambda (name args kws kw-vals level)
(as-trace-notify (as-trace-notify
(lambda () (lambda ()
(trace-print-args name args kws kw-vals level))))) ((current-trace-print-args) name args kws kw-vals level)))))
(define trace-print-args (define current-trace-print-args
(lambda (name args kws kw-vals level) (make-parameter
(let-values (((first rest) (lambda (name args kws kw-vals level)
(build-prefixes level))) (let-values (((first rest)
(parameterize ((pretty-print-print-line (build-prefixes level)))
(lambda (n port offset width) (parameterize ((pretty-print-print-line
(display (lambda (n port offset width)
(if n (display
(if (zero? n) first (if n
(format "~n~a" rest)) (if (zero? n) first
(format "~n")) (format "~n~a" rest))
port) (format "~n"))
(if n port)
(if (zero? n) (if n
(string-length first) (if (zero? n)
(string-length rest)) (string-length first)
0)))) (string-length rest))
(pretty-print (append (cons name args) 0))))
(apply append (map list kws kw-vals)))))))) (pretty-print (append (cons name args)
(apply append (map list kws kw-vals)))))))))
(define -:trace-print-results (define -:trace-print-results
(lambda (name results level) (lambda (name results level)
@ -197,6 +195,8 @@
;; the nesting depth: ;; the nesting depth:
(define -:trace-level-key (gensym)) (define -:trace-level-key (gensym))
(define (trace-apply id f kws kw-vals . args) (do-traced id args kws kw-vals f))
;; Apply a traced procedure to arguments, printing arguments ;; Apply a traced procedure to arguments, printing arguments
;; and results. We set and inspect the -:trace-level-key continuation ;; and results. We set and inspect the -:trace-level-key continuation
;; mark a few times to detect tail calls. ;; mark a few times to detect tail calls.