forked from lhope/cl-syslog
-
Notifications
You must be signed in to change notification settings - Fork 1
/
cl-syslog.lisp
67 lines (54 loc) · 1.62 KB
/
cl-syslog.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
56
57
58
59
60
61
62
63
64
65
66
67
;;;; See the LICENSE file for licensing information.
(in-package #:syslog)
;;
;; Condition
;;
(define-condition invalid-facility (error)
((facility
:reader facility
:initarg :facility))
(:report (lambda (condition stream)
(format stream "Invalid facility ~A." (facility condition)))))
(define-condition invalid-priority (error)
((priority
:reader priority
:initarg :priority))
(:report (lambda (condition stream)
(format stream "Invalid priority ~A." (priority condition)))))
;;
;; Foreign function
;;
(cffi:defcfun "openlog" :void
(ident :string)
(option :int)
(facility :int))
(cffi:defcfun "closelog" :void)
(cffi:defcfun "syslog" :void
(priority :int)
(format :string)
&rest)
;;
;; Utility
;;
(defun get-facility (facility-name)
"Return facility number given the facility's name. If there is no
such facility, signal `invalid-facility' error."
(ash (or (cdr (assoc facility-name *facilities*))
(error 'invalid-facility :facility facility-name))
3))
(defun get-priority (priority-name)
"Return priority number given the priority's name. If there is no
such priority, signal `invalid-priority' error."
(or (cdr (assoc priority-name *priorities*))
(error 'invalid-priority :priority priority-name)))
;;
;; Log function
;;
(defun log (name facility priority text &optional (option 0))
"Print message to syslog.
'option' can be any of the +log...+ constants"
(cffi:with-foreign-strings ((cname name) (ctext text))
(openlog cname option (get-facility facility))
(syslog (get-priority priority) "%s" :string ctext)
(closelog))
text)