Skip to content

Commit

Permalink
log4cl syslog: don't require cffi when unnecessary
Browse files Browse the repository at this point in the history
We create a separate system for this functionality. Also, if
implementation has its native support for syslog - use it. SBCL for
now.

Moreover we remove ABCL hack (since it is a separate system, then if
cffi fails to load – it's OK – system won't load anyway).
  • Loading branch information
dkochmanski committed Jan 14, 2017
1 parent 1c77374 commit 5c6f819
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 42 deletions.
18 changes: 10 additions & 8 deletions log4cl.asd
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,10 @@

(in-package :log4cl.system)

#+abcl
(eval-when (:compile-toplevel :load-toplevel :execute)
(ignore-errors
(mapc #'require '(abcl-contrib jna))
(pushnew :abcl-jna *features*)))

(defsystem :log4cl
:version "1.1.3"
:depends-on (:bordeaux-threads
:bt-semaphore
#-(and abcl (not abcl-jna)) :cl-syslog
#+sbcl :sb-posix)
:components
((module "src" :serial t
Expand All @@ -47,13 +40,22 @@
(:file "simple-layout")
(:file "pattern-layout")
(:file "appender")
#-(and abcl (not abcl-jna)) (:file "syslog-appender")
(:file "watcher")
(:file "configurator")
(:file "property-parser")
(:file "property-configurator")
(:file "package")))))

(defsystem :log4cl/syslog
:version "1.1.3"
:depends-on (:log4cl
#-sbcl :cl-syslog)
:components ((:module "src"
:serial t
:components ((:file "syslog-appender")
#+sbcl (:file "syslog-appender-sbcl")
#-sbcl (:file "syslog-appender-cffi")))))

(defsystem :log4cl/test
:version "1.1.3"
:depends-on (:log4cl :stefil)
Expand Down
30 changes: 30 additions & 0 deletions src/syslog-appender-cffi.lisp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; Base: 10 -*-
;;;
;;; Copyright (c) 2013, 2014, Jan Moringen. All rights reserved.
;;;
;;; License: Apache-2.0
;;;
(in-package #:log4cl)

(defmethod appender-do-append ((appender syslog-appender) logger level log-func)
(let* ((syslog-level (%log4cl-level->syslog-level level))
(layout (appender-layout appender))
(message
(with-output-to-string (stream)
(layout-to-stream layout stream logger level log-func))))
(cl-syslog:log (syslog-appender-name appender) :user syslog-level message
(if (syslog-appender-include-pid? appender)
cl-syslog:+log-pid+ 0))))


;; Utility functions

(defun %log4cl-level->syslog-level (level)
(let ((level/keyword (aref +log-level-to-keyword+ level)))
(ecase level/keyword
(:fatal :crit)
(:error :err)
(:warn :warning)
(:info :info)
((:debu1 :debu2 :debu3 :debu4 :debu5 :debu6 :debu7 :debu8 :debu9)
:debug))))
32 changes: 32 additions & 0 deletions src/syslog-appender-sbcl.lisp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; Base: 10 -*-
;;;
;;; Copyright (c) 2013, 2014, Jan Moringen. All rights reserved.
;;;
;;; License: Apache-2.0
;;;
(in-package #:log4cl)

(defmethod appender-do-append ((appender syslog-appender) logger level log-func)
(let* ((syslog-level (%log4cl-level->syslog-level level))
(layout (appender-layout appender))
(message
(with-output-to-string (stream)
(layout-to-stream layout stream logger level log-func))))
(sb-posix:openlog (syslog-appender-name appender)
(logior sb-posix:log-user
(if (syslog-appender-include-pid? appender)
sb-posix:log-pid 0)))
(sb-posix:syslog syslog-level "~A" message)
(sb-posix:closelog)))

;; Utility functions

(defun %log4cl-level->syslog-level (level)
(let ((level/keyword (aref +log-level-to-keyword+ level)))
(ecase level/keyword
(:fatal sb-posix:log-crit)
(:error sb-posix:log-err)
(:warn sb-posix:log-warning)
(:info sb-posix:log-info)
((:debu1 :debu2 :debu3 :debu4 :debu5 :debu6 :debu7 :debu8 :debu9)
sb-posix:log-debug))))
37 changes: 3 additions & 34 deletions src/syslog-appender.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,8 @@
;;;
;;; Copyright (c) 2013, 2014, Jan Moringen. All rights reserved.
;;;
;;; This file is licensed to You under the Apache License, Version 2.0
;;; (the "License"); you may not use this file except in compliance
;;; with the License. You may obtain a copy of the License at
;;; http://www.apache.org/licenses/LICENSE-2.0
;;; License: Apache-2.0
;;;
;;; Unless required by applicable law or agreed to in writing, software
;;; distributed under the License is distributed on an "AS IS" BASIS,
;;; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
;;; See the License for the specific language governing permissions and
;;; limitations under the License.

(in-package #:log4cl)

Expand All @@ -22,8 +14,8 @@
:accessor syslog-appender-include-pid?))
(:default-initargs
:layout (make-instance 'pattern-layout :conversion-pattern "%m")
:name (lisp-implementation-type)
:include-pid? t)
:name (lisp-implementation-type)
:include-pid? t)
(:documentation
"An appender that writes log messages to the syslog.
Expand All @@ -36,30 +28,7 @@ The :include-pid? initarg controls whether log entries produced by the
syslog connection should include the process id (PID). The default is
true."))


(defmethod appender-do-append ((appender syslog-appender) logger level log-func)
(let* ((syslog-level (%log4cl-level->syslog-level level))
(layout (appender-layout appender))
(message
(with-output-to-string (stream)
(layout-to-stream layout stream logger level log-func))))
(cl-syslog:log (syslog-appender-name appender) :user syslog-level message
(if (syslog-appender-include-pid? appender)
cl-syslog:+log-pid+ 0))))

(defmethod property-alist ((instance syslog-appender))
(append (call-next-method)
'((:name name :string-skip-whitespace)
(:include-pid? include-pid? boolean))))

;; Utility functions

(defun %log4cl-level->syslog-level (level)
(let ((level/keyword (aref +log-level-to-keyword+ level)))
(ecase level/keyword
(:fatal :crit)
(:error :err)
(:warn :warning)
(:info :info)
((:debu1 :debu2 :debu3 :debu4 :debu5 :debu6 :debu7 :debu8 :debu9)
:debug))))

0 comments on commit 5c6f819

Please sign in to comment.