-
-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathlog.lisp
55 lines (47 loc) · 1.75 KB
/
log.lisp
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
(in-package #:jupyter)
(defvar *kernel* nil)
(defclass sink ()
((path
:initarg :path
:accessor logger-path)
(stream
:initform nil
:accessor logger-stream)
(lock
:initform (bordeaux-threads:make-lock (make-uuid))
:reader logger-lock)))
(defclass source ()
((sink
:initarg :sink
:initform nil
:accessor source-sink)))
(defmethod start ((l sink))
(with-slots (lock path stream) l
(bordeaux-threads:with-lock-held (lock)
(uiop:ensure-all-directories-exist (list path))
(setf stream (open path :direction :output
:if-exists :rename
:if-does-not-exist :create
#+ccl :sharing #+ccl :external)))))
(defmethod stop ((l sink))
(with-slots (lock stream) l
(bordeaux-threads:with-lock-held (lock)
(close stream)
(setf stream nil))))
(defun inform (level src format-control &rest format-arguments)
(alexandria:when-let ((s (or src *kernel*)))
(with-slots (lock stream) (source-sink s)
(bordeaux-threads:with-lock-held (lock)
(ignore-errors
(when (and (eql :error level)
*kernel*)
(format *error-output* "<~A> ~?~%"
(class-name (class-of src)) format-control format-arguments)
(finish-output *error-output*)))
(multiple-value-bind (second minute hour day month year)
(get-decoded-time)
(format stream "~4,'0d-~2,'0d-~2,'0d ~2,'0d:~2,'0d:~2,'0d [~A] <~A> ~?~%"
year month day hour minute second
level (class-name (class-of src))
format-control format-arguments)
(finish-output stream))))))