| 1 |
;;; |
| 2 |
;;; SRFI-215 - Central Log Exchange |
| 3 |
;;; |
| 4 |
;;; Copyright (c) 2020 Goran Weinholt |
| 5 |
;;; Copyright (c) 2024 Antero Mejr <[email protected]> |
| 6 |
;;; Copyright (c) 2024 Shiro Kawai <[email protected]> |
| 7 |
;;; |
| 8 |
;;; This module is a Gauche port of the SRFI 215 sample implementation, |
| 9 |
;;; and is therefore MIT-licensed. |
| 10 |
;;; |
| 11 |
;;; Permission is hereby granted, free of charge, to any person |
| 12 |
;;; obtaining a copy of this software and associated documentation files |
| 13 |
;;; (the "Software"), to deal in the Software without restriction, |
| 14 |
;;; including without limitation the rights to use, copy, modify, merge, |
| 15 |
;;; publish, distribute, sublicense, and/or sell copies of the Software, |
| 16 |
;;; and to permit persons to whom the Software is furnished to do so, |
| 17 |
;;; subject to the following conditions: |
| 18 |
;;; |
| 19 |
;;; The above copyright notice and this permission notice (including the |
| 20 |
;;; next paragraph) shall be included in all copies or substantial |
| 21 |
;;; portions of the Software. |
| 22 |
;;; |
| 23 |
;;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 24 |
;;; EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 25 |
;;; MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 26 |
;;; NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS |
| 27 |
;;; BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN |
| 28 |
;;; ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 29 |
;;; CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 30 |
;;; SOFTWARE. |
| 31 |
|
| 32 |
;; Originally from SRFI-215 reference implementation by Goran Weinholt |
| 33 |
;; Antero Mejr adapted it to Gauche's gauche.logger layer. |
| 34 |
;; Shiro Kawai modified it to utilize Gauche's style as much. |
| 35 |
|
| 36 |
(define-module srfi.215 |
| 37 |
(use gauche.logger) |
| 38 |
(use util.match) |
| 39 |
(export send-log |
| 40 |
current-log-fields |
| 41 |
current-log-callback |
| 42 |
EMERGENCY ALERT CRITICAL ERROR WARNING NOTICE INFO DEBUG |
| 43 |
)) |
| 44 |
(select-module srfi.215) |
| 45 |
|
| 46 |
(define-syntax define-severities |
| 47 |
(syntax-rules () |
| 48 |
[(_ lookup (name val) ...) |
| 49 |
(begin (define-constant name val) ... |
| 50 |
(define (lookup v) (alist-ref '((val . name) ...) v)))])) |
| 51 |
|
| 52 |
;; These severities are from RFC 5424 ("The Syslog Protocol"). |
| 53 |
(define-severities severity |
| 54 |
(EMERGENCY 0) ; system is unusable |
| 55 |
(ALERT 1) ; action must be taken immediately |
| 56 |
(CRITICAL 2) ; critical conditions |
| 57 |
(ERROR 3) ; error conditions |
| 58 |
(WARNING 4) ; warning conditions |
| 59 |
(NOTICE 5) ; normal but significant condition |
| 60 |
(INFO 6) ; informational messages |
| 61 |
(DEBUG 7) ; debug-level messages |
| 62 |
) |
| 63 |
|
| 64 |
;; Check validity of key-value list PLIST. |
| 65 |
;; SRFI only allows certain types in VALs. We convert evreything else |
| 66 |
;; to a string. |
| 67 |
(define (validate-field-list plist) |
| 68 |
(define (rec kvs) |
| 69 |
(match kvs |
| 70 |
[() '()] |
| 71 |
[(k #f . rest) (rec rest)] |
| 72 |
[(k v . rest) |
| 73 |
(unless (symbol? k) (error "invalid key in field list:" k)) |
| 74 |
`(,k ,(if (or (string? v) |
| 75 |
(exact-integer? v) |
| 76 |
(u8vector? v) |
| 77 |
(<error> v)) |
| 78 |
v |
| 79 |
(write-to-string v)) |
| 80 |
,@(rec rest))] |
| 81 |
[_ (error "Incomplete field plist:" plist)])) |
| 82 |
(rec plist)) |
| 83 |
|
| 84 |
;; Concatenate given plists and convert it to an alist. |
| 85 |
;; Assumes all plists are well-formed. |
| 86 |
(define (plists->alist . plists) |
| 87 |
(define (rec1 plist plists) |
| 88 |
(match plist |
| 89 |
[() (rec2 plists)] |
| 90 |
[(k v . rest) `((,k . ,v) ,@(rec1 rest plists))])) |
| 91 |
(define (rec2 plists) |
| 92 |
(match plists |
| 93 |
[() '()] |
| 94 |
[(plist . rest) (rec1 plist rest)])) |
| 95 |
(rec2 plists)) |
| 96 |
|
| 97 |
(define current-log-fields (make-parameter '() validate-field-list)) |
| 98 |
|
| 99 |
(define current-log-callback |
| 100 |
(make-parameter (^[log-entry] |
| 101 |
(log-format "~a: ~a ~s" |
| 102 |
(severity (alist-ref log-entry 'SEVERITY)) |
| 103 |
(alist-ref log-entry 'MESSAGE) |
| 104 |
(remove |
| 105 |
(^p (memq (car p) '(SEVERITY MESSAGE))) |
| 106 |
log-entry))) |
| 107 |
(^[hook] |
| 108 |
(assume (procedure? hook)) |
| 109 |
hook))) |
| 110 |
|
| 111 |
(define (send-log severity message . plist) |
| 112 |
(assume (and (exact-integer? severity) (<= 0 severity 7)) |
| 113 |
"Expected a severity from 0 to 7, but got:" severity) |
| 114 |
(assume (string? message) |
| 115 |
"Expected message to be a string, but got:" message) |
| 116 |
((current-log-callback) `((SEVERITY . ,severity) |
| 117 |
(MESSAGE . ,message) |
| 118 |
,@(plists->alist (validate-field-list plist) |
| 119 |
(current-log-fields)))) |
| 120 |
(undefined)) ; make the caller not rely on the result of callback |