forked from mighty-gerbils/gerbil
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.ss
175 lines (155 loc) · 4.67 KB
/
logger.ss
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
;;; -*- Gerbil -*-
;;; (C) vyzo
;;; Logging support
(import :gerbil/gambit
:std/error
:std/sugar
:std/format
:std/srfi/19)
(export start-logger!
current-logger
current-logger-options
make-logger-options
logger-options?
deflogger
errorf
warnf
infof
debugf
verbosef
)
(def default-level 1) ; WARN
(def verbose-level 4)
(def levels
(hash (0 'ERROR)
(1 'WARN)
(2 'INFO)
(3 'DEBUG)
(4 'VERBOSE)))
(def symbolic-levels
(hash (ERROR 0)
(WARN 1)
(INFO 2)
(DEBUG 3)
(VERBOSE 4)))
(def (level->symbolic level)
(hash-ref levels level 'UNKNOWN))
(def (object->level level)
(cond
((fixnum? level) level)
((symbol? level)
(hash-ref symbolic-levels level verbose-level))
(else
(raise-bad-argument logger "log level: fixnum or symbol" level))))
;; the current logger actor
(def current-logger
(make-parameter #f))
(def (get-logger)
(cond
((not (actor-thread? (current-thread)))
(current-logger))
((thread-local-get 'logger))
(else
(let (logger (current-logger))
(thread-local-set! 'logger logger)
logger))))
;; options can be:
;; - a level, either symbolic or a fixnum
;; - a logger-options object, which specifies per source options as a hash table of source -> level
(def current-logger-options
(make-parameter 1))
(def (get-logger-options)
(cond
((not (actor-thread? (current-thread)))
(current-logger-options))
((thread-local-get 'logger-options))
((current-logger-options)
=> (lambda (opts)
(let (opts (if (symbol? opts) (object->level opts) opts))
(thread-local-set! 'logger-options opts)
opts)))))
(defstruct logger-options (threshold sources))
;; the generic message logger
(def (log-message source level fmt args)
(def (log-it logger threshold)
(let ((level (object->level level))
(threshold (object->level threshold)))
(when (##fx<= level threshold)
(let ((now (current-date))
(msg (if (null? args) fmt (apply format fmt (map exception->string args)))))
(thread-send logger (!log-message now level source msg))))))
(cond
((get-logger)
=> (lambda (logger)
(let (opts (get-logger-options))
(cond
((logger-options? opts)
(using (opts :- logger-options)
(cond
((hash-get opts.sources source)
=> (cut log-it logger <>))
(else
(log-it logger opts.threshold)))))
(else
(log-it logger opts))))))))
(def (exception->string obj)
(if (exception? obj)
(let (outp (open-output-string))
(display-exception obj outp)
(get-output-string outp))
obj))
;; log sources
(defsyntax (deflogger stx)
(syntax-case stx ()
((_ source)
(with-syntax ((errorf (stx-identifier #'source 'errorf))
(warnf (stx-identifier #'source 'warnf))
(infof (stx-identifier #'source 'infof))
(debugf (stx-identifier #'source 'debugf))
(verbosef (stx-identifier #'source 'verbosef)))
#'(begin
(def (errorf fmt . args)
(log-message 'source 0 fmt args))
(def (warnf fmt . args)
(log-message 'source 1 fmt args))
(def (infof fmt . args)
(log-message 'source 2 fmt args))
(def (debugf fmt . args)
(log-message 'source 3 fmt args))
(def (verbosef fmt . args)
(log-message 'source 4 fmt args)))))))
;; the default logger
(deflogger default)
;;; logger implementation
(defstruct !log-message (ts level source msg))
(def (start-logger! (output (current-error-port)))
(cond
((current-logger))
(else
(let* (((values port own-port?)
(cond
((output-port? output)
(values output #f))
((string? output)
(values (open-output-file [path: output append: #t]) #t))
(else
(raise-bad-argument start-logger! "filename or port" output))))
(srv (spawn/name 'logger logger-server port own-port?)))
(current-logger srv)
srv))))
(def (logger-server port own-port?)
(def (loop)
(match (thread-receive)
((!log-message ts level source msg)
(fprintf port "~a ~a ~a ~a~n"
(date->string ts "~4")
(level->symbolic level)
source msg)
(force-output port)
(loop))
('shutdown
(void))
(else
(loop))))
(try (loop)
(finally (when own-port? (close-port port)))))