-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathtrace.rkt
153 lines (137 loc) · 5.56 KB
/
trace.rkt
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
#lang racket/base
;; al2-profiler.rkt -- profiling and tracing capabilities
;;
;; This file is part of ActivityLog2, an fitness activity tracker
;; Copyright (C) 2016, 2020 Alex Harsányi <[email protected]>
;;
;; This program is free software: you can redistribute it and/or modify it
;; under the terms of the GNU General Public License as published by the Free
;; Software Foundation, either version 3 of the License, or (at your option)
;; any later version.
;;
;; This program is distributed in the hope that it will be useful, but WITHOUT
;; ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
;; FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
;; more details.
(require (for-syntax racket/base racket/list)
racket/format
racket/string)
(provide
define/trace
define/public/trace
define/augment/trace
define/private/trace
define/override/trace
define/augride/trace
trace-output)
;; Current indent level for tracing
(define indent-level (make-parameter 0))
;; Number of spaces by which we increase the indent for each nested function
;; call.
(define indent-amount 2)
;; A pre-prepared padding string for the indent
(define indent-padding (make-parameter ""))
;; Port where tracing output is sent (if #f, it is sent to
;; current-output-port).
(define trace-output (make-parameter #f))
;; Define a traced function. When the function is invoked, its name and the
;; value of its parameters are printed to TRACE-OUTPUT and when returning the
;; results are printed out.
(define-syntax (define/trace stx)
(syntax-case stx ()
[(_ (name . args) body ...)
(with-syntax
([arg-vals
(datum->syntax
stx
(let ([e (syntax-e #'args)])
(cond
[(symbol? e)
;; (define (foo . args) ...) case
`(cons (~s (quote ,(syntax->datum #'name))) (map ~s ,e))]
[(list? e)
;; (define (a b #:keyword k) ...) case
`(list
(~s (quote ,(syntax->datum #'name)))
,@(for/list ([item (in-list e)])
(if (keyword? (syntax-e item))
(let ([kw (string-append "#:" (keyword->string (syntax-e item)))])
`(~a ,kw))
`(~s ,item))))]
[#t
;; (define (a b #:keyword k . rest) ...) case
(define-values (a r)
(let loop ([e e]
[result '()])
(if (cons? e)
(loop (cdr e) (cons (car e) result))
(values (reverse result) e))))
`(append
(list
(~s (quote ,(syntax->datum #'name)))
,@(for/list ([item (in-list a)])
(if (keyword? (syntax-e item))
(let ([kw (string-append "#:" (keyword->string (syntax-e item)))])
`(~a ,kw))
`(~s ,item))))
(map ~a ,r))])))])
#`(define (name . args)
(let ((out (or (trace-output) (current-output-port)))
(text (string-append
"T" (~a #:width 3 (indent-level) #:align 'right #:pad-string "0") ":"
(indent-padding)
(string-join arg-vals #:before-first "(" #:after-last ")")
)))
(display text out)
(newline out))
(call-with-values
(lambda ()
(parameterize* ((indent-level (+ (indent-level) indent-amount))
(indent-padding (make-string (indent-level) #\ )))
body ...))
(lambda result
(let* ((out (or (trace-output) (current-output-port)))
(sresult (map ~a result))
(text (string-append
"R" (~a #:width 3 (indent-level) #:align 'right #:pad-string "0") ":"
(indent-padding)
(string-join sresult #:before-first "(values " #:after-last ")"))))
(display text out)
(newline out))
(apply values result)))))]))
;; Same as define/trace, but for different class methods
(define-syntax (define/public/trace stx)
(syntax-case stx ()
[(_ (name . args) body ...)
(with-syntax ([name+args (datum->syntax stx (list* #'name #'args))])
#'(begin
(public name)
(define/trace name+args body ...)))]))
(define-syntax (define/augment/trace stx)
(syntax-case stx ()
[(_ (name . args) body ...)
(with-syntax ([name+args (datum->syntax stx (list* #'name #'args))])
#'(begin
(augment name)
(define/trace name+args body ...)))]))
(define-syntax (define/private/trace stx)
(syntax-case stx ()
[(_ (name . args) body ...)
(with-syntax ([name+args (datum->syntax stx (list* #'name #'args))])
#'(begin
(private name)
(define/trace name+args body ...)))]))
(define-syntax (define/override/trace stx)
(syntax-case stx ()
[(_ (name . args) body ...)
(with-syntax ([name+args (datum->syntax stx (list* #'name #'args))])
#'(begin
(override name)
(define/trace name+args body ...)))]))
(define-syntax (define/augride/trace stx)
(syntax-case stx ()
[(_ (name . args) body ...)
(with-syntax ([name+args (datum->syntax stx (list* #'name #'args))])
#'(begin
(augride name)
(define/trace name+args body ...)))]))