forked from greghendershott/racket-mode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.rkt
89 lines (80 loc) · 3.08 KB
/
logger.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
#lang at-exp racket/base
(require racket/match
racket/list
racket/format
racket/string
"util.rkt")
(provide start-log-receiver
log-display)
(define current-log-receiver-thread (make-parameter #f))
(define global-logger (make-logger))
(current-logger global-logger)
(define other-level 'fatal)
;; Default a couple specific loggers one notch above their "noisy"
;; level. That way, if someone sets "all other" loggers to e.g. debug,
;; these won't get noisy. They need to be specifically cranked up.
(define logger-levels (make-hasheq '([cm-accomplice . warning]
[GC . info]
[module-prefetch . warning]
[optimizer . info]
[sequence-specialization . info])))
(define racket-log-file (build-path (find-system-path 'temp-dir) "racket-log"))
(with-output-to-file racket-log-file #:exists 'truncate void)
(define (update-log-receiver)
(show-logger-levels)
(start-log-receiver))
(define (start-log-receiver)
(cond [(current-log-receiver-thread) => kill-thread])
(let* ([args (append (list global-logger)
(flatten (for/list ([(k v) logger-levels])
(list v k)))
(list other-level))]
[r (apply make-log-receiver args)])
(current-log-receiver-thread
(thread
(λ ()
(let loop ()
(match (sync r)
[(vector l m v name)
(define s @~a{[@l] @m})
(display-commented s)
(flush-output (current-error-port))
;; To /tmp/racket-log (can `tail -f' it)
(with-output-to-file racket-log-file #:exists 'append
(λ () (displayln s)))])
(loop)))))))
(define (show-logger-levels)
(define wid 30)
(define (pr k v)
(printf "; ~a ~a\n"
(~a k
#:min-width wid
#:max-width wid
#:limit-marker "...")
v))
(pr "Logger" "Level")
(pr (make-string wid #\-) "-------")
(for ([(k v) logger-levels])
(pr k v))
(pr "[all other]" other-level)
(printf "; Writing ~v.\n" racket-log-file))
(define (log-display specs)
(match specs
[(list) (show-logger-levels)]
[(list (and level (or 'none 'fatal 'error 'warning 'info 'debug)))
(set! other-level level)
(update-log-receiver)]
[(list logger 'default)
(hash-remove! logger-levels logger)
(update-log-receiver)]
[(list logger (and level (or 'none 'fatal 'error 'warning 'info 'debug)))
(hash-set! logger-levels logger level)
(update-log-receiver)]
[_ (eprintf
(string-join
'("; Usage:"
",log -- show the levels currently in effect."
",log <logger> <level> -- set logger to level debug|info|warning|error|fatal|none"
",log <logger> default -- set logger to use the default, 'all other' level."
",log <level> -- set the default level, for 'all other' loggers.\n")
"\n; "))]))