Add the logging module.
This commit is contained in:
parent
c634bf733f
commit
e8bd82ef1e
2 changed files with 93 additions and 1 deletions
|
@ -48,7 +48,7 @@ HACKERBASE-OBJS=hackerbase.o testing.o listing.o month.o period.o \
|
||||||
util-set-list.o util-time.o util-tag.o util-io.o \
|
util-set-list.o util-time.o util-tag.o util-io.o \
|
||||||
util-string.o util-io.o util-list.o util-parser.o texts.o \
|
util-string.o util-io.o util-list.o util-parser.o texts.o \
|
||||||
tests.o util-proc.o util-mail.o reminders.o util-format.o \
|
tests.o util-proc.o util-mail.o reminders.o util-format.o \
|
||||||
brmember-format.o
|
brmember-format.o logging.o
|
||||||
|
|
||||||
.PHONY: imports
|
.PHONY: imports
|
||||||
imports: $(HACKERBASE-DEPS)
|
imports: $(HACKERBASE-DEPS)
|
||||||
|
@ -338,3 +338,8 @@ BRMEMBER-FORMAT-SOURCES=brmember-format.scm util-dict-list.import.scm \
|
||||||
|
|
||||||
brmember-format.o: brmember-format.import.scm
|
brmember-format.o: brmember-format.import.scm
|
||||||
brmember-format.import.scm: $(BRMEMBER-FORMAT-SOURCES)
|
brmember-format.import.scm: $(BRMEMBER-FORMAT-SOURCES)
|
||||||
|
|
||||||
|
LOGGING-SOURCES=logging.scm util-string.import.scm
|
||||||
|
|
||||||
|
logging.o: logging.import.scm
|
||||||
|
logging.import.scm: $(LOGGING-SOURCES)
|
||||||
|
|
87
src/logging.scm
Normal file
87
src/logging.scm
Normal file
|
@ -0,0 +1,87 @@
|
||||||
|
;;
|
||||||
|
;; logging.scm
|
||||||
|
;;
|
||||||
|
;; Universal logging module.
|
||||||
|
;;
|
||||||
|
;; ISC License
|
||||||
|
;;
|
||||||
|
;; Copyright 2023 Brmlab, z.s.
|
||||||
|
;; Dominik Pantůček <dominik.pantucek@trustica.cz>
|
||||||
|
;;
|
||||||
|
;; Permission to use, copy, modify, and/or distribute this software
|
||||||
|
;; for any purpose with or without fee is hereby granted, provided
|
||||||
|
;; that the above copyright notice and this permission notice appear
|
||||||
|
;; in all copies.
|
||||||
|
;;
|
||||||
|
;; THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
|
||||||
|
;; WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
|
||||||
|
;; WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
|
||||||
|
;; AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR
|
||||||
|
;; CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
|
||||||
|
;; OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
|
||||||
|
;; NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
|
||||||
|
;; CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||||
|
;;
|
||||||
|
|
||||||
|
(declare (unit logging))
|
||||||
|
|
||||||
|
(module
|
||||||
|
logging
|
||||||
|
(
|
||||||
|
*log-file*
|
||||||
|
|
||||||
|
log-debug
|
||||||
|
log-info
|
||||||
|
log-warning
|
||||||
|
log-error
|
||||||
|
)
|
||||||
|
|
||||||
|
(import scheme
|
||||||
|
(chicken base)
|
||||||
|
(chicken format)
|
||||||
|
(chicken time posix)
|
||||||
|
util-string)
|
||||||
|
|
||||||
|
;; No logging by default
|
||||||
|
(define *log-file* (make-parameter #f))
|
||||||
|
|
||||||
|
;; Opened log file
|
||||||
|
(define log-file (make-parameter #f))
|
||||||
|
|
||||||
|
;; Ensures leading zeroes
|
||||||
|
(define (format-number2 n)
|
||||||
|
(format "~A~A"
|
||||||
|
(if (< n 10) "0" "")
|
||||||
|
n))
|
||||||
|
|
||||||
|
;; Current time for logging
|
||||||
|
(define (format-current-time)
|
||||||
|
(let ((ltime (seconds->local-time)))
|
||||||
|
(format "~A-~A-~A ~A:~A:~A"
|
||||||
|
(+ 1900 (vector-ref ltime 5))
|
||||||
|
(format-number2 (add1 (vector-ref ltime 4)))
|
||||||
|
(format-number2 (vector-ref ltime 3))
|
||||||
|
(format-number2 (vector-ref ltime 2))
|
||||||
|
(format-number2 (vector-ref ltime 1))
|
||||||
|
(format-number2 (vector-ref ltime 0)))))
|
||||||
|
|
||||||
|
;; Handles the actual logging
|
||||||
|
(define ((log-line level) fmt . args)
|
||||||
|
(when (or (not (*log-file*))
|
||||||
|
(not (log-file)))
|
||||||
|
(when (not (log-file))
|
||||||
|
(log-file (open-output-file (*log-file*) #:append)))
|
||||||
|
(display (format "~A [~A] ~A"
|
||||||
|
(format-current-time)
|
||||||
|
(string-upcase (symbol->string level))
|
||||||
|
(apply format fmt args))
|
||||||
|
(log-file))
|
||||||
|
(flush-output (log-file))))
|
||||||
|
|
||||||
|
;; Specific log procedures
|
||||||
|
(define log-debug (log-line 'debug))
|
||||||
|
(define log-info (log-line 'info))
|
||||||
|
(define log-warning (log-line 'warning))
|
||||||
|
(define log-error (log-line 'error))
|
||||||
|
|
||||||
|
)
|
Loading…
Add table
Add a link
Reference in a new issue