civodul pushed a commit to branch master
in repository shepherd.
commit e45820839e23a19a00347d3b8f88206243fb3eb0
Author: Ludovic Courtès <[email protected]>
Date: Wed Sep 19 16:23:49 2018 +0200
comm: Do not introduce empty lines in the log.
Fixes a regression introduced in
329cec8b6fcc57f945e748793470f573b2b449a3 whereby regular "has been
started" etc. lines in the log would be followed by an empty line.
* modules/shepherd/comm.scm (make-shepherd-output-port): Use
'string-index' in a loop instead of 'string-split' to detect newlines in
STR.
* tests/basic.sh: Check for zero empty lines in $log.
---
modules/shepherd/comm.scm | 21 +++++++++++++--------
tests/basic.sh | 5 +++++
2 files changed, 18 insertions(+), 8 deletions(-)
diff --git a/modules/shepherd/comm.scm b/modules/shepherd/comm.scm
index 43110a7..c31bf02 100644
--- a/modules/shepherd/comm.scm
+++ b/modules/shepherd/comm.scm
@@ -308,18 +308,23 @@ available."
;; completed line.
(if (not (string-index str #\newline))
(set! buffer (cons str buffer))
- (let* ((str (string-concatenate-reverse (cons str buffer)))
- (lines (string-split str #\newline)))
+ (let ((str (string-concatenate-reverse (cons str buffer))))
(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)
+ ;; Note: We want to render as many newlinew as present in STR,
+ ;; so neither 'string-split' nor 'string-tokenize' helps.
+ (let loop ((str str))
+ (let* ((index (string-index str #\newline))
+ (line (if index (string-take str (+ 1 index)) str)))
+ (unless (string-null? str)
+ ;; Make exactly one 'display' call per line to make sure we
+ ;; don't create several entries for each line.
+ (display (string-append prefix line) (log-output-port))
+ (when index
+ (loop (string-drop str (+ index 1)))))))
+
(set! buffer '())))))
;; Flush output.
diff --git a/tests/basic.sh b/tests/basic.sh
index fc53aa7..cd4ade9 100644
--- a/tests/basic.sh
+++ b/tests/basic.sh
@@ -98,6 +98,11 @@ then false; else true; fi
$herd enable test-2
$herd start test-2
+# Make sure we didn't emit empty lines in the log (strip the timestamp that
+# prefixes each line.)
+test `wc -l < "$log"` -gt 0
+test `cut -c 21- < "$log" | grep "^$" | wc -l` -eq 0
+
# Try a custom action; make sure we get all the lines, including the empty
# lines (this was not the case in 0.4.0.)
$herd doc test-2 action hi | grep "Say hi\."