Skip to content

Commit 9812766

Browse files
authored
Add debug macro D and T (#194)
* Add debug macro D and T macro D: accepts an expr, log its result to stderr, return result macro T: accepts an expr, log the spent time, return result * use define-syntax-rule for old Racket version compatibility
1 parent ea0cdae commit 9812766

File tree

4 files changed

+54
-24
lines changed

4 files changed

+54
-24
lines changed

.lispwords

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,4 @@
1313
(define-json-union 1)
1414
(call-with-read-lock 1)
1515
(call-with-write-lock 1)
16+
(define-syntax-parse-rule 1)

common/debug.rkt

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#lang racket/base
2+
(require racket/file
3+
racket/runtime-path
4+
racket/match
5+
racket/string
6+
racket/format)
7+
8+
(provide
9+
maybe-debug-log
10+
maybe-debug-file
11+
D
12+
T)
13+
14+
(define debug? #f)
15+
16+
(define-runtime-path df "debug.out.rkt")
17+
(define (maybe-debug-file t)
18+
(when debug?
19+
(display-to-file t df #:exists 'replace)))
20+
21+
(define-runtime-path dp "debug.log")
22+
(define (maybe-debug-log m)
23+
(when debug?
24+
(with-output-to-file dp
25+
#:exists 'append
26+
(lambda ()
27+
(writeln m)))))
28+
29+
(define (err-log tag name msg)
30+
(eprintf "[~a] ~a:\n~a\n" tag name msg))
31+
32+
;; DEBUG macro: evaluates the expression, logs the result, and returns the result.
33+
(define-syntax-rule (D expr)
34+
(call-with-values
35+
(lambda () expr)
36+
(lambda results
37+
(err-log 'DEBUG (quote expr)
38+
(match results
39+
[(list) (format "void")]
40+
[(list x) (~v x)]
41+
[_ (string-join (map ~v results) "\n")]))
42+
(apply values results))))
43+
44+
;; TIME macro: evaluates the expression, logs the time taken, and returns the result.
45+
(define-syntax-rule (T expr)
46+
(let-values ([(results cpu-time real-time gc-time)
47+
(time-apply (lambda () expr) '())])
48+
(err-log 'TIME (quote expr)
49+
(format "cpu time: ~a real time: ~a gc time: ~a"
50+
cpu-time real-time gc-time))
51+
(apply values results)))
52+

lsp/debug.rkt

Lines changed: 0 additions & 23 deletions
This file was deleted.

main.rkt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
racket/class
99
racket/async-channel
1010
"common/interfaces.rkt"
11-
"lsp/debug.rkt"
11+
"common/debug.rkt"
1212
"lsp/methods.rkt"
1313
"lsp/msg-io.rkt"
1414
"lsp/responses.rkt")

0 commit comments

Comments
 (0)