From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:33036) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1etWta-0001Dg-C0 for guix-patches@gnu.org; Wed, 07 Mar 2018 06:06:07 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1etWtW-0000fp-FZ for guix-patches@gnu.org; Wed, 07 Mar 2018 06:06:06 -0500 Received: from debbugs.gnu.org ([208.118.235.43]:40580) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1etWtW-0000fS-BU for guix-patches@gnu.org; Wed, 07 Mar 2018 06:06:02 -0500 Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1etWtW-0004VJ-3M for guix-patches@gnu.org; Wed, 07 Mar 2018 06:06:02 -0500 Subject: [bug#30498] [PATCH 2/3] Simplify 'make-shepherd-output-port'. Resent-Message-ID: From: Ludovic =?UTF-8?Q?Court=C3=A8s?= Date: Wed, 7 Mar 2018 12:04:53 +0100 Message-Id: <20180307110454.17110-3-ludo@gnu.org> In-Reply-To: <20180307110454.17110-1-ludo@gnu.org> References: <87371ea2jj.fsf@gnu.org> <20180307110454.17110-1-ludo@gnu.org> List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guix-patches-bounces+kyle=kyleam.com@gnu.org Sender: "Guix-patches" To: 30498@debbugs.gnu.org * modules/shepherd/comm.scm (%not-newline): New variable. (make-shepherd-output-port): Rewrite second method to simplify and make a single 'display' call per line. --- modules/shepherd/comm.scm | 36 ++++++++++++++++-------------------- 1 file changed, 16 insertions(+), 20 deletions(-) diff --git a/modules/shepherd/comm.scm b/modules/shepherd/comm.scm index 596a258..e686bfa 100644 --- a/modules/shepherd/comm.scm +++ b/modules/shepherd/comm.scm @@ -216,6 +216,9 @@ on service '~a':") ;; 'strftime' format strings for entries in the log file. (make-parameter default-logfile-date-format)) +(define %not-newline + (char-set-complement (char-set #\newline))) + ;; We provide our own output mechanism, because we have certain ;; special needs; most importantly, we want to send output to herd ;; sometimes. @@ -242,26 +245,19 @@ on service '~a':") ;; completed line. (if (not (string-index str #\newline)) (set! buffer (cons str buffer)) - (let* ((log (lambda (x) - (display x (log-output-port)))) - (init-line (lambda () - (log (strftime (%current-logfile-date-format) - (localtime (current-time))))))) - (init-line) - (for-each log (reverse buffer)) - (let* ((lines (string-split str #\newline)) - (last-line (car (take-right lines 1))) - (is-first #t)) - (for-each (lambda (line) - (if is-first - (set! is-first #f) - (init-line)) - (log line) - (log #\newline)) - (drop-right lines 1)) - (set! buffer (if (string-null? last-line) - '() - (list last-line)))))))) + (let* ((str (string-concatenate-reverse (cons str buffer))) + (lines (string-tokenize str %not-newline))) + (define prefix + (strftime (%current-logfile-date-format) + (localtime (current-time)))) + + ;; Make exactly one 'display' call per line to make sure we + ;; don't create several entries for each line. + (for-each (lambda (line) + (display (string-append prefix line "\n") + (log-output-port))) + lines) + (set! buffer '()))))) ;; Flush output. (lambda () -- 2.16.2