civodul pushed a commit to branch devel
in repository shepherd.

commit 0def34599cc03305d823bd0cdcf7291fbe4ff9f9
Author: Ludovic Courtès <l...@gnu.org>
AuthorDate: Wed Aug 7 23:07:59 2024 +0200

    logger: Factorize ‘rotate-and-reopen-log-file’.
    
    * modules/shepherd/logger.scm (log-line): New procedure, formerly in
    ‘%service-file-logger’.
    (rotate-and-reopen-log-file): New procedure, formerly in
    ‘%service-file-logger’.
    (%service-file-logger): Use them.
---
 modules/shepherd/logger.scm | 77 ++++++++++++++++++++++++++-------------------
 1 file changed, 44 insertions(+), 33 deletions(-)

diff --git a/modules/shepherd/logger.scm b/modules/shepherd/logger.scm
index 9937507..bb756f5 100644
--- a/modules/shepherd/logger.scm
+++ b/modules/shepherd/logger.scm
@@ -41,7 +41,8 @@
             logger-files
             rotate-log-file
 
-            open-log-file))
+            open-log-file
+            rotate-and-reopen-log-file))
 
 (define default-log-history-size
   ;; Number of lines of service log kept in memory by default.
@@ -97,6 +98,17 @@ first message received."
                       (get-operation channel1)
                       (get-operation channel2))))
 
+(define (log-line line output)
+  "Write @var{line} to @var{output} and return its timestamp."
+  (let* ((now (current-time))
+         (prefix (strftime default-logfile-date-format
+                           (localtime now))))
+    ;; Avoid (ice-9 format) to reduce heap allocations.
+    (put-string output prefix)
+    (put-string output line)
+    (newline output)
+    now))
+
 (define* (open-log-file file #:optional (mode #o640))
   "Open @var{file} as a log file, close-on-exec, in append mode, with UTF-8
 encoding, etc."
@@ -107,6 +119,31 @@ encoding, etc."
     (set-port-conversion-strategy! output 'substitute)
     output))
 
+(define* (rotate-and-reopen-log-file output file rotated-file
+                                     #:optional (mode #o640))
+  "Rotate @var{file}, currently opened as port @var{output}, to
+@var{rotated-file}; close @var{output} and reopen @var{file} with @var{mode}
+and return the resulting output port.  If rotation or reopening fails, return
+@code{#f}."
+  (local-output (l10n "Rotating '~a' to '~a'.")
+                file rotated-file)
+  (newline output)
+  (log-line (l10n "Rotating log.") output)
+  (close-port output)
+
+  (let ((output (catch 'system-error
+                  (lambda ()
+                    (rename-file file rotated-file)
+                    (open-log-file file mode))
+                  (lambda args
+                    args))))
+    (or (and (port? output) output)
+        (begin
+          (local-output (l10n "Failed to rotate '~a' to '~a': ~a.")
+                        file rotated-file
+                        (strerror (system-error-errno output)))
+          #f))))
+
 (define* (%service-file-logger channel file input
                                #:key
                                (service (current-service))
@@ -116,17 +153,6 @@ not exist."
   (define lines
     (make-channel))
 
-  (define (log-line line output)
-    ;; Write LINE to OUTPUT and return its timestamp.
-    (let* ((now (current-time))
-           (prefix (strftime default-logfile-date-format
-                             (localtime now))))
-      ;; Avoid (ice-9 format) to reduce heap allocations.
-      (put-string output prefix)
-      (put-string output line)
-      (newline output)
-      now))
-
   (lambda ()
     (spawn-fiber (line-reader input lines))
 
@@ -162,27 +188,12 @@ not exist."
                (loop messages service))
               (('rotate requested-file rotated-file reply)
                (if (string=? requested-file file)
-                   (begin
-                     (local-output (l10n "Rotating '~a' to '~a'.")
-                                   file rotated-file)
-                     (newline output)
-                     (log-line (l10n "Rotating log.") output)
-                     (close-port output)
-                     (let ((output (catch 'system-error
-                                     (lambda ()
-                                       (rename-file file rotated-file)
-                                       (open-log-file file))
-                                     (lambda args
-                                       args))))
-                       (put-message reply (port? output))
-                       (if (port? output)
-                           (log output messages service)
-                           (begin
-                             (local-output
-                              (l10n "Failed to rotate '~a' to '~a': ~a.")
-                              file rotated-file
-                              (strerror (system-error-errno output)))
-                             (loop messages service)))))
+                   (let ((output (rotate-and-reopen-log-file output file
+                                                             rotated-file)))
+                     (put-message reply (port? output))
+                     (if (port? output)
+                         (log output messages service)
+                         (loop messages service)))
                    (begin
                      (local-output (l10n "Ignoring request to \
 rotate '~a' (log file is '~a').")

Reply via email to