diff options
| author | Ludovic Courtès <ludo@gnu.org> | 2020-02-18 18:23:19 +0100 |
|---|---|---|
| committer | Ludovic Courtès <ludo@gnu.org> | 2020-02-22 00:45:42 +0100 |
| commit | 2cf65e1d543407bc7db43e7c7d39a215907efebc (patch) | |
| tree | 66d36459d0f6392f5ee457bc8b099a48e8341758 /gnu | |
| parent | b6ec284fe8b67bba4b17aab98641a91a17658133 (diff) | |
installer: Add 'syslog' macro to write to syslog.
* gnu/installer/utils.scm (open-syslog-port, syslog-port): New
procedures.
(syslog): New macro.
Diffstat (limited to 'gnu')
| -rw-r--r-- | gnu/installer/utils.scm | 43 |
1 files changed, 41 insertions, 2 deletions
diff --git a/gnu/installer/utils.scm b/gnu/installer/utils.scm index ddb96bc3382..a5f390e7a21 100644 --- a/gnu/installer/utils.scm +++ b/gnu/installer/utils.scm | |||
| @@ -1,6 +1,6 @@ | |||
| 1 | ;;; GNU Guix --- Functional package management for GNU | 1 | ;;; GNU Guix --- Functional package management for GNU |
| 2 | ;;; Copyright © 2018 Mathieu Othacehe <m.othacehe@gmail.com> | 2 | ;;; Copyright © 2018 Mathieu Othacehe <m.othacehe@gmail.com> |
| 3 | ;;; Copyright © 2019 Ludovic Courtès <ludo@gnu.org> | 3 | ;;; Copyright © 2019, 2020 Ludovic Courtès <ludo@gnu.org> |
| 4 | ;;; | 4 | ;;; |
| 5 | ;;; This file is part of GNU Guix. | 5 | ;;; This file is part of GNU Guix. |
| 6 | ;;; | 6 | ;;; |
| @@ -24,12 +24,16 @@ | |||
| 24 | #:use-module (srfi srfi-34) | 24 | #:use-module (srfi srfi-34) |
| 25 | #:use-module (ice-9 rdelim) | 25 | #:use-module (ice-9 rdelim) |
| 26 | #:use-module (ice-9 regex) | 26 | #:use-module (ice-9 regex) |
| 27 | #:use-module (ice-9 format) | ||
| 27 | #:use-module (ice-9 textual-ports) | 28 | #:use-module (ice-9 textual-ports) |
| 28 | #:export (read-lines | 29 | #:export (read-lines |
| 29 | read-all | 30 | read-all |
| 30 | nearest-exact-integer | 31 | nearest-exact-integer |
| 31 | read-percentage | 32 | read-percentage |
| 32 | run-shell-command)) | 33 | run-shell-command |
| 34 | |||
| 35 | syslog-port | ||
| 36 | syslog)) | ||
| 33 | 37 | ||
| 34 | (define* (read-lines #:optional (port (current-input-port))) | 38 | (define* (read-lines #:optional (port (current-input-port))) |
| 35 | "Read lines from PORT and return them as a list." | 39 | "Read lines from PORT and return them as a list." |
| @@ -91,3 +95,38 @@ COMMAND exited successfully, #f otherwise." | |||
| 91 | (newline) | 95 | (newline) |
| 92 | (pause) | 96 | (pause) |
| 93 | #t)))) | 97 | #t)))) |
| 98 | |||
| 99 | |||
| 100 | ;;; | ||
| 101 | ;;; Logging. | ||
| 102 | ;;; | ||
| 103 | |||
| 104 | (define (open-syslog-port) | ||
| 105 | "Return an open port (a socket) to /dev/log or #f if that wasn't possible." | ||
| 106 | (let ((sock (socket AF_UNIX SOCK_DGRAM 0))) | ||
| 107 | (catch 'system-error | ||
| 108 | (lambda () | ||
| 109 | (connect sock AF_UNIX "/dev/log") | ||
| 110 | (setvbuf sock 'line) | ||
| 111 | sock) | ||
| 112 | (lambda args | ||
| 113 | (close-port sock) | ||
| 114 | #f)))) | ||
| 115 | |||
| 116 | (define syslog-port | ||
| 117 | (let ((port #f)) | ||
| 118 | (lambda () | ||
| 119 | "Return an output port to syslog." | ||
| 120 | (unless port | ||
| 121 | (set! port (open-syslog-port))) | ||
| 122 | (or port (%make-void-port "w"))))) | ||
| 123 | |||
| 124 | (define-syntax syslog | ||
| 125 | (lambda (s) | ||
| 126 | "Like 'format', but write to syslog." | ||
| 127 | (syntax-case s () | ||
| 128 | ((_ fmt args ...) | ||
| 129 | (string? (syntax->datum #'fmt)) | ||
| 130 | (with-syntax ((fmt (string-append "installer[~d]: " | ||
| 131 | (syntax->datum #'fmt)))) | ||
| 132 | #'(format (syslog-port) fmt (getpid) args ...)))))) | ||
