forked from cxxxr/cl-lsp
-
Notifications
You must be signed in to change notification settings - Fork 2
/
logger.lisp
38 lines (33 loc) · 1.09 KB
/
logger.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
(defpackage :cl-lsp/logger
(:use :cl)
(:export :*enable-logger*
:*logger-stream*
:log-format
:with-log-file
:with-log-stream))
(in-package :cl-lsp/logger)
(defvar *enable-logger* nil)
(defvar *logger-stream*)
(let ((lock (bt:make-lock)))
(defun log-format (string &rest args)
(bt:with-lock-held (lock)
(when (boundp '*logger-stream*)
(apply #'format *logger-stream* string args)
(force-output *logger-stream*)))))
(defun call-with-log-file (file function)
(if *enable-logger*
(let ((stream (open file
:direction :output
:if-does-not-exist :create
:if-exists :append)))
(setf *logger-stream* stream)
(unwind-protect (funcall function)
(close stream)))
(funcall function)))
(defmacro with-log-file ((file) &body body)
`(call-with-log-file ,file (lambda () ,@body)))
(defmacro with-log-stream ((stream) &body body)
`(if *enable-logger*
(let ((*logger-stream* ,stream))
,@body)
(progn ,@body)))