unofficial mirror of guix-patches@gnu.org 
 help / color / mirror / code / Atom feed
blob 913bc9ed3f95564587e95ff1df9d1e1fd0badffd 3459 bytes (raw)
name: guix/logging.scm 	 # note: path name is non-authoritative(*)

 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
 
;;; GNU Guix --- Functional package management for GNU
;;; Copyright © 2023, 2024 Maxim Cournoyer <maxim.cournoyer@gmail.com>
;;;
;;; This file is part of GNU Guix.
;;;
;;; GNU Guix is free software; you can redistribute it and/or modify it
;;; under the terms of the GNU General Public License as published by
;;; the Free Software Foundation; either version 3 of the License, or (at
;;; your option) any later version.
;;;
;;; GNU Guix is distributed in the hope that it will be useful, but
;;; WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;;; GNU General Public License for more details.
;;;
;;; You should have received a copy of the GNU General Public License
;;; along with GNU Guix.  If not, see <http://www.gnu.org/licenses/>.

(define-module (guix logging)
  #:use-module (logging logger)
  #:use-module (logging port-log)

  #:use-module (oop goops)
  #:use-module (srfi srfi-209)

  #:export (setup-logging
            shutdown-logging

            log-level
            log-debug
            log-info
            log-warning
            log-error
            log-critical))

(define-syntax define-log-level
  ;; This macro defines a log-level enum type bound to ENUM-NAME for the
  ;; provided levels.  The levels should be specified in increasing order of
  ;; severity.  It also defines 'log-LEVEL' syntax to more conveniently log at
  ;; LEVEL, with location information.
  (lambda (x)
    (define-syntax-rule (id parts ...)
      ;; Assemble PARTS into a raw (unhygienic) identifier.
      (datum->syntax x (symbol-append (syntax->datum parts) ...)))

    (syntax-case x ()
      ((_ enum-name (level ...))
       #`(begin
           (define enum-name
             (make-enum-type '(level ...)))

           #,@(map (lambda (lvl)
                     (with-syntax ((log (id 'log- lvl))
                                   (lvl lvl))
                       #'(define-syntax log
                           (lambda (y)
                             (syntax-case y ()
                               ((log . args)
                                #`(log-msg
                                   '#,(datum->syntax
                                       y (syntax-source #'log))
                                   'lvl #,@#'args)))))))
                   #'(level ...)))))))

(define-log-level log-level
  (debug info warning error critical))

(define* (setup-logging #:key (level 'warning))
  "Configure and open logger at LEVEL)."
  (let* ((level-enum (or (enum-name->enum log-level level)
                         (begin
                           (format (current-error-port)
                                   "error: invalid log level~%")
                           (exit 1))))
         (lgr        (make <logger>))
         (console    (make <port-log> #:port (current-error-port)))
         (levels-count (enum-type-size log-level)))

    ;; Register logging handlers.
    (add-handler! lgr console)

    ;; Configure the log level.
    (let loop ((enum (enum-min log-level)))
      (let ((lvl (enum-name enum)))
        (unless (eq? level lvl)
          (disable-log-level! lgr lvl)
          (loop (enum-next enum)))))

    ;; Activate the configured logger.
    (set-default-logger! lgr)
    (open-log! lgr)
    (log-debug "logging initialized")))

(define (shutdown-logging)
  "Flush and destroy logger."
  (flush-log)
  (close-log!)
  (set-default-logger! #f))

debug log:

solving 913bc9ed3f ...
found 913bc9ed3f in https://yhetil.org/guix-patches/8aa12707e491aa2fc5da48ba3877d368fe5710cf.1707192720.git.maxim.cournoyer@gmail.com/ ||
	https://yhetil.org/guix-patches/0b81168e693a21f7fb08bd7677a5cc3120418fc9.1707626620.git.maxim.cournoyer@gmail.com/

applying [1/1] https://yhetil.org/guix-patches/8aa12707e491aa2fc5da48ba3877d368fe5710cf.1707192720.git.maxim.cournoyer@gmail.com/
diff --git a/guix/logging.scm b/guix/logging.scm
new file mode 100644
index 0000000000..913bc9ed3f

Checking patch guix/logging.scm...
Applied patch guix/logging.scm cleanly.

skipping https://yhetil.org/guix-patches/0b81168e693a21f7fb08bd7677a5cc3120418fc9.1707626620.git.maxim.cournoyer@gmail.com/ for 913bc9ed3f
index at:
100644 913bc9ed3f95564587e95ff1df9d1e1fd0badffd	guix/logging.scm

(*) Git path names are given by the tree(s) the blob belongs to.
    Blobs themselves have no identifier aside from the hash of its contents.^

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/guix.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).