From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp2.migadu.com ([2001:41d0:303:e16b::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms8.migadu.com with LMTPS id sDG1HdNQyGVIjgAAe85BDQ:P1 (envelope-from ) for ; Sun, 11 Feb 2024 05:45:07 +0100 Received: from aspmx1.migadu.com ([2001:41d0:303:e16b::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp2.migadu.com with LMTPS id sDG1HdNQyGVIjgAAe85BDQ (envelope-from ) for ; Sun, 11 Feb 2024 05:45:07 +0100 X-Envelope-To: larch@yhetil.org Authentication-Results: aspmx1.migadu.com; dkim=fail ("headers rsa verify failed") header.d=gmail.com header.s=20230601 header.b=D9OR3mgl; spf=pass (aspmx1.migadu.com: domain of "guix-patches-bounces+larch=yhetil.org@gnu.org" designates 209.51.188.17 as permitted sender) smtp.mailfrom="guix-patches-bounces+larch=yhetil.org@gnu.org"; dmarc=fail reason="SPF not aligned (relaxed)" header.from=gmail.com (policy=none) ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=yhetil.org; s=key1; t=1707626707; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding:resent-cc: resent-from:resent-sender:resent-message-id:in-reply-to:in-reply-to: references:references:list-id:list-help:list-unsubscribe: list-subscribe:list-post:dkim-signature; bh=wBliG3Q3MoYQqQ1pkpO1UwC6PQP6u6/L1xx2yfhK5Cg=; b=gZKF9RntRiApCI3wr+9c5PMaCFGT6N9H5snmPfvJKZye24PZcygn+HvZhN1EWvWdvSEfNK aE81jwpMh1mugh1EN6NMFzMHwm1N4pCPnZqsdbvWYoeA6sEYJtU+2sMb9gK6s6+94kNVd5 kOviGzhjBUE4y5YOGuy3aDR48Lw6qPimz+7lXbflspxf5JUUv4o+inrtpP0D8P9n59K7E9 W5TEGc9hAr3urSyh6tFzc1df3pqj8NY4QBNksvR8pNhTbibbDHcMGQwfQ+PpBd0JwuoUeE wcFDM0tsS5R6jR0p7j4O/gC+7G7qP3R7Zrw6GfnlKQLQdx81lQ8sswoiY+QxvA== ARC-Authentication-Results: i=1; aspmx1.migadu.com; dkim=fail ("headers rsa verify failed") header.d=gmail.com header.s=20230601 header.b=D9OR3mgl; spf=pass (aspmx1.migadu.com: domain of "guix-patches-bounces+larch=yhetil.org@gnu.org" designates 209.51.188.17 as permitted sender) smtp.mailfrom="guix-patches-bounces+larch=yhetil.org@gnu.org"; dmarc=fail reason="SPF not aligned (relaxed)" header.from=gmail.com (policy=none) ARC-Seal: i=1; s=key1; d=yhetil.org; t=1707626707; a=rsa-sha256; cv=none; b=tIPe7cn4lnW/l0A81xI/7cPGQRg5RPnSmdB/SWdEMP5vHFBHdPW6lJP6FDCrsGg6+O1z3D tYNpfBhPtq1LqT4BosgMBGCb7apE+Lbft4RGpMlD+7JL14N/IQ5SbTkrRgsJjgXu2LJ6NL ArBdnZY2CHY6jM4Ua4V83EJYpPJdRbebrxq+aGmnuIgXpay9THiqV/FhbAMEhpIP+nK+ST /QqkWTFzoG93VrMrLQAa18hBuvH3kci/OO+MuKVVgXDiBGKGwVkw+VKfa8fv+SjoUDvjtY rPctx0FH1ic4yzejfJTaIoKiJdVtuThkdBY8830toID7Df0xRbQLQh9ihAwdDQ== Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by aspmx1.migadu.com (Postfix) with ESMTPS id 4F3AD4103F for ; Sun, 11 Feb 2024 05:45:07 +0100 (CET) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rZ1i1-0004Ut-Fl; Sat, 10 Feb 2024 23:44:53 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1rZ1hy-0004UF-P1 for guix-patches@gnu.org; Sat, 10 Feb 2024 23:44:51 -0500 Received: from debbugs.gnu.org ([2001:470:142:5::43]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1rZ1hx-0001x7-NJ; Sat, 10 Feb 2024 23:44:49 -0500 Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1rZ1iA-0000fw-FJ; Sat, 10 Feb 2024 23:45:02 -0500 X-Loop: help-debbugs@gnu.org Subject: [bug#68946] [PATCH v2] guix: Add logging module. References: In-Reply-To: Resent-From: Maxim Cournoyer Original-Sender: "Debbugs-submit" Resent-CC: guix@cbaines.net, dev@jpoiret.xyz, ludo@gnu.org, othacehe@gnu.org, rekado@elephly.net, zimon.toutoune@gmail.com, me@tobias.gr, guix-patches@gnu.org Resent-Date: Sun, 11 Feb 2024 04:45:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 68946 X-GNU-PR-Package: guix-patches X-GNU-PR-Keywords: patch To: 68946@debbugs.gnu.org Cc: Maxim Cournoyer , Christopher Baines , Josselin Poiret , Ludovic =?UTF-8?Q?Court=C3=A8s?= , Mathieu Othacehe , Ricardo Wurmus , Simon Tournier , Tobias Geerinckx-Rice X-Debbugs-Original-Xcc: Christopher Baines , Josselin Poiret , Ludovic =?UTF-8?Q?Court=C3=A8s?= , Mathieu Othacehe , Ricardo Wurmus , Simon Tournier , Tobias Geerinckx-Rice Received: via spool by 68946-submit@debbugs.gnu.org id=B68946.17076266692516 (code B ref 68946); Sun, 11 Feb 2024 04:45:02 +0000 Received: (at 68946) by debbugs.gnu.org; 11 Feb 2024 04:44:29 +0000 Received: from localhost ([127.0.0.1]:46304 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1rZ1hd-0000eV-0v for submit@debbugs.gnu.org; Sat, 10 Feb 2024 23:44:29 -0500 Received: from mail-qt1-x834.google.com ([2607:f8b0:4864:20::834]:53434) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1rZ1ha-0000eE-Bk for 68946@debbugs.gnu.org; Sat, 10 Feb 2024 23:44:27 -0500 Received: by mail-qt1-x834.google.com with SMTP id d75a77b69052e-42c6e2f3aa2so2364811cf.2 for <68946@debbugs.gnu.org>; Sat, 10 Feb 2024 20:44:10 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1707626644; x=1708231444; darn=debbugs.gnu.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=wBliG3Q3MoYQqQ1pkpO1UwC6PQP6u6/L1xx2yfhK5Cg=; b=D9OR3mgly4tT2XpUjDKwjIEV6TJTRPLtnt5VLaRJk9aqrJ3TBaCgLPF4wc+VgP7z0t nrIAjvYDdglE38krryBvtOPK6XCJXj49CfzS4IUkl+zVCTFM9AsvchsuktSNBnd7s2/g WIPczJnUed1xN5GvKuIXQZG9WqDHy1tYxm5w6JAg0HxKa0yf3/buNN0CCtt0e2eamJcu 5jDZSJxExbVBCwJmHCaFuR3/lQH6JCMAzFbi2D3IqHIFibV1lTy6vy04EOt6TV75/KXZ zuQzaG8dFLxvgmq42RGRphi4vNJn+k8tWPlROKTcLOlPncJmWoyV8sFB0lWWb0bqKtUz foAA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707626644; x=1708231444; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=wBliG3Q3MoYQqQ1pkpO1UwC6PQP6u6/L1xx2yfhK5Cg=; b=FFLPdtU9YSfLi1UPsmRQz4EVGPzilET0PvOlxOSybupSlaPwRyorVWiju0yXEIvPTZ uGC7rUnPL4kqSUj2rnHLLggCCmWo/DJ4W8UkUdVnbjf3A/zOzyIu3ocRWrpGVdbP9OXA xZ1LoFJV8pu/ZSeYB9m9k48926DFJ0U25DOW6jaOtXQBgTCuf1YKgVerVH5SI9cfmLF9 nZ6XeuDrLNpR/qIptrdmRtVSMUgMZHXhx3m3jc5CSW0xV+WX18INvym307G6KJPc83Hq FmFigaUuV4XNMO4jmOmMDSUzC80/uT8pLd6by3iRPsOYNwtoviDoGl0LS6dqN7qzhjAE CnfA== X-Gm-Message-State: AOJu0YwP1ojvCSLaXFZCkSvf9wQtLNsJY9o2SlvfIhPRSsP7e+hms68K LuLfx+Enou0pTFNkTT3hxUgbcdq1LG7sUZvjfXnKvFz9PoAlH3SWBcEmDEIf X-Google-Smtp-Source: AGHT+IE4a9bQ5xzveoOyT+6LK25QSEKlcDv+Xc4zl74uEyvCtajWori6soW2DE+EYHrG5HFkN97DVg== X-Received: by 2002:ac8:5c88:0:b0:42c:4826:7f04 with SMTP id r8-20020ac85c88000000b0042c48267f04mr5603150qta.29.1707626643999; Sat, 10 Feb 2024 20:44:03 -0800 (PST) Received: from localhost.localdomain (dsl-205-151-56-177.b2b2c.ca. [205.151.56.177]) by smtp.gmail.com with ESMTPSA id g7-20020ac85807000000b0042ab4a129easm2029619qtg.73.2024.02.10.20.44.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 10 Feb 2024 20:44:03 -0800 (PST) From: Maxim Cournoyer Date: Sat, 10 Feb 2024 23:43:41 -0500 Message-ID: <0b81168e693a21f7fb08bd7677a5cc3120418fc9.1707626620.git.maxim.cournoyer@gmail.com> X-Mailer: git-send-email 2.41.0 MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: guix-patches@gnu.org List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guix-patches-bounces+larch=yhetil.org@gnu.org Sender: guix-patches-bounces+larch=yhetil.org@gnu.org X-Migadu-Flow: FLOW_IN X-Migadu-Country: US X-Migadu-Spam-Score: 3.82 X-Migadu-Scanner: mx13.migadu.com X-Spam-Score: 3.82 X-Migadu-Queue-Id: 4F3AD4103F X-TUID: 4b7dKeAVItB9 * 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 # Copyright © 2018 Alex Vong # Copyright © 2019, 2023 Efraim Flashner -# Copyright © 2020, 2021, 2023 Maxim Cournoyer +# Copyright © 2020, 2021, 2023, 2024 Maxim Cournoyer # Copyright © 2021 Chris Marusich # Copyright © 2021 Andrew Tropin # Copyright © 2023 Clément Lassieur @@ -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 +;;; +;;; 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 . + +(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 )) + (console (make #: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