From e8bd82ef1e51371630895460f8091d19b758a1bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Pant=C5=AF=C4=8Dek?= Date: Tue, 11 Apr 2023 20:53:39 +0200 Subject: [PATCH] Add the logging module. --- src/Makefile | 7 +++- src/logging.scm | 87 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 93 insertions(+), 1 deletion(-) create mode 100644 src/logging.scm diff --git a/src/Makefile b/src/Makefile index d4c303a..363f821 100644 --- a/src/Makefile +++ b/src/Makefile @@ -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-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 \ - brmember-format.o + brmember-format.o logging.o .PHONY: imports 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.import.scm: $(BRMEMBER-FORMAT-SOURCES) + +LOGGING-SOURCES=logging.scm util-string.import.scm + +logging.o: logging.import.scm +logging.import.scm: $(LOGGING-SOURCES) diff --git a/src/logging.scm b/src/logging.scm new file mode 100644 index 0000000..2294c10 --- /dev/null +++ b/src/logging.scm @@ -0,0 +1,87 @@ +;; +;; logging.scm +;; +;; Universal logging module. +;; +;; ISC License +;; +;; Copyright 2023 Brmlab, z.s. +;; Dominik Pantůček +;; +;; 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)) + + )