Package: guix-patches;
Reported by: Maxim Cournoyer <maxim.cournoyer <at> gmail.com>
Date: Tue, 6 Feb 2024 04:15:01 UTC
Severity: normal
Tags: patch
View this message in rfc822 format
From: Maxim Cournoyer <maxim.cournoyer <at> gmail.com> To: 68946 <at> debbugs.gnu.org Cc: Maxim Cournoyer <maxim.cournoyer <at> gmail.com>, Christopher Baines <guix <at> cbaines.net>, Josselin Poiret <dev <at> jpoiret.xyz>, Ludovic Courtès <ludo <at> gnu.org>, Mathieu Othacehe <othacehe <at> gnu.org>, Ricardo Wurmus <rekado <at> elephly.net>, Simon Tournier <zimon.toutoune <at> gmail.com>, Tobias Geerinckx-Rice <me <at> tobias.gr> Subject: [bug#68946] [PATCH v2] guix: Add logging module. Date: Sat, 10 Feb 2024 23:43:41 -0500
* configure.ac: Require Guile-Lib. * guix/logging.scm: New module. * Makefile.am (MODULES): Register it. * guix/ui.scm (show-guix-help): Document --log-level global option. (%log-level): New parameter. (run-guix-command): Init logging. (run-guix): Parse new --log-level option. Change-Id: I5026a0d62119615fec3cd0131309f9bcc346a7e9 --- Changes in v2: - Honor the GUIX_LOG_LEVEL environment variable Makefile.am | 5 ++- configure.ac | 6 +-- guix/logging.scm | 98 ++++++++++++++++++++++++++++++++++++++++++++++++ guix/ui.scm | 19 +++++++++- 4 files changed, 122 insertions(+), 6 deletions(-) create mode 100644 guix/logging.scm diff --git a/Makefile.am b/Makefile.am index cef972880c..6929ee5f69 100644 --- a/Makefile.am +++ b/Makefile.am @@ -14,7 +14,7 @@ # Copyright © 2018 Oleg Pykhalov <go.wigust <at> gmail.com> # Copyright © 2018 Alex Vong <alexvong1995 <at> gmail.com> # Copyright © 2019, 2023 Efraim Flashner <efraim <at> flashner.co.il> -# Copyright © 2020, 2021, 2023 Maxim Cournoyer <maxim.cournoyer <at> gmail.com> +# Copyright © 2020, 2021, 2023, 2024 Maxim Cournoyer <maxim.cournoyer <at> gmail.com> # Copyright © 2021 Chris Marusich <cmmarusich <at> gmail.com> # Copyright © 2021 Andrew Tropin <andrew <at> trop.in> # Copyright © 2023 Clément Lassieur <clement <at> lassieur.org> @@ -125,7 +125,8 @@ MODULES = \ guix/substitutes.scm \ guix/upstream.scm \ guix/licenses.scm \ - guix/lint.scm \ + guix/lint.scm \ + guix/logging.scm \ guix/glob.scm \ guix/git.scm \ guix/git-authenticate.scm \ diff --git a/configure.ac b/configure.ac index ecbd596a34..133692c033 100644 --- a/configure.ac +++ b/configure.ac @@ -149,12 +149,12 @@ if test "x$guix_cv_have_recent_guile_git" != "xyes"; then AC_MSG_ERROR([A recent Guile-Git could not be found; please install it.]) fi -dnl Check for the optional Guile-Lib. +dnl Require Guile-Lib, for logging and HTML parsing. +GUILE_MODULE_REQUIRED([logging logger]) GUILE_MODULE_EXPORTS([have_guile_lib], [(htmlprag)], [%strict-tokenizer?]) AM_CONDITIONAL([HAVE_GUILE_LIB], [test "x$have_guile_lib" = "xyes"]) AM_COND_IF(HAVE_GUILE_LIB,, - [AC_MSG_WARN([The Guile-Lib requirement was not satisfied (>= 0.2.7); -Some features such as the Go importer will not be usable.])]) + [AC_MSG_ERROR([The Guile-Lib requirement was not satisfied (>= 0.2.7)])]) dnl Check for Guile-zlib. GUIX_CHECK_GUILE_ZLIB diff --git a/guix/logging.scm b/guix/logging.scm new file mode 100644 index 0000000000..913bc9ed3f --- /dev/null +++ b/guix/logging.scm @@ -0,0 +1,98 @@ +;;; GNU Guix --- Functional package management for GNU +;;; Copyright © 2023, 2024 Maxim Cournoyer <maxim.cournoyer <at> 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)) diff --git a/guix/ui.scm b/guix/ui.scm index 962d291d2e..f5a6966854 100644 --- a/guix/ui.scm +++ b/guix/ui.scm @@ -44,6 +44,7 @@ (define-module (guix ui) #:use-module (guix utils) #:use-module (guix store) #:use-module (guix config) + #:use-module (guix logging) #:use-module (guix packages) #:use-module (guix profiles) #:use-module (guix derivations) @@ -2225,6 +2226,9 @@ (define (show-guix-help) -h, --help display this helpful text again and exit")) (display (G_ " -V, --version display version and copyright information and exit")) + (display (G_ " + --log-level=LEVEL set the logging verbosity to LEVEL, one of + debug, warning, error or critical")) (newline) (newline) @@ -2248,6 +2252,10 @@ (define (show-guix-help) categories)) (show-bug-report-information)) +(define %log-level (make-parameter (or (and=> (getenv "GUIX_LOG_LEVEL") + string->symbol) + 'warning))) + (define (run-guix-command command . args) "Run COMMAND with the given ARGS. Report an error when COMMAND is not found." @@ -2282,10 +2290,12 @@ (define (run-guix-command command . args) (symbol-append 'guix- command)))) (parameterize ((program-name command)) (dynamic-wind - (const #f) + (lambda () + (setup-logging #:level (%log-level))) (lambda () (apply command-main args)) (lambda () + (shutdown-logging) ;; Abuse 'exit-hook' (which is normally meant to be used by the ;; REPL) to run things like profiling hooks upon completion. (run-hook exit-hook)))))) @@ -2311,6 +2321,13 @@ (define (run-guix . args) (leave-on-EPIPE (show-guix-help))) ((or ("-V") ("--version")) (show-version-and-exit "guix")) + (((? (cut string-prefix? "--log-level=" <>) o) args ...) + (parameterize ((%log-level (string->symbol + (second (string-split o #\=))))) + (apply run-guix args))) + (("--log-level" level args ...) + (parameterize ((%log-level (string->symbol level))) + (apply run-guix args))) (((? option? o) args ...) (format (current-error-port) (G_ "guix: unrecognized option '~a'~%") o) base-commit: 9edbb2d7a40c9da7583a1046e39b87633459f656 -- 2.41.0
GNU bug tracking system
Copyright (C) 1999 Darren O. Benham,
1997,2003 nCipher Corporation Ltd,
1994-97 Ian Jackson.